Reader small image

You're reading from  Mastering Arduino

Product typeBook
Published inSep 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788830584
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Jon Hoffman
Jon Hoffman
author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman

Right arrow

Fun with Sound

Adding sound to your robotic project can be the difference between a good robot and an awesome robot. Just think about how cute R2-D2, from the movie Star Wars, would have been if he did not make any sound. We can use sound for more than just robots. For example, we may want to add a loud alarm if a motion sensor detects motion or maybe we just want to play a melody when the temperature is just right outside.

In this chapter, you will learn:

  • How to connect a piezo buzzer to the Arduino
  • How to connect a speaker to the Arduino
  • How to use the tone() function to generate a sound
  • How to play music with the Arduino

Introduction

In this chapter, we will be doing several projects that can use either a piezo buzzer or a small 8-ohm speaker. By using both the buzzer and the speaker, you will be able to hear the difference between the two to help determine which is right for our project.

A piezo buzzer is compact, reliable and very inexpensive. They are easier to mount and use than a normal speaker in most electronic projects. These buzzers can emit a wide range of sounds, from soft hums to loud alerts.

A piezo buzzer, sometimes known as a piezo speaker, creates sounds a little differently than a normal speaker. The working component of these buzzers is a thin disc of piezoelectric material usually bonded to a metal diaphragm. As the voltage is applied to the piezoelectric material, it deforms. This causes the metal diaphragm to bend forward or backward. This deformation happens very rapidly...

Components needed

We will need the following components for this chapter's project:

  • One Arduino Uno or compatible board
  • One piezo buzzer
  • One 8 ohm speaker
  • Jumper wires
  • One breadboard

Circuit diagrams

Here is the circuit diagram that we will use for all code samples in this chapter:

This diagram shows that the ground pins on both the speaker and piezo buzzer are connected to the ground rail on the breadboard. The power pin on the piezo buzzer is connected to pin 8 on the Arduino, and the power wire on the speaker is connected to pin 7 on the Arduino.

Code

Let's start off by using the tone() function.

Using the tone function

For the first few examples in this chapter, we will be using the Arduino tone() function. This function comes in two varieties. The first variety takes two arguments, where the first is the pin number that the buzzer or speaker is connected to and the second is the frequency in hertz to play the sound at. The function looks like this:

tone(pinNumber, frequency);

When this function is used with only two parameters, the sound is played indefinitely. The following code shows how we could use this function to play a note using the previous circuit diagram:

#define PIEZOPIN 7
#define SPEAKERPIN 8

int soundPin = PIEZOPIN;

void setup() {
  tone...

Challenge

For the challenge, we will stay with the Star Wars theme. Let's say that we wanted to build a robot that looks like R2-D2 from Star Wars. One of the features that we would put in would be to have it sound like R2-D2. How would you make the robot sound like R2-D2?

Summary

In this chapter, we saw how to connect both a speaker and a piezo buzzer to an Arduino. We then learned how to use the tone() function to create sounds and also play a song. We also saw how we could install and use a third-party library so we could play RTTTL files.

In the next chapter, we will look at how we can use LCD displays to display messages.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Arduino
Published in: Sep 2018Publisher: PacktISBN-13: 9781788830584
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman