This tutorial demonstrated to you how to interface a buzzer switch with Arduino nano. I will do a quick overview on how buzzer works and show a project example using Arduino nano
Description of Buzzer
The buzzer is a sound-making device commonly used in alarms, alert devices, etc. Low prices and cheaply available these devices are used in cars/Truck as reverse indicators.
Parts Required
- Arduino Nano
- 12mm Active buzzer
- Breadboard
- Jumber wire
- Button switch
- Resistor 10k
Here we are using an Active buzzer for 12 mm, It smaller buzzer available, which commonly available in the market to connect with Arduino nano. The busser has two legs Longer legs are positive and shorted legs are negative. It is a simple buzzer that makes a continuous beep sound when we connect it with power
This buzzer can be used by simply powering it using a DC power supply ranging from 4v to 9v.
Schematic Diagram
Code
int buttonpin= 9; // declare the variable for the store button pin int buzzerpin= 8; // declare variable for the store buzzer pin int pinstatus; // declare variable to store the pin status void setup() { pinMode(buttonpin, INPUT); pinMode(buzzerpin,OUTPUT); } void loop() { pinstatus=digitalRead(buttonpin); // read the status of digital pin and stores the value of pin status if (pinstatus==HIGH) // condition statement which checks the pin status, pinstatus high means { // when we press the button switch values in the pin status becomes HIGH digitalWrite(buzzerpin,HIGH); // turn on the buzzer } else { digitalWrite(buzzerpin,LOW); // turn off the buzzer } }
How Code Works
First, we need to create 3 variables, buttonpin to store the pin number of button switch which connected to 9, buzzerpin stores the buzzer connected to pin number 8, and pinstatus stores the status of the switch
int button pin= 9; // declare the variable for the store button pin int buzzerpin= 8; // declare variable for the store buzzer pin int pinstatus; // declare variable to store the pin status
Inside the setup function we declare the buttonpin as input mode and buzzer as output mode
void setup() { pinMode(buttonpin, INPUT); pinMode(buzzerpin,OUTPUT); }
Next is inside the loop function, which included two parts first it reads the status of the switch and stores values inside the pin status variable. When we press the switch pin status to become HIGH values 1 is stores in it, when we release the press-button switch status moves to LOW
pinstatus=digitalRead(buttonpin); // read the status of digital pin and stores the value of pin status
The next step of code is the condition statement which checks the status of the push-button and produces the beep sound from the buzzer.
Next is the condition statement, We are using if conditions stated here. In if condition first it checks the status of the switch if pin status is high makes buzzer switch ON by performing digitalWrite function and if it’s not high makes the buzzer pin OFF
if (pinstatus==HIGH) // condition statement which checks the pin status, pinstatus high means { // when we press the button switch values in the pin status becomes HIGH digitalWrite(buzzerpin,HIGH); // turn on the buzzer } else { digitalWrite(buzzerpin,LOW); // turn off the buzzer }