/arduino

Building a keyboard with Arduino

Update After completing some tests I've now varnished the top of the keyboard, so no more sticky fingers slowly removing the paint!

Read about it in this section

Ages ago, my wife bought me some conductive paint from a company called Bare Conductive and I've been looking for something to do with it ever since. This article is all about what I built.

Scene Setting

As well as a tub of conductive paint and a large stash of electronic components, I also have a ~18 month old toddler. He loves things which he can interact with, that play noises and shine lights based on him triggering them.

So I decided to try to build a toy which plays musical notes when he touches it - like a keyboard, but with big simple keys and only eight notes (one octave).

Capacitive Sensing

I wanted to use the paint to create 'keys' that when touched triggered the action of playing sound and illuminating a light. A simple way to do this is to take advantage of the change in capacitance that occurs when a human touches a conductor with a small voltage flowing through it. It is is the same approach that is used by smartphone screens, however those are much more sophisticated as they need to be able to tell the position and manner of touch rather than the simple binary of 'touched' or 'not-touched'. You can read more about the physics of this here https://en.wikipedia.org/wiki/Capacitive_sensing.

Luckily for me, somebody has previously created a library for Arduino to perform capacitive sensing using two pins, so I decided to use some Arduino chips I had lying around for this project. The details of the library are here https://playground.arduino.cc/Main/CapacitiveSensor/

My first step was building a prototype on a piece of cardboard to see if I could detect touch:

Prototype of cap touch keyboard

I did this by painting the pads on the cardboard and then taping some 22AWG wire to them. It worked like a charm!

Arduino without the board

So I made the decision to use the Arduino, but I did not want the bulk of the Arduino Uno board in my project. Luckily it is quite simple to run an Atmel ATMega328 chip (the microprocessor used by Arduino) outside of the Uno board. All you need is the 16MHz crystal, a couple of 18-22pF capacitors and a 10k resitor to pull-up the reset pin. There are more details on this here: https://www.arduino.cc/en/Tutorial/ArduinoToBreadboard

So I set about designing my circuit and checking it worked on a breadboard (which it did).

circuit

The antennas on my circuit digram represent the sensor pads.

full prototype on breadboard

Building the circuit

I did not want to have a breadboard inside my project, so I needed to build a circuit board on which to put all the components. I had a bunch of strip-board (veroboard) lying around from a previous bit of work I did so I decided to use this. Stripboard is pretty simple stuff, it consists of vertical tracks that you can solder components to and when you don't want comonents to be connected you can break the track with a special tool (or like me a 2.5mm drill bit). My design for the veroboard is below.

veroboard design for arduino keyboard

The red and black rails are the +5v and 0v (gnd) respectively. Where there are green dots there is soldering and the red crosses are where the tracks need to be broken.

Here's my completed board - both the neat side and the side with my (quite poor) soldering:

arduino keyboard circuit board

Having done this I remember why I chose software engineering over electrical engineering :)

Power

The ATmega328 is quite forgiving when it comes to power requirements. It will operate anywhere between 2.7 and 5.5v. Which is great, because USB supplies 5v so that means I can use USB to power the circuit.

The code

Capacitive sensing is not an exact science and different bodies can hold different amounts of charge and therefore have different amounts of capacitance. I needed something that would work with me, my wife and my son who is considerably smaller than us! I tried various methods from setting an absolute threshold that must be exceeded to using averages and moving averages. In the end I settled on taking an average of all the pin readings and considering any pin whose value was more than 4 times the average was 'high' and the others were 'low'. This provided a good balance between performance and reducing false triggers.

I found a number of webpages with the frequencies for musical notes and it was a simple case of using a piezo buzzer + the tone function to generate the tones when the keys were pressed.

The code is here: https://github.com/richardjkendall/arduino-keyboard

The enclosure

I'll freely admit that my practical skills in building things are very poor! I decided to try a simple way of building the enclosure which was to use wood panels for the front and back and square dowel for the sides. I got some balsa wood from a local DIY place and some glue and set about building it.

I then used tape to make a mask so I could apply the conductive paint to the front and end up with well-defined shapes. The red tape in the pictures is Christmas gift wrapping tape - it was exactly 1cm thick and so perfect based on the measurements I wanted.

painting the conductive paint on the keyboard

To connect the sensor pads I drilled 2mm holes in the wood and then put stripped 22AWG multi-core wire through the holes. I then glued the wire in place using a glue-gun (on the non-painted side) and trimmed it so it was flush with the painted surface. Then I repainted over the wire and hole to form a connection between the painted sensor pad and the wire. That approach is illustrated in the diagram below

cross-section of wood showing how cable is attached

Then I drilled holes for the LEDs and installed them, soldering wire to their contacts at the same time.

Finally I soldered the LED and sensor wires, and added the buzzer and USB power connector - then I was ready for a final test before closing the case up!

Finished Keyboard

Here's the finished item!

finished keyboard

Here's my son playing with it...

playing with the keyboard

Varnish

After finding that the conductive paint comes off easily when touched by wet fingers I decided to try and varnish the case so that it protects the paint. I picked an oil based clear varnish and conducted a test with a spare piece of wood I painted with the same conductive paint.

Screen shot of test with varnish

I found through this test that the sensors still performed adequately so I painted the whole case with the same varnish.

-- Richard, May 2020