Introduction
In this article, I'm going to explain about making sound in the Arduino, using the push button.
Parts Of Lists
- Arduino Uno
- Push Button
- Buzzer
- Bread Board
- Hookup Wire.
Connection
- Push Button To Board:
- Connect the 1st pin of the VCC of 5v to the Arduino Board
- Connect the 2nd pin to the Gnd and any digital pin of the input side to the Board.
Buzzer to Board
- Connect one side of the wire to the VCC
- Connect the another side of the wire to the Gnd.
Programming
- #include "pitches.h"
- const int buttonPin = 2;
- int note1 = NOTE_C4;
-
- int buttonState = 0;
- void setup() {
-
- pinMode(buttonPin, INPUT);
- }
- void loop() {
-
- buttonState = digitalRead(buttonPin);
-
-
- if (buttonState == HIGH) {
-
- tone(8, note1);
- } else {
-
- noTone(8);
- }
- }
That's it. Now, when the button is pressed, it will automatically produce the sound.