Using multiple buttons on a single pin
In this article you will learn how to use multiple buttons with single Arduino pin. 50 + buttons can be connected.
Intro
You may have a project that requires multiple buttons for inputting things like character(or making a tiny keyboard).Voltage Divider Circuit
Using this circuit we can get different voltages by dividing input voltage.Suppose 4 resistors of 50Ω are connected in a series and their end is connected to power supply say 5V. Then voltage across GND and different resistances are -
For R1
Vout = (Input voltage x R1)/(Total Resistance)
= 550/200
= 1.25*V
For R2
Vout = (Input voltage x (R1+R2) ) / (Total Resistance)
= 5100/200
= 2.5*V
For R3
Vout = (Input voltage x (R1+R2+R3) ) / (Total Resistance)
= 5150/200
= 3.75*V
For R4
Vout = (Input voltage x (R1+R2+R3+R4) ) / (Total Resistance)
= 5150/200
= 5.00*V
It can be observed if we use equal resistances, then a voltage is equally divided into and rises from GND to +5V
How to Use with Arduino
Precautions First
- Power Voltage Regulator circuit with Arduino pins(+5v and GND) only, highly recommended.
- A minimum total resistance circuit should 200Ω so less current will flow from +5v to GND and less heat will be generated.According OMHs low I = 5/200 = 25mA.
- Don’t use an external power supply for voltage divider circuit, also its easier to use Arduino power pins(+5v and GND).
- That’s enough.
Ciruit
Check This
Coding
Analog reading = voltage * 1023/5Example for 1.25 V
Analog Reading = 1.25 * 1023/5
= 255.75 = 256
Following code doesn’t need anything to be written in void setup().
void loop()
{
switch(analogRead(A0))
{
case 256: Serial.println("Button One"); break;
case 512: Serial.println("Button Two"); break;
case 768: Serial.println("Button Three"); break;
case 1023: Serial.println("Button Four");
}
}
If the analog input fluctuate then you can try following codevoid loop()
{
if( analogRead(A0) >=240 && analogRead(A0) <=270 ) Serial.println("Button One");
if( analogRead(A0) >=490 && analogRead(A0) <=530 ) Serial.println("Button Two");
if( analogRead(A0) >=750 && analogRead(A0) <=790 ) Serial.println("Button Three");
if( analogRead(A0) >=1000 && analogRead(A0) <=1023 ) Serial.println("Button Four");
}
Tips
- You must read precautions :)
- Don’t add too many buttons (i.e too many resistors), as its program will be long enough to slow down Arduino. 30-40 is OK. For more try it yourself.
No comments