Arduino Learning Studio full lab
0/13 lessons marked complete
● Saved in this browser← Back to homepage

Lesson 8 of 13

Make a sound

Objective: Play notes and alert patterns with tone() and noTone(): hear why pitch is a frequency, why tone() does not wait, and which buzzer to use.

MODEL OF THIS SKETCHSTOPPED
ARDUINO UNOATmega328PTimer2D8GND+−passive buzzer

The sound is your browser's approximation of the buzzer (a square wave through your speakers), not the buzzer itself. It plays only while the sketch runs.

While the sketch runs, a zoomed-in view shows D8's square wave, and a timeline shows which notes played over the last 3 seconds.

Stage 1 of 7 · What am I building?

Explore

A robot warns people with sound: a reversing beeper, an obstacle alert. Here the board makes a note on D8 and a buzzer turns it into sound. Run it, then select each part on the circuit, or below.

Pick a part. 0/4 inspected.

Before you start (optional refreshers)

Digital HIGH and LOW

A digital pin is either HIGH (about 5 V on an Uno) or LOW (0 V). digitalWrite() sets an output; digitalRead() reports an input.

Timing and delay()

delay(ms) pauses the whole program. Nothing else happens while it waits, so a blink lasts ON delay + OFF delay.

New ideas in this lesson

Frequency and sound

Frequency is cycles per second, in hertz (Hz): one cycle lasts 1000 ÷ frequency ms. tone() makes a square wave at that frequency, HIGH for half of each cycle, and a passive buzzer turns it into a pitch: higher frequency, higher pitch. tone() returns at once, plays one tone at a time and stops with noTone().

Build it for real · Arduino Uno R3

You need

  • Arduino Uno R3 (or compatible) and its USB cable
  • Breadboard and jumper wires
  • Passive piezo buzzer

Wiring

  1. Buzzer + → D8.
  2. Buzzer − → GND.

Before you power up: Use a passive piezo buzzer: tone() supplies the frequency. A magnetic (coil) buzzer draws more than 20 mA and needs a transistor driver and a flyback diode: don't drive it from a pin.

The lab models ideal parts. Real circuits add tolerances, noise and loose wires, so check your wiring against this list before you plug in.

Sketch · read-only
// Two-tone alert on a passive piezo buzzer
// Buzzer: + to D8, - to GND (a passive buzzer: tone() sets the pitch)
const int buzzerPin = 8;                // the buzzer's + leg

void setup() {
  // nothing to do: tone() makes buzzerPin an output by itself
}

void loop() {
  tone(buzzerPin, 880);                 // 880 Hz until stopped or changed
  delay(200);                           // wait: a tone keeps playing meanwhile
  tone(buzzerPin, 440);                 // 440 Hz until stopped or changed
  delay(200);                           // wait: a tone keeps playing meanwhile
  noTone(buzzerPin);                    // stop the tone: the pin stays LOW
  delay(600);                           // wait: a tone keeps playing meanwhile
}

Press ▶ Run, then listen and watch the timeline.