Blink Led by Switch using function in Arduino

We read in previous topics how to make blinking LEDs and control output using input (by switch). In this topic, we will combine both the logic’s and make the led blinking on button pressed / Blink Led by Switch using function in Arduino.

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

Blink Led by Switch using function in Arduino
Blink Led by Switch using function in 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 : BLINK LED BY SWITCH USING FUNCTION IN ARDUINO

int buttonState = 0;

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

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

void blink_led() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

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 using 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 the arduino pin, Here we read the state of pin number 2 of the arduino. After Reading the state of Digital pin 2, we will store the value to variable buttonState, So now buttonState value will be Either LOW=0 or HIGH=1.

If a button is pressed, that means current from the 5V power source can 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 call a function we define (user defined) blink_led( ) which is programmed to blink a led. If we left the button pressed then it will continuously blink the LED

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

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. Or if you want LED to remain ON while button not pressed you can just replace the digitalWrite(13, LOW) by digitalWrite(13, HIGH);

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

READ NEXT
WORKING AND INTERFACING OF RELAY WITH ARDUINO


 

5 1 vote
Article Rating
Subscribe
Notify of
guest
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
pert
pert
4 years ago

I tried many ways but never succeeded,after i read this,I feel confident.Thanks for walking us through it.home automation india

Last edited 4 years ago by pert
MUZAMMIL
MUZAMMIL
2 years ago

KY HM BINA IF ELSE CONDITION KE KR SKTE H

Nagavardhan
Nagavardhan
2 years ago

design a implement basic logic gates operation by using switch and leds

5
0
Would love your thoughts, please comment.x
()
x