Interfacing of Switch and Arduino (Turn ON LED Using a Switch)

We all know switch is a input device and used; to make or break an electrical connection. Now let learn Interfacing of Switch and Arduino, Here we will use switch to take input signal to the microcontroller so that we will able to interface more input devices to the microcontroller, like sensors output as input to micro controller.

Required hardware or components for Interfacing of Switch with Arduino Uno

S.N.ComponentQuantity
1.Arduino Uno1
2.Breadboard1
3.LED1
4.Resistor 220 or 280 ohm1
5.Resistor 1K = 1000 ohm1
6.Switch / Button1

Circuit Diagram : Interfacing of Switch and Arduino

Interfacing of Switch as Input with Arduino
Interfacing of Switch as Input with Arduino

CONNECTION TABLE

S.N.ArduinoLED
1.13Anode (+)
2.GNDCathode (-)
S.N.ArduinoSwitchPull Down Resistor
1.GNDOne Terminal
2.2 (common to both)One TerminalOther terminal
3.+5VOther terminal

Arduino Programming Code : Interfacing of Switch and Arduino

int buttonState = 0;

void setup() {
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  buttonState = digitalRead(2);
  if (buttonState == HIGH) {
    digitalWrite(13, HIGH);
  } else if (buttonState == LOW) {
    digitalWrite(13, LOW);
  }
}

Arduino Programming Code Explanation

Define a global variable buttonState, we will use this to read state of pin number 2. State of digital pin can be either LOW=0 or HIGH=1 set default to LOW(0)

int buttonState = 0;

Here we assigned pin number 2 as input and 13 as ouput of arduino using pinMode(pinNumber, mode) function.

void setup() {
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);
}

Here we are use a new inbuilt function that is digitalRead(pinNumber), it is just opposite to digitalWrite where we set two states (HIGH or LOW) of pin.

The digitalRead( ) function will read the state of arduino pin, Here we read the state of pin number 2 of arduino. After Reading the state of Digital pin 2, we wil store the value to variable buttonState, So now buttonState value will be Either LOW=0 or HIGH=1

If button is press down, that means current from 5V power source can be flow to pin number 2 of arduino which makes pin 2 state HIGH.
Now at this time if we read the state of pin number 2 using digitalRead( ) function then the value of buttonState is HIGH (1).
Now we will check, using if function does buttonState is HIGH, if yes, then make Pin number 13 also HIGH this will turn ON the Led connected on pin 13.

void loop() {
  buttonState = digitalRead(2);
  if (buttonState == HIGH) {
    digitalWrite(13, HIGH);
  }

Else If button is not pressed, that means buttonState read from pin number 2 is LOW, if LOW then make Pin number 13 also LOW this will turn OFF the Led connected on pin 13.

 else if (buttonState == LOW) {
    digitalWrite(13, LOW);
  }
}

As you press the button you find LED to be glow and as you release you find it OFF.

Read Next: BLINK LED USING SWITCH WITH ARDUINO

Total Visits: 524
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Translate »
0
Would love your thoughts, please comment.x
()
x