Available Languages?:

# "Piano" with RTOS

## Intro

(Sorry for my appalling english)

This arcticle describes a possibility of ADC usage to read touch sence buttons. As example we will encode program "Piano" for working with 36 capacitive sensing buttons (3 octaves). To avoid bore, we will make it polyphonic. For our project we will use demo-boards from PicKit2 kits based on PIC16F690, PIC16F886 or PIC16F887. In addition we use PIC16F88.

Here is two minutes demonstration video (sorry for my bad piano play).

## Theory

### Capacitive sensing

Capacitive sensing principles are described here. When we touch couper pad by finger, we increase scheme capacitance. The controller can detect capacitance increasing. In this case couper pad is used as capacitive sensor (like capacitor with small capacitance). If we will charge this capacitor with constant current, then voltage will increase in inverse proportion to capacity. Let's assume that we make measures of voltage on capacitor at the same times after start of charging. It is evident that low-capacity capacitor will have charged less than the capacitor of large capacity. Look at the picture:

Two capacitors C1 and C2 (capacitance of C1 is less then capacitance of C2) are charged with the same current. At the time t0 voltage at C1 increased by more than at C2. We know that touching couper pad by finger will increase capacitance. Imagine that we periodically charge/discharge capacitor (capacitive sensor) and makes measurings of voltage every time with same delay after start. While capacitance of sensor remain unchanged, we will read the same voltage's value. But when we will touch sensor by finger, the capacitance will increase and voltage will decrease. Thus we can detect the touching.

This principle of reading voltage on capacitive sensor after the same delay after start of charging we will use to read our touch buttons.

#### Implementation

We need four things:

1. a capacitive sensor;
2. a current source;
3. a precise delay;
4. a voltage meter.

First of all we need a capacitive sensor. We can use any metal plate, e.g. a coin.

Now we need a current source. Ideally, it must be a source of constant current. Is there any way to simplify this requirement? Can we apply just a RC-circuit? Look at the picture:

Capacitors C1 and C2 (C1 < C2) are in RC-circuit with the identical values of resistance. It is obviously that C1 will be charged faster than C2. The charge digrams are not linear, however we can determine in which case the capacitance is larger, because of at the time t0 values of voltages are different. Thus we can use simple RC-circuits instead of current sources.

Next we need to form a precise delay. Due to fact that time of charging is too little, we can form this delay with empty cycle. While delay is active we have to disable interrupts to avoid increasing time of charging.

And we will use ADC module as a voltage meter. Look at the picture:

When we need to measure capacitance, we follow that steps:

1. At the begin: U(Ch)=0, AIN is digital output = "0", DOUT = "0".
2. At the point t pin DOUT becames "1", and pin AIN switches to analog input mode.
3. At the point t0 we set GODONE = 1 to start AD convertion. Writing 1 to GODONE will disconnect internal capacitor Chold from external scheme and the voltage on Chold will be held and measured.
4. After the measuring is done (at moment t1) pin AIN switches to digital output with "0"-state to discharge Ch.
5. Pin DOUT set to "0" to reduce power consumption.

As a result we have a scheme to connect one touch button:

(In out example we use 1 ruble coin as capacitive sensor). While we do not touch a coin by finger, PIC measures the Cp - sum of capacitance of analog input pin, capacitance between coin and ground, coin and other coins, between wires. When we touch coin by finger, we add capacitor Ch (human), the resulting capacity increases.

#### Many buttons

But thus we are limited by number of analog inputs (e.g. 7 at PIC16F88). Piano with 7 keys will be too uninteresting. We need more keys. Let's try to modify our scheme to get possibility of connection more than one button per one analog input. Do do it we just add a splitting diodes:

As you see at the picture, we can measure capacitance of each sensor separately using only one analog input by controlling digital outputs DOUT1 and DOUT2. Thus we can connect to one analog input three, four, five or more keys. We are limitted by number of pins that can be used as digital control pins DOUTx only.

If we will use 6 analog inputs and 6 control digital outputs, then we can to read matrix of 6x6 = 36 capacitive sensors! As a result we have a scheme of touch buttons connecting:

That's it!      