Project tutorial

100+ Switches in a Single Pin of Arduino © GPL3+

Did you run out of input pins? Don't worry, here is a solution without any shift registers.

  • 14,194 views
  • 30 comments
  • 63 respects

Components and supplies

Apps and online services

About this project

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, other wise you won't be able to understand what I'm saying. Whenever I press a switch the circuit will be completing through 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 resistors,
  • 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 switches, so we will use analogRead() function to read the values from the pin A1 and then we use a if else if condition to perform different operations for each switches.

Let's Build

  • First connect five push switches to a breadboard.
  • Of course, you can connect maximum of 1023 switches theoretically to a 8-bit microcontroller like Arduino.
  • Then interconnect resistors in between the push switches as shown in the diagram.
  • Connect the other end of all the switches to 5v of Arduino, as you can see here I have connected the in such a way that one end is connected to the blue line of the beard board which is connected to the 5v.
  • Then from the end of the final switch connect a wire to analog pin A1 of Arduino.
  • Then connect a resistor across A1 and GND of Arduino, which is for pull down, That is to keep the value to zero when no switch is pressed.

Connect some LEDs

Lets connect some LEDs to check the functioning of our circuit.

  • Connect the LEDs as shown in the circuit,
  • Connect all the positive terminal all LEDs to the 5v.
  • Connect the negative terminal of each LEDs to the digital pin D12 to D8 of Arduino, respectively.
  • Practically we have to connect the LEDs through resistors for good life time.

Coding

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

Now let's upload the code and see it in action.

Video

Applications

  • Keypad
  • Full-sized keyboard for Arduino.
  • Custom mini keyboard for your Raspberry Pi tablet, etc..

Drawbacks

  • Multiple switch won't work in a single instant.

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

Thanks.

Read in my blog: https://www.diyusthad.com/2019/04/arduino-projects-single-pin-multiple-switches.html

Code

MultipleSw_SinglePin.inoC/C++
//www.diyusthad.com
//https://www.youtube.com/channel/UCjwtJsOrZyH7E6sBr_YJSeA
//Code by Najad

#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
  

}
Multiple Switches Single Pin

Schematics

circuit_bb_ViQSgzJlL3.jpg
Circuit bb viqsgzjll3
With LEDs
Circuit bb led vgvowmqcox

Comments

Similar projects you might like

Magnet Levitation with Arduino

Project tutorial by jsirgado

  • 45,486 views
  • 100 comments
  • 173 respects

Retro View Timer

Project tutorial by yilmazyurdakul

  • 975 views
  • 1 comment
  • 2 respects

Programmable Pocket Power Supply with OLED Display

Project tutorial by Shahariar

  • 6,798 views
  • 8 comments
  • 36 respects

2€ OLED I2C Analog Pin Dynamic Plotting

Project showcase by javier muñoz sáez

  • 7,134 views
  • 0 comments
  • 14 respects

Keyboard Melodies

Project tutorial by Ava Baker -21 and Natalie Clark -20

  • 2,248 views
  • 2 comments
  • 9 respects

Arduino External Mouse Buttons

by fmattiussi

  • 750 views
  • 4 comments
  • 3 respects
Add projectSign up / Login