Part 11 – Adding Sound

Adding sound is relatively simple, just add a piezo sounder directly to the Arduino. As they use so little current nothing else is needed, see this guide for a basic guide to adding one.  A youtube video to support this article is also available:

There is also a in-built tone command within the arduino environment but I went for the (what I consider better) toneAC which has a some advantages. Firstly it can make louder sounds, you can control the volume and it possibly enhances the lifetime of your speaker by flexing it in two directions rather than one. Normally I would provide a direct link to the github page etc. but for this project I needed to alter the code of toneAC slightly to support noise.

What is noise?
Noise is used in computer games (particularly from the early 80’s era) to add a base beat or for explosions etc. We need it for explosions and for when the player shoots, as in Space Invaders it uses a sort of “woosh” noise as the missile launches upwards. Noise is in fact just random frequencies played very quickly one after the other. The original toneAC did not support this so I added it.

The connections
The piezo speaker must be connected to the PWM pins, on the Arduino Uno and Nano it’s pins 9 and 10. Doesn’t matter which ones the wires are connected to.

My toneAC code
Below is the link to the extended toneAC library.

toneAC

Download it and add it to your libraries (if you’ve never used this library before) in the normal way (see this guide if your not sure). If you’ve already added the standard toneAC, for whatever reason, go into your library folder and over -copy it with this version ensuring you over-write any files already present.

Once you’ve done this you can add the Space Invaders source code below and upload to your Arduino

That about wraps it up for this series, the only planned extension at present is to add a more proper speaker and small amplifier to make the sound even better. For now I hope you enjoy the completed project.