Bird
0
0
Arduinoprogramming~20 mins

tone() function for frequency generation in Arduino - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the tone() Function for Frequency Generation on Arduino
📖 Scenario: You want to create simple sounds using a buzzer connected to an Arduino board. The buzzer will play different tones based on frequencies you set.
🎯 Goal: Build a program that uses the tone() function to generate sounds of specific frequencies on a buzzer connected to pin 8.
📋 What You'll Learn
Create a variable for the buzzer pin number
Create a variable for the frequency in Hertz
Use the tone() function to play the frequency on the buzzer pin
Stop the tone after a short delay
Print a message to the Serial Monitor showing the frequency played
💡 Why This Matters
🌍 Real World
Generating tones is useful for alarms, notifications, and simple music on embedded devices.
💼 Career
Understanding how to control hardware like buzzers with Arduino is important for electronics and embedded systems jobs.
Progress0 / 4 steps
1
Set up the buzzer pin
Create an integer variable called buzzerPin and set it to 8.
Arduino
Hint

Use int buzzerPin = 8; to store the pin number.

2
Set the frequency value
Create an integer variable called frequency and set it to 440 (which is the musical note A).
Arduino
Hint

Use int frequency = 440; to set the frequency in Hertz.

3
Play the tone using tone()
In the setup() function, use tone(buzzerPin, frequency); to start playing the tone. Then add delay(1000); to play it for 1 second, and finally use noTone(buzzerPin); to stop the sound.
Arduino
Hint

Use tone() to start the sound, delay() to wait, and noTone() to stop.

4
Print the frequency to Serial Monitor
In the setup() function, before playing the tone, add Serial.begin(9600); to start serial communication. Then add Serial.print("Playing frequency: "); and Serial.println(frequency); to show the frequency value.
Arduino
Hint

Use Serial.begin(9600); to start, then Serial.print() and Serial.println() to show the frequency.