Speaker (8 ohm)

A small 8 ohm speaker, the 40 mm paper cone in every kit: a coil in a magnet's gap, glued to a cone.

It is what the Arduino's toneMelody example drives with tone(), and that example has a 100 ohm resistor in series for a reason this part models.

Unlike the piezo, a speaker is a low resistance, and a board pin straight across eight ohms is close to a short circuit.

Pins

Pin What it does
1 One tag.
2 The other. Either way around.

Properties

ohms: the voice coil's resistance, 8.

What the model gets right

The coil is a real 8 ohm load. With 100 ohms in series from a 5 V pin it takes about 46 mA and the pin holds its voltage; straight on the pin, the pin sags to a volt or so and would be sourcing far past its 40 mA absolute maximum, which a multimeter in series shows.

The note. The part times each push of the cone, the current through the coil rising past 4 mA one way after being under 2 mA, and reports the frequency in hertz. It counts a pin toggling against ground, against 5 V, or an H bridge driving both ends alike.

What it does not model

Loudness: the probe is a frequency and says nothing about level, which on a real speaker rises with the current and falls off at the bottom and top of its range. The coil's inductance, its power rating (usually half a watt) and burning out. No sound comes out of the page; the part shows the note it hears.

Code

tone() plays the note from a timer interrupt while the sketch carries on, as on a real board, and noTone() stops it. On an Uno it takes timer 2, as the Arduino core does, so PWM on pins 3 and 11 pauses while a note plays; on the XIAO and the Uno R4 put the speaker on a PWM pin. pitches.h has the note names:

#include "pitches.h"

void setup() {}

void loop() {
  tone(8, NOTE_A4, 200);   // an A for 200 ms on pin 8
  delay(400);
}

The older mokxiTone from mokxi_tone.h still works too: it toggles the pin itself and blocks for the length of the note.

See it in action

Music box plays a tune on one through 100 ohms. Open it at /templates.