Site icon DIY Usthad

100+ Switches in a single pin of Arduino

multiple switches at a single pin of arduino by diyusthad

Introduction

Did you run out of input pins? Don’t worry, here is a solution without any shift registers. In this video, we are going to learn about connecting more than 100 switches to a single pin of Arduino.

Working Theory

Take a look at the circuit diagram first, otherwise, you won’t be able to understand what I’m saying. Whenever I press a switch the circuit will be completed through a different number of resistors,

  • In the circuit, if we press the 5th switch then the circuit is completing through all the 4 resistors,
  • If we press the 4th switch the circuit is completing through 3 resistors,
  • If we press the 3rd switch the circuit is completing through 2 resistors,
  • If we press the 2nd switch the circuit is completing through 1 resistor,
  • And if we press the 1st switch the circuit is completing without any resistors

That means the voltage reaching the analog pin A1 will be different for each switch, so we will use analogRead() function to read the values from the pin A1 and then we use an if else if condition to perform different operations for each switch.

ComponentsDIY UsthadAmazon.comBanggoodAliExpressUtsource
Arduino Nano
Push Button

 Let’s Build

Connect some LEDs

Let’s connect some LEDs to check the functioning of our circuit.

Coding

Take a look at the program. All the lines are commented properly.  


#define sw A1 //name for analog pin A1
#define led1 12
#define led2 11
#define led3 10
#define led4 9
#define led5 8

void setup() 
{
  Serial.begin(9600);
  pinMode(led1,OUTPUT); digitalWrite(led1,HIGH); //setting led1 pin HIGH ie LED will be off according to our circuit
  pinMode(led2, OUTPUT); digitalWrite(led2,HIGH); //setting led2 pin HIGH ie LED will be off according to our circuit
  pinMode(led3, OUTPUT); digitalWrite(led3,HIGH); //setting led3 pin HIGH ie LED will be off according to our circuit
  pinMode(led4, OUTPUT); digitalWrite(led4,HIGH); //setting led4 pin HIGH ie LED will be off according to our circuit
  pinMode(led5, OUTPUT); digitalWrite(led5,HIGH); //setting led5 pin HIGH ie LED will be off according to our circuit

}

void loop() 
{
  Serial.println(analogRead(sw)); // reading and printing the values from analog pin A1

  if(analogRead(sw) >197 && analogRead(sw) <207 )
  digitalWrite(led1,LOW);// LED will turn ON
  else if(analogRead(sw) >248 && analogRead(sw) <258)
  digitalWrite(led2,LOW);// LED2 will turn ON
  else if(analogRead(sw) >333 && analogRead(sw) <343)
  digitalWrite(led3,LOW);// LED3 will turn ON
  else if(analogRead(sw) >509 && analogRead(sw) <514)
  digitalWrite(led4,LOW);// LED2 will turn ON
  else if(analogRead(sw) >1015 && analogRead(sw) <1023)
  digitalWrite(led5,LOW);// LED2 will turn ON
  

}


Applications

Drawbacks

If you can think of another application post it in the comments. Thanks.

Video

To buy electronic components order from UTSOURCE

Exit mobile version