Arduino Lesson 7
Generating Tones
Generate tones from the buzzer
Components Required
Buy
Introduction
In this example, you are learning to generate a tone using the onboard buzzer on the Magicbit.
Learning Outcomes
From this example, you’ll get an understanding about,
- Tone Function
Theory
Piezo buzzers are commonly used in embedded systems to give audible tones. Combined with ESPservo library, Magicbit can generate various tones.
Methodology
For this example, we use the piezo buzzer wired to pin 25 of the Magicbit.
ESP32Servo.h library is used to generate PWM signals needed to generate tones. We could specify the frequency & duration of the tone.
.
Code
#include <ESP32Servo.h>
void setup(){
pinMode(25,OUTPUT);
}
void loop(){
tone( 25 ,4186,500); //C Note
delay(1000);
tone( 25,5274,500); //E Note
delay(1000);
}
Explanation
tone(pin No, frequency, duration): generates PWM to corresponding to the given parameters
Activity
Note:: Create a program that plays one frequency when one push button on the board is pressed, and another frequency when the other push-button when pressed.
Anupama Nethmal
I want follow this course