Lesson 6

Adding Sound

There is a device connected to our Arduino that converts electric current into sound. It’s called a “buzzer.”

Many electric appliances such as washing machines, microwaves, and alarms use buzzers.

The buzzer is connected to pin 4 of the Arduino.

Let’s continue to test our components first and make sure the buzzer works. We also need to know how to program the buzzer before adding it to our racing game code.

Step 1: Startup sound

To test our buzzer, we will add a play block to our code that plays a short sound.

Add a play block right after the servo block, as shown in the video.
(Make sure Arduino is connected to your computer with the USB cable!)

 

This is the result you should get:

The play block has 3 parameters:
1) Pin (in our case pin is 4)
2) Note (choose a musical note)
3) Beats (how long the note will be played)

Step 2: Adding melody

Now, let’s transform our simple sound into a short melody.
The melody will consist of three notes with a 0.25-second delay between each note.

Change the code as shown in the video below:

This is the result you should get:

Step 3: Countdown

We will create a countdown effect by adding sounds while the traffic light changes from red to green.

Change your code according to the image below. (you can also download the final code of the lesson at the end of this page instead of making the changes yourself)

 

If you customized the notes, your code will look a little different. You should get a similar result:

Let’s continue!

Our race code is improved, and we now have a working traffic light and sound!

Now, it’s time to start using the LCD screen in the next lesson.