Hey guys! Want to learn how to read input into arduino and use it to control the output? Well here you've got the perfect tutorial on how to do so! In this instructable, I will guide you through the simple steps needed to turn a buzzer and led on when a button is clicked an turn it back off when it is clicked again.
Step 1: Step 1: Downloading the Arduino IDE
Download and Install the Arduino IDE (Interactive Development Environment) using the link below:
Choose and save the version that best suits your operating system and configuration.
Step 2: Step 2: Hardware Materials
- 1 Arduino board
- 1 breadboard
- 1 LED
- 1 piezo speaker
- 1 button
- Jumper wires
Step 3: Step 3: Building the Hardware
1) Add the LED to the breadboard. Connect the short leg of the LED to Ground, long leg of LED to pin 7 with jumper wires and resistors shown in the diagram.
2) Add the piezo speaker to the breadboard. Connect the short leg of the piezo speaker to Ground, long leg to pin 9 with jumper wires and resistors shown in the diagram.
3) Add a button which will act as an input
4) Above picture shows how the connections should look like.
Step 4: Step 4: Downloading and Running the Program
Download the attached arduino program to your laptop. Connect the arduino to your laptop, and run the program. You should have a buzzer and LED that goes on/off when the button is clicked.
Step 5: Step 5: Understanding the Program
LED has two methods
- digitalWrite(ledPinNumber, HIGH) which sends a HIGH signal to LED making it glow.
- digitalWrite(ledPinNumber, LOW) which sends a LOW signal to LED turning it off.
Piezo has two methods
- tone(piezoPinNumber, delay) which makes piezo to buzz
- noTone(piezoPinNumber) which makes piezo to switch off
In setup method, declare pin 4 as Input and intializes buttonOn as false. pin 9 is declared as output. In loop method, the current value of button input is read. if it is pressed, it toggles the button from off to on. Next time the button is pressed it toggles from on to off. Delays are added to reduce noise and avoid changing the output too quickly.
When buttonstate is On, the tone method is called causing the piezo speaker to buzz. When buttonState is Off, the noTone method is called causing the piezo speaker to turn off.
Similarly when buttonstate is On, HIGH is sent to LEDPin causing the LED to glow. When buttonState is Off, LOW is sent to LEDPin causing the LED to turn off.