Questions
























Write a program in to interface on LED, buzzer and button with Arduino: modify the program to make the buzzer to go on for 1 minute and led glow for 45 seconds, whenever the button is pressed.

Last Updated : 04 Dec 2024 Jan 22, Jul 23, Jan 24, Jul 24

Solution:

int btnstate = 0;

void setup() {
  pinMode(2, INPUT);    // Set pin 2 as input for the button
  pinMode(7, OUTPUT);   // Set pin 7 as output for the LED
  pinMode(8, OUTPUT);   // Set pin 8 as output for the buzzer
}

void loop() {
  btnstate = digitalRead(2);  // Read the button state
  
  if (btnstate == HIGH) {
    digitalWrite(8, HIGH);    // Turn on the buzzer
    delay(60000);             // Wait for 1 minute
    
    digitalWrite(7, HIGH);    // Turn on the LED
    delay(45000);             // Wait for 45 seconds
  } else {
    digitalWrite(8, LOW);     // Turn off the buzzer
    digitalWrite(7, LOW);     // Turn off the LED
  }
}

 

Output Description:

Button Pressed:
        Buzzer: Sounds for 60 seconds.
        LED: Lights up for 45 seconds, starting after the buzzer has been on for 1 minute.
Button Not Pressed:
        Both the buzzer and LED are off.

 

Code Explanation:

Variables:

  • int btnstate = 0;: Stores the button's state.

setup() Function:

  • pinMode(2, INPUT);: Sets pin 2 as input (button).
  • pinMode(7, OUTPUT);: Sets pin 7 as output (LED).
  • pinMode(8, OUTPUT);: Sets pin 8 as output (buzzer).

loop() Function:

  • btnstate = digitalRead(2);: Reads button state.
  • If pressed (HIGH):
    • digitalWrite(8, HIGH);: Turns on the buzzer.
    • delay(60000);: Buzzer on for 1 minute.
    • digitalWrite(7, HIGH);: Turns on the LED.
    • delay(45000);: LED on for 45 seconds.
  • If not pressed:
    • digitalWrite(8, LOW);: Turns off the buzzer.
    • digitalWrite(7, LOW);: Turns off the LED.

Meanwhile you can watch this video

Watch Video
Latest Updates