Analogue pin problem.

I am having trouble with the following circuit.

Q1, Q2 and Q3 have their emitters connected to analogue pins 0, 1 and 2 on my arduino and I am monitoring the voltage levels.

Imagine that S1 and S2 are keys on an electronic keyboard. What I am trying to do is to ultimately map 24 keys (or what ever) to 24 unique combinations of HIGHs and LOWs on the 6 analogue pins.

What I was expecting to happen was that, when I close S1, analogue pins 0 and 2 would go HIGH but analogue pin 1 would remain LOW.

But it is not working as I envisaged.

I soon discovered that the voltage level of floating analogue pins oscillate fairly unpredictably around roughly 2V.

And when I do close S1, all 3 analogue pins go to HIGH (4V or there abouts).

Any suggestions on how I can get this to work as expected.

why not making a multiple voltage divider and let the keys connect all to 1 analogIn?
0V --[r1] –[r2]-–[r3] -* … *-[r24]–5V
keys are connected to * on one side and to the analog in on the other…

get the idea?

why not making a multiple voltage divider and let the keys connect all to 1 analogIn?
0V --[r1] --[r2]---[r3] -* ... *-[r24]--5V
keys are connected to * on one side and to the analog in on the other...

get the idea?

Yeah that did occur to me but then I would be limited to 6 keys for the six analogue pins.

The way I have devised allows me up to 64 keys - 2 x 2 x 2 x 2 x 2 x2.

Or if I adjust the circuitry accordingly for the digital pins then it could allow me many more keys.

Actually I figured out what was wrong with my circuit anyway. I need a high value resistor, e.g. 100k, from each arduino analogue pin to GND to tie the analogue pins down to a known reference value for when the transistor switches on and its emitter goes HIGH.

And I need diodes on both the leads leaving S1 to the transistor bases and diodes on both leads leaving S2 to the transistor bases. I suspect 1N4007 is not ideal due to voltage drops etc, but that is what I have used on my bread board. 1N5819 would probably be a better choice or a signal diode.

If I do that then the serial output from the arduino behaves exactly as I expect. Unconnected analogue pins are well below 1V and connected analogue inputs are from 2-5V.

I meant something like this:

Why are you using analogue input?
Analogue readings will always be full of noice and other grounds for fail readings.
If you are only using switches, then there are no reason to read it as analogue input (?)

There are a way for reading as many switches you want for the cost of only the switches. No need for resistors, transistors or capasitors.
The technology is called a "Matrix". It is used in all keyboards, keypads and even remote contollers.

For some reason, I'm not alowed to upload pictures in this forum, so I'm just linking to a microcontroller sellers chematics of a product...

I want you do dedicate 4 pins of your controller to be output. Call them ROW1-4.
Dedicate 4 pins of your controller to be input. Call then COL1-4
In a loop:
I want you to set pin "ROW1" to 5volt, and read all the digital signals of COL1, COL2, COL3 and COL4.

  • If any keys on that row is pressed, the corresponding COL? vill be read as 5volt (or a logical 1).
    Then I want you to set ROW1 to 0volt and set ROW2 to 5volt. Then repeat the COL-polling.
    When the "ROW4" has been dealt with, take a short break, and restart with the "ROW1" :wink:

This is of cause not limited to 4x4 keypads. If you add one more "ROW", you will get 4 more switches.
... You get the picture ...
For 24 switches, you will need 5 ROWs and 5COLs. That will add up to 25 possible switches.