Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Read Switch With Arduino

Read Switch With Arduino

 


Fig: Switch

In this tutorial i will take you through the process of interfacing Arduino with a switch. If you haven't gone through my previous post on blinking LEDs with Arduino click here .

A switch is an electrical component used to disconnect or connect the conducting path in an electrical circuit. Electronic devices such as transistors, MOSFETs, and relays can be acted as switches and they fall under the category of electrical/electronic switches.


There are two ways of connecting a switch to a PIC MCU:

  • Pull-Up method
  • Pull-Down method
When using the Pull-Up method, the I/O pin reads a Logic One (i.e. a digital HIGH) when the switch is open but reads a Logic Zero (i.e. a digital LOW) when the switch is closed. In this method, the power supply is connected directly to a pull-up resistor.



Fig: Pull -Up Method



When using the Pull-Down method, the I/O pin reads a Logic Zero (i.e. a digital LOW) when the switch is open but reads a Logic One  (i.e. a digital HIGH) when the switch is closed.




Fig: Pull -Down Method



Switch Debounce:

Whenever Switches are pressed or when two metals strike each other, Spikes are usually generated. The Arduino  can misread these spikes as different or multiple switch press whereas the switch was pressed just once. To avoid this error ,there is need for a filtering mechanism which can be achieved with hardware (a filtering circuit) or software. In this tutorial we would use the software approach and what we are going to do is to initiate a small delay and check again for the status of the switch. if the microcontroller reads a Logic Zero ( Pull-up method) then we can be certain that the switch was pressed and vice-versa.

In this Tutorial we would be using the Pull-Up method. Our aim is to turn ON or turn OFF an LED whenever the switch is closed or opened . our switch is connected to I/O PIN13 of the Arduino UNO and note that Pin13 must then be configured as an input pin in order to read the status of the switch. The LED is connected to I/O pin4 of the Arduino UNO which is configured as an output pin.


Materials Required:

  • Arduino UNO
  • LED
  • 330R Resistor
  • Switch
  • 10K Resistor


CODE:


*
    Name     : Read Switch with Arduino UNO
    Author   : Daniel Oluwole
                       (C) 2020 Danitronics
                       danieloluwole51@gmail.com, danieloluwole51@yahoo.com
    Mobile:    +234 8188508765.
   
    Notes    :   Our aim is to turn ON or turn OFF an LED whenever the switch is closed or opened
 
*/
// Variables to set switch and LED pins
int SWITCH_PIN = 13;  
int LED_PIN = 4;
int SWITCH_STATE = 0;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
   pinMode(SWITCH_PIN,INPUT);   //switch pin set as input
  pinMode(LED_PIN, OUTPUT);      // LED pin set as output
}

// the loop function runs over and over again forever
void loop() {
SWITCH_STATE = digitalRead(SWITCH_PIN);  //check if switch is pressed
  if(SWITCH_STATE == LOW)   
     {
       delay(100);                       //  switch debounce deelay
       if(SWITCH_STATE == LOW)    //if switch is pressed
          {
            digitalWrite(LED_PIN,HIGH);  //LED  ON
          }
     }else
             {
               digitalWrite(LED_PIN,LOW); //LED OFF
             }
                        
}
Next Post:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement