Thursday, August 22, 2019

Arduino Touch Sensors and i2c OLED display tests

Today I have been looking at some inexpensive touch sensors and a 128x64 pixel graphical OLED Display unit.

Its been a while since I have had chance to do much in the way of experimentation, and I don't really have the bench space for much these days (In fact I don't really have a bench any more - its more of a shelf now!) But that's another story.

Anyway, I picked up two touch sensor PCBs and the OLED for just a few pounds. Both are plentifully available on both Amazon or eBay (see bottom of post for links if you want to grab them and experiment yourself).


The touch sensors are a simple PCB with just three connections: +V, 0V and output (hi/lo).

Really easy to use and fantastically sensitive to touch. No false triggering noted. Awesome!


I have two here plugged into the breadboard. One is designated as "dot" and the other is "dash" (as this is a test for a future morse paddle keyer system).

A simple Arduino sketch running on a nano checking if the "dot" "dash" or "both" are touched. (see code below)





The plan is to mount the two touch sensors vertically opposite each other with roughly the same spacing as a conventional paddle key.

Currently I have a simple spacer block mocked up in Tinkercad ready for 3D printing as part of the eventual enclosure for the device.





And at the moment just the the status of the touch sensor inputs is pushed out to the 128x64 pixel OLED Display as plain text.

Thats about all we do for now, but there will of course be a lot more going on than this eventually as it evolves to be part of a touch sensitive  Arduino powered paddle keyer system.

More info will undoubtedly follow.



Sensors : There really isn't much to say about the touch sensors. You simply connect them up to either 3v3 or 5v (either is fine) and ground. The third pin goes high when you touch the pcb (either side), and returns low when you release.  I don't have my scope here, but based on what I can see there doesn't seem to be any appreciable delay or bounce on the make/break action.  You could use them in any number of fun ways as they work through some reasonably thick paper, plastic or fabric - ie: a totally invisible switch if you so needed it.

OLED :  Although I have used the ever popular 16x2 char LCD display units with arduinos many times in the past, this is the first time I have actually got around to interfacing a graphical  OLED device to one.

It took a while to find an arduino library that worked with the OLED.  Initially I could not coax it into life at all, and a little bit of googling was needed to find the correct i2c bus address for it. The usual Adafruit SSD1603 library did talk to it once I edited the bus address, but produced a garbled result with just a couple of lines showing the test patterns with the rest of the display just noise.

A lot of head scratching and some more googling followed!

Eventually the reason for this came to light. I had assumed when I ordered the OLED unit that it was a standard SSD1306 based device, but it turns out it is actually an SH1106 device.

Very similar, but with minor driver differences.

Thankfully the SSD1306Ascii Arduino library by William Germain  https://github.com/greiman/SSD1306Ascii is not only super memory efficient for simple text display on SSD1306 devices -  but also has support for the SH1106 variant included too.  Yay!

If you have a recent version of the Arduino IDE you can load it via the library manager, if not the libs can be grabbed from github at the above address and installed into your arduino libraries subfolder

This is the simple arduino sketch I used as a proof of concept test :


// Simple I2C test for ebay / amazon SH1106 128x64 oled.

#include <wire .h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"

// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C

// Define proper RST_PIN if required.
#define RST_PIN -1

// Define the digital inout pins from the touch sensors
const int dotPin = 4;
const int dashPin = 5;

int dotState =0;
int dashState =0;

SSD1306AsciiWire oled;
//------------------------------------------------------------------------------
void setup() {
  Wire.begin();
  Wire.setClock(400000L);

#if RST_PIN >= 0
  oled.begin(&SH1106_128x64, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
  oled.begin(&SH1106_128x64, I2C_ADDRESS);
#endif // RST_PIN >= 0

oled.clear();  // clear the display
oled.displayRemap(1); //rotate 180 degrees to
oled.setFont(lcd5x7); // select a font
oled.set2X(); // double char size

pinMode(dotPin, INPUT);
pinMode(dashPin, INPUT);  
  
  
}
//------------------------------------------------------------------------------
void loop() {

oled.setRow(1); // move to the 1,1 position
oled.setCol(1);
    
dotState=digitalRead(dotPin); //read the inputs from the touch sensor boards
dashState=digitalRead(dashPin);   

if (dotState==HIGH && dashState==HIGH){
    oled.println("Both");
} else {
  if (dotState==HIGH){
      oled.println("Dot ");
  }else if (dashState==HIGH){
      oled.println("Dash");
  }else{
    oled.clear();
 }
}


}


That should all pretty much make sense without a lot of further explanation.  I will post a lot more info as the project progresses in the future.

If you want any of the parts I used you can find them here:

Touch sensors : https://amzn.to/2Nq8p0c
OLED i2c screen : https://amzn.to/31XItwW
Arduino Nano clone : https://amzn.to/2MwVR7t

And if you need a breadboard and jumper cables there is a nice starter kit : https://amzn.to/31UkHSn