Simulating and Playing any tone for Buzzer/Speakers in Arduino Nano. Step by step tutorial on how to program this buzzers/speakers for tone.
utopian-io·@jbeguna04·
0.000 HBDSimulating and Playing any tone for Buzzer/Speakers in Arduino Nano. Step by step tutorial on how to program this buzzers/speakers for tone.
### <center>Buzzer/Speakers(Tone Application-Jingle Bell Tone)</center> <center> </center> #### What is a buzzer? A buzzer or a beeper is an audio signalling device, which may be mechanical, electromechanical, or piezoelectric. Typical uses of buzzers and beepers include alarm devices,timers and confirmation of user input such as a mouse click or keystroke. [Google Definition Credits](https://en.wikipedia.org/wiki/Buzzer) I'm using buzzer in this project because i'm testing and experimenting how buzzer is able to create tone and how it could create different variations of sound. Buzzers are really helpful for our daily uses such as for our securities for alarming devices and entertainment for it create music also. #### Pin Configurations <center> </center> #### What Will I Learn? Things that we could learn from it: - How to program an example tone for the buzzer from our Arduino Nano - How to simply connect the pin configurations from buzzer to Arduino Nano - How to simulate this buzzers by programming any tone #### Requirements/Materials needed Simple materials to follow this tutorial - Buzzers/Speakers - Arduino Nano - Braedboard - Connecting wires(if necessary) - Laptop/Desktop PC w/ Arduino IDE installed #### Difficulty - Basic #### Tutorial Contents ##### Let's Start #### Step 1: Physical connection of the buzzer to the Arduino Nano <center> </center> <center>*Created using Fritzing*</center> Connect the following: - Buzzer(Gnd) to Arduino Nano Gnd - Buzzer(DataIn) to Arduino Nano Digital Pin(3) - Arduino Nano to your laptop using usb cable #### Step 2: Software Connection from Arduino Nano to Buzzers - Open your Arduino IDE. Go to *__Tools__* and change your *__Board__* to Arduino Nano. Next, change your port where Arduino Nano is connected on your computer. Go to *__Port__* and select port for your Arduino Nano. <center> </center> - Paste the given code below on your Arduino IDE. ``` #define C 2100 #define D 1870 #define E 1670 #define f 1580 #define G 1400 #define R 0 int spkOut = 3; int DEBUG = 1; void setup() { pinMode(spkOut, OUTPUT); if (DEBUG) { Serial.begin(9600); } } int melody[] = {E, E, E,R, E, E, E,R, E, G, C, D, E, R, f, f, f,f, f, E, E,E, E, D ,D,E, D, R, G ,R, E, E, E,R, E, E, E,R, E, G, C, D, E, R, f, f, f,f, f, E, E, E, G,G, f, D, C,R }; int MAX_COUNT = sizeof(melody) / 2; long tempo = 10000; int pause = 1000; int rest_count = 100; int tone_ = 0; int beat = 0; long duration = 0; void playTone() { long elapsed_time = 0; if (tone_ > 0) { while (elapsed_time < duration) { digitalWrite(spkOut,HIGH); delayMicroseconds(tone_ / 2); digitalWrite(spkOut, LOW); delayMicroseconds(tone_ / 2); elapsed_time += (tone_); } } else { for (int j = 0; j < rest_count; j++) { delayMicroseconds(duration); } } } void loop() { for (int i=0; i<MAX_COUNT; i++) { tone_ = melody[i]; beat = 50; duration = beat * tempo; playTone(); delayMicroseconds(pause); } } ``` - When your done programming the Arduino Nano, just click on the Upload button to upload the code from Arduino IDE to your Arduino Nano. <center> </center> #### Step 3: Testing the buzzer if it really works. This is my sample output video on this tutorial. it really works for me. <center>https://www.youtube.com/watch?v=TPKoKkRpXVc&feature=youtu.be</center> #### Curriculum Other Arduino projects/contribution: - [1st Contribution](https://utopian.io/utopian-io/@jbeguna04/ardubblock-tool-for-easy-programming-in-arduino-ide-lighting-up-led-application) - [2nd Contribution](https://utopian.io/utopian-io/@jbeguna04/obstacle-sensing-with-ultrasonic-sensor-on-arduino) - [3rd Contribution](https://utopian.io/utopian-io/@jbeguna04/simulating-servo-motors-in-arduino-nano-step-by-step-tutorial-how-to-program-arduino-nano-for-servo-motor) - [4th Contribution](https://utopian.io/utopian-io/@jbeguna04/simulating-water-sensors-in-arduino-nano-step-by-step-tutorial-how-to-program-arduino-nano-for-water-sensors) # <center>That's All! Hope you learn something with this tutorial.</center> ## <center>Thank You!</center> <br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@jbeguna04/simulating-and-playing-any-tone-for-buzzer-speakers-in-arduino-nano-step-by-step-tutorial-on-how-to-program-this-buzzers">Utopian.io - Rewarding Open Source Contributors</a></em><hr/>