How to use push-button as on-off switch?
How to use push-button as on-off switch in Arduino?
The below Arduino code uses a push-button as an on-off switch
Wiring Diagram for the above code
Image is developed using Fritzing. Click to enlarge image
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- On Arduino IDE, Go to Tools Manage Libraries
- Search “ezButton”, then find the button library by ArduinoGetStarted
- Click Install button to install ezButton library.
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Press the button several time
- See the switch's state on Serial Monitor
※ NOTE THAT:
The above code debounces for the button. See why do we need to debounce for button
You can also refer to the following tutorials:
|1 × Arduino UNO Buy on Amazon|
|1 × USB 2.0 cable type A/B Buy on Amazon|
|1 × Jumper Wires Buy on Amazon|