Blogroll

Powered by Blogger.

Sunday 6 December 2015

Interface 16 switches to arduino uno using only 5 digital pins (HEF4067BP)

by realfinetime  |  in Microcontroller at  20:11

Under certain situations, we will have to interface many switches to our arduino uno. Simplest method to interface switches to arduino uno is to connect the switch directly to any one of the digital pin or analog pin of arduino uno. But, if we have to interface many switches to arduino uno, previous mentioned method is not advisable because, only 14 digital pins and 6 analog pins are available in arduino uno. If we connect switches to all these pins, we could not use this arduino uno board for any other purposes. Then, we have to go for some other methods which use minimum pins of arduino to interface maximum number of switches.

Circuit given below shows a simple method to interface 16 switches to arduino uno using only 5 digital pins. A HEF4067BP multiplexer/demultiplexer IC should be connected in between switches and arduino uno as shown in the diagram.

After completing the circuit, upload the following program to your arduino board.

int val = 0;         // variable to store the read value

int A_zero = 8;       
int A_one = 9;
int A_two = 10;
int A_three = 11;

int z = 12;

void setup()
{
  
  pinMode(z, INPUT);      // sets the digital pin "z" as input

  pinMode(A_zero, OUTPUT);      // sets the digital pin "A_zero" as output
  pinMode(A_one, OUTPUT);       // sets the digital pin "A_one" as output
  pinMode(A_two, OUTPUT);       // sets the digital pin "A_two" as output
  pinMode(A_three, OUTPUT);     // sets the digital pin "A_three" as output
  
  Serial.begin(9600);

}

void loop()
{
  // Select address 0000  
  digitalWrite(A_zero, LOW);    
  digitalWrite(A_one, LOW);     
  digitalWrite(A_two, LOW);     
  digitalWrite(A_three, LOW);   
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 1");  
    delay(50);
  }

  // Select address 0001  
  digitalWrite(A_zero, HIGH);   
  digitalWrite(A_one, LOW);     
  digitalWrite(A_two, LOW);     
  digitalWrite(A_three, LOW);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 2");  
    delay(50);
  }

  // Select address 0010  
  digitalWrite(A_zero, LOW);   
  digitalWrite(A_one, HIGH);     
  digitalWrite(A_two, LOW);     
  digitalWrite(A_three, LOW);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 3");
    delay(50);  
  }

  // Select address 0011  
  digitalWrite(A_zero, HIGH);   
  digitalWrite(A_one, HIGH);     
  digitalWrite(A_two, LOW);     
  digitalWrite(A_three, LOW);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 4");
    delay(50);  
  }

  // Select address 0100  
  digitalWrite(A_zero, LOW);   
  digitalWrite(A_one, LOW);     
  digitalWrite(A_two, HIGH);     
  digitalWrite(A_three, LOW);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 5");
    delay(50);  
  }  

  // Select address 0101  
  digitalWrite(A_zero, HIGH);   
  digitalWrite(A_one, LOW);     
  digitalWrite(A_two, HIGH);     
  digitalWrite(A_three, LOW);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 6");
    delay(50);  
  }  

  // Select address 0110  
  digitalWrite(A_zero, LOW);   
  digitalWrite(A_one, HIGH);     
  digitalWrite(A_two, HIGH);     
  digitalWrite(A_three, LOW);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 7");
    delay(50);  
  }  

  // Select address 0111  
  digitalWrite(A_zero, HIGH);   
  digitalWrite(A_one, HIGH);     
  digitalWrite(A_two, HIGH);     
  digitalWrite(A_three, LOW);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 8");
    delay(50);  
  }  

  // Select address 1000  
  digitalWrite(A_zero, LOW);   
  digitalWrite(A_one, LOW);     
  digitalWrite(A_two, LOW);     
  digitalWrite(A_three, HIGH);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 9");
    delay(50);  
  }  

  // Select address 1001  
  digitalWrite(A_zero, HIGH);   
  digitalWrite(A_one, LOW);     
  digitalWrite(A_two, LOW);     
  digitalWrite(A_three, HIGH);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 10");
    delay(50);  
  }

  // Select address 1010  
  digitalWrite(A_zero, LOW);   
  digitalWrite(A_one, HIGH);     
  digitalWrite(A_two, LOW);     
  digitalWrite(A_three, HIGH);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 11");
    delay(50);  
  }

  // Select address 1011  
  digitalWrite(A_zero, HIGH);   
  digitalWrite(A_one, HIGH);     
  digitalWrite(A_two, LOW);     
  digitalWrite(A_three, HIGH);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 12");
    delay(50);  
  }

  // Select address 1100  
  digitalWrite(A_zero, LOW);   
  digitalWrite(A_one, LOW);     
  digitalWrite(A_two, HIGH);     
  digitalWrite(A_three, HIGH);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 13");
    delay(50);  
  }

  // Select address 1101  
  digitalWrite(A_zero, HIGH);   
  digitalWrite(A_one, LOW);     
  digitalWrite(A_two, HIGH);     
  digitalWrite(A_three, HIGH);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 14");
    delay(50);  
  }

  // Select address 1110  
  digitalWrite(A_zero, LOW);   
  digitalWrite(A_one, HIGH);     
  digitalWrite(A_two, HIGH);     
  digitalWrite(A_three, HIGH);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 15");
    delay(50);  
  }

  // Select address 1111  
  digitalWrite(A_zero, HIGH);   
  digitalWrite(A_one, HIGH);     
  digitalWrite(A_two, HIGH);     
  digitalWrite(A_three, HIGH);  
  if(digitalRead(z)==1)
  {         
    Serial.println("Switch pressed is 16");
    delay(50);  
  }
}
If uploading is successful, open your serial monitor. Serial monitor will print the number of the switch pressed as shown in the following image.
Watch the video demonstration here.

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I've been looking for this pattern for a long time, thanks

    ReplyDelete
  3. Hey fellow learners! I stumbled upon an incredible resource that perfectly complements our discussion on interfacing switches with Arduino Uno. If you're looking to enhance your project with captivating binary code visuals, check out this collection on Depositphotos: binary code images. Visualizing binary patterns can add an extra layer of understanding to your projects, especially when working with switches. These images could be a great addition to your documentation or presentations. Take a look and let me know what you think!

    ReplyDelete

IMPORTANT NOTICE

All the circuits, published in this blog is only after testing and getting proper results in my private lab. When you try these circuits, you should check the supply voltage, polarity of components, presence of childrens nearby and shorts in the circuits. This website will not be responsible for any harm happened to you or your components caused by your carelessness.

For More Electronic Tips



Proudly Powered by Blogger.