How to make an Arduino-powered imperial doorbell

bell_16x9

A long time ago in a galaxy far, far away, gaming didn’t involve the latest multi-channel 24-bit digital audio, but wonderfully archaic 8-bit tones that matched the graphics of the era.

The fact the era coincided with Star Wars: Episode IV created lots of memories for those of us around at the time.

Today, retro game soundtracks are a ‘thing’, even 8-bit synthesisers are popular.

Unsurprisingly, this is something Arduino can do with aplomb and to pay our own Star Wars tribute, we’ve put together a very simple ‘Imperial Doorbell’.

Just press the button and it plays the first 12 bars of the Imperial March.


Grab the code!

Grab the source code from our website here.

You’ll need the Arduino IDE from Arduino.cc. Unzip the source code zip, load the ‘ImperialDoorbell.ino’ code, built the circuit and flash it to the Arduino.


How to make sound

overlayNormally, for a computer to make sound, you’re looking for a sound card or at least a digital-to-analog converter (DAC) of some description.

What makes 8-bit music so distinct is that it doesn’t use this new-fangled rubbish — instead, it’s created by manufacturing square-wave tones at frequencies that match musical notes.

Square-waves are easy to make because they’re just simple oscillations between digital ‘1’ and ‘0’ with a 50% duty cycle (meaning the wave is ‘1’ for half the time, ‘0’ the other half).

The distinctive ‘edgy’ sound comes from the fact that square-waves are full of multiples of the root frequency called ‘harmonics’.

The faster the oscillations repeat, the higher the tone’s pitch and Arduino has a built-in command called ‘tone()’ that creates sounds at particular frequencies:

tone(pin, frequency, duration);

Where ‘pin’ is the Arduino pin you take the sound from, ‘frequency’ is the pitch in Hertz and ‘duration’ is the note duration in milliseconds.

The Arduino IDE includes a sample source code called ‘ToneMelody’ and we’ve borrowed it, replacing the original ‘Shave and a Haircut’ riff melody with the Star Wars Imperial March.

The sheet music for this is everywhere on the web — we’ve just converted the melody into digital notes in the source code.

audThe source code itself has two parts — the ImperialDoorBell.ino ‘function code’ that does the work, and ‘pitches.h’, the ‘header’ file containing the data for the musical notes.

The Arduino makes music by setting a timer to run for a certain period, toggling the desired pin ‘high’ and ‘low’ once during that time. The data in ‘pitches.h’ are the frequencies of each note in Hertz, which the source code uses to set the time delay.

The end result is a reasonably pitch-accurate set of musical notes and all we do is choose the notes and the required duration, the Arduino does the rest.

Hearing the output

modGenerating the audio tones is one thing, but how do we hear them?

Arduino doesn’t have any audio ports, speakers or buzzers built-in. The answer is we build them — and this is where you can get as simple or as complex as you like.

The simplest option is to grab the Sound and Buzzer Module from Freetronics.

It’s a small piezo speaker board you can plug straight into the Arduino to generate audio tones from zero to 20kHz (although it’s best around 1–6kHz). Like a normal speaker, you connect one pin to your audio source output and the other to ground (GND).

But to make it easier to use, we’ve set the Arduino tone output to digital pin 8 and set pin 9 as an artificial ‘ground’, by switching it to output mode and setting it as digital ‘0’.

An artificial ground doesn’t suit every circuit, but can be very useful in the right application.

panelThe reason it works here is that the power drawn of the piezo speaker is less than pin 9 can handle when set to digital ‘0’ (called ‘sinking’ power). For us, it means you can plug the module straight into pins 8 and 9 without any need for connecting wires.

Unfortunately, the module’s output level is too low for anything but personal listening, so we also need an alternative.

The easy option is to grab a powered computer speaker and plug the Arduino output in via a 3.5mm socket and cable – but first, we must add some protection components.

Too much output

The Arduino pin 3 output has a signal level of five-volts peak-to-peak (5Vp-p or 2.8Vrms) — more than most computer speakers can handle directly without distorting.

Another issue is that we need to provide AC-coupling for the audio, so there’s no direct-current path from the Arduino to the computer speaker.

At the moment, connecting the Arduino straight into an amplifier gives us DC-coupling, so to remedy that, we add three resistors and a capacitor to the Arduino pin 3 output — two resistors form a voltage-divider, dropping the signal level by half, a capacitor provides the AC-coupling and a final resistor connecting the output side of the capacitor to ground ensures there’s no ‘thump’ when the computer speaker is plugged in.

Now, you can use the volume control on the speaker to suit.

More techie options include buying a cheap 5VDC audio amplifier module on eBay, or building your own mini amplifier using a tiny LM386 8-pin DIP chip.

One thing, though — the tone() command can only handle one output so swap the lines of code noted in the source code to switch from speaker module to this ‘line output’ circuitry.

Triggering the doorbell

bellThe last thing is triggering the doorbell itself.

The original ‘ToneMelody.ino’ example shows all function code resides in the setup() method. The basic Arduino rule is the setup() method always runs once, followed by the loop() method running continuously thereafter.

By keeping the play code in the setup() method, we just need to add a switch across the Arduino’s reset button, connected between the RESET pin and the GND pin, so that when it’s pressed, the Arduino reboots and runs the setup() method, playing our Imperial March melody once.

Up to you

If you go the basic Android/powered speaker option, you’ll need AC mains to power everything, as batteries won’t last more than a week at best.

As for the rest of the build (nuts and bolts etc), we’ll leave that to you.

If you want to get into retro 8-bit music generation, Arduino is a great place to start and explore your creative side.