In this lesson, we’ll introduce a new component: the buzzer! It’s like a tiny speaker that can make different sounds. Get ready to make your Arduino sing a simple tune!
Objective: Learn how to use a buzzer to create sound.
Materials:
- Arduino Uno board
- Breadboard
- Buzzer
- Jumper wires
- Computer with Arduino IDE software installed
Step 1: Connecting the Buzzer
- Find the two legs of the buzzer: One is positive (+) and the other is negative (-).
- Connect the positive leg of the buzzer to a digital output pin on the Arduino: We’ll use pin 8 in this lesson.
- Connect the negative leg of the buzzer to a ground rail on the breadboard: This completes the buzzer’s circuit.
Step 2: Programming the Arduino
- Open the Arduino IDE.
- Copy and paste the following code:
const int buzzerPin = 8; // Buzzer connected to pin 8
void setup() {
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
}
void loop() {
tone(buzzerPin, 440); // Play note A (440 Hz) for 1 second
delay(1000);
noTone(buzzerPin); // Stop playing
delay(1000);
}
- Upload the code to the Arduino board.
Step 3: Listen to the Buzzer
- You should hear the buzzer playing a simple tone (note A) for one second, then stopping for one second, and repeating.
Congratulations! You’ve made your Arduino sing!
Let’s Experiment:
- Change the frequency: Try changing the number in the tone(buzzerPin, 440); line to create different sounds. For example, try tone(buzzerPin, 880); for a higher note.
- Play a melody: Try creating a short melody by using multiple tone() commands with different frequencies and delays.
Keep experimenting with sound and have fun with Arduino!