Q.1 - Write a program to interface Button, buzzer and LED, whenever the button is pressed the buzzer gives beep for 350ms and LED status is toggled
ANSWER
Complete Arduino Solution:
// Simple Button, Buzzer, and LED Interface
const int buttonPin = 2; // Button pin
const int buzzerPin = 3; // Buzzer pin
const int ledPin = 4; // LED pin
int ledState = LOW; // LED state
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Button setup
pinMode(buzzerPin, OUTPUT); // Buzzer setup
pinMode(ledPin, OUTPUT); // LED setup
}
void loop() {
// Check if button is pressed
if (digitalRead(buttonPin) == LOW) {
// Toggle LED
ledState = !ledState;
digitalWrite(ledPin, ledState);
// Turn on buzzer for 350ms
digitalWrite(buzzerPin, HIGH);
delay(350);
digitalWrite(buzzerPin, LOW);
// Wait a bit to avoid multiple triggers
delay(200);
}
}
Simple Program Explanation:
- Setup Pins: Button (pin 2), Buzzer (pin 3), LED (pin 4)
- Button Check: Check if button is pressed (LOW state)
- Toggle LED: Switch LED between ON and OFF
- Buzzer Beep: Turn on buzzer for 350ms then turn off
- Small Delay: Wait 200ms to prevent multiple triggers
Simple Connections:
• Button: Connect to pin 2 and GND
• Buzzer: Connect to pin 3 and GND
• LED: Connect to pin 4 and GND (with 220Ω resistor)
• Button: Connect to pin 2 and GND
• Buzzer: Connect to pin 3 and GND
• LED: Connect to pin 4 and GND (with 220Ω resistor)
How It Works:
• Press button → LED toggles + Buzzer beeps for 350ms
• Simple and direct approach
• Easy to understand and modify
• Press button → LED toggles + Buzzer beeps for 350ms
• Simple and direct approach
• Easy to understand and modify