ARDUINO > REDUCING THE INPUTS. PART. 1
REDUCING THE INPUTS. PART. 1 PDF Print E-mail
(0 votes, average: 0 out of 5)

10 SWITCHES WITH 7 PINS.

Arduino logoWhen using the small Arduino Duamilanove or Uno and want to connect 10 buttons to digital inputs but you also need pins to drive leds, motors, sirens etc. you usually may face the problem that you may run out of pins. The method I will show you reduces the number of pins needed for iput switches - buttons. It works for 6 or more input pins and for even number of digital inputs. As an example I will show how to use 7 pins for reading 10 input buttons thus freeing up 3 I/O pins!

The basic idea is the following. We split the 10 input buttons into 2 groups of 5 buttons each and connect them to the same 5 I/O pins. Then we use 2 I/O pins as power supply to each group. The two power supply pins are activated alternatively. Each time a supply pin is activated we read the status of pins connected to the buttons and because we know what supply pin is activated we also know to which group the button belongs to.
In the following example supply pin A0 is connected to Switches (GA1 -GA 5) (group A) and supply pin A1 is connected to switches (GB1 - GB5) (group B).
Input pins are pin 2 to 6 and are set to INPUT and LOW. Power pins are A0 and A1 and are set as OUTPUT pins. Power pins are set HIGH in an alternate manner. When A0 is HIGH, A1 must be LOW and "vice versa". Each time one of supply pins is set to HIGH one of the groups of Switches is activated and ready to be read. So when A0 is HIGH you can read status of switches GA1- GA5 and when A1 is HIGH you can read GB1 - GB5. Although the switches from both groups are connected to the same pins, due to the fact that are separately activated and with the help of protective diodes at the supply pins, there is no way two switches from different groups are read HIGH at the same time. The resistors are the typical resistors used when connecting a switch between Vcc and one Arduino I/O pin.

To understand look at the schematic bellow.
schematic_2pins_4switches

GA1 and GB1 are connected both to pin2. GA2 and GB2 are connected both to pin PIN3 and etc. When A0 is HIGH A1 is LOW, thus reading pins 2 to 6 we read the status of switches of group A (GA1 to GA5). Switches of group B (GB1 to GB5) are ignored. Protective diodes D1 and D2 protect the Arduino from reverse currents that can burn the MC and also take care that every time only one group is read.
In the next image created on "Fritzing" all 10 switches are connected to the Arduino.

fritzing 10buttons 7 I/O pins

And here is a simple code that supposes that each group has 2 buttons.

const int in1 = 2;   // GA1 and GB1 are connected here.
const int in2 = 3;  // GA2 and GB2 are connected here.
const int P1 =  A0;   // the number of power1 pin. Group A supply
const int P2 =  A1;   // the number of power2 pin. Group B supply
const int led =  13;  // led pin

void setup() {
Serial.begin(9600);

// initialize outputs:
pinMode(P1, OUTPUT);     
pinMode(P2, OUTPUT);
pinMode(led, OUTPUT);  
digitalWrite(P1, LOW);
digitalWrite(P2, LOW);
digitalWrite(led, LOW);

// initialize inputs:

pinMode(in1, INPUT);  
pinMode(in2, INPUT);

}


// START PRESSING BUTTONS
void loop() {

Serial.println("Starting test P1 in ON");

digitalWrite(P1, HIGH);
digitalWrite(P2, LOW);
delay(500);
Serial.print("button A1 status:  ");
Serial.println(digitalRead(in1));
if (digitalRead(in1) == HIGH ) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
Serial.print("button A2 status:  ");
Serial.println(digitalRead(in2));
if (digitalRead(in2) == HIGH ) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
delay(500);

Serial.println("Starting test P2 in ON");

digitalWrite(P1, LOW);
digitalWrite(P2, HIGH);
delay(500);
Serial.print("button B1 status:  ");
Serial.println(digitalRead(in1));
if (digitalRead(in1) == HIGH ) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
Serial.print("button B2 status:  ");
Serial.println(digitalRead(in2));
if (digitalRead(in2) == HIGH ) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
delay(500);
}

 

Disclamer: Circuits and Code are provided as is. You can study, use them as you will .
 

Add your comment

Your name:
Subject:
Comment:

Anti-spam: complete the taskJoomla CAPTCHA
What do you want from my site?