This is the source code landing page for the Arduino Beat Box project. The sketch is written for the littleBits Arduino module, which is a Leonardo-compatible Arduino. The code should port to other Arduino boards like the UNO without too much difficulty.
The Beat Box supports eight simultaneous rhythm instruments. The sound generator is interrupt driven using the bit capture counter in TIMER1. The interrupt driver sums the individual instrument samples into a DAC value. The DAC value is written into the compare register where the 16-bit PWM hardware takes over and produces a PWM bit stream. The PWM signal (pin D9) is driven into a passive low pass filter (10,610Hz corner frequency) to produce an audio signal. I used a solderless breadboard to prototype the filter and I connected the filter to the littleBits Arduino using jumper wires and a Proto module.
Waveforms (“the samples”) for sixteen rhythm instruments are provided. The waveforms are raw format, 22,050Hz, signed bit, mono. The rhythm instruments cover the classic TR-808. At this sampling rate and bit depth, it’s crunchy lo-fi all the way. The waveforms are stored in PROGMEM and are trimmed short in order to fit the limited space allowed by the ATMega.
The Beat Box has two dimmers (potentiometers) connected to analog pins A0 and A1. Pin A0 sets the tempo which sweeps from 60 BPM to 188 BPM. Pin A1 chooses one of five rhythm patterns to be played.
A rhythm pattern consists of 1 to 64 steps. Each step consists of eight bits. Each bit in a pattern step triggers a particular rhythm instrument (i.e., starts the instrument’s waveform playing). A rhythm waveform plays through to the end unless it is restarted from the beginning by a later step.
Instruments are organized into one or more kits. A kit consists of eight rhythm instruments, selected from the sixteen available waveforms. The bits in a pattern step correspond to the eight instruments in the currently selected kit.
A small number of predefined rhythm patterns is provided. The rhythm patterns are taken from songs made famous by the Roland TR-808. Guess which ones!
There are four source code files:
- BeatBox.ino: The sketch including TIMER1 set-up and interrupt handler.
- waveforms.h: Samples for the sixteen rhythm instruments.
- kits.h: Rhythm kits needed by the example patterns.
- patterns.h: Exaample rhythm patterns.
All four files must reside in an Arduino IDE project directory named “BeatBox”.
waveforms.h is a long, boring file, so I did not post it here as a WordPress page. Instead, you should download BeatBox.zip which contains all of the source, including
waveforms.h. The ZIP file decompresses into a directory named “BeatBox”, leaving you ready to open the project in the Arduino IDE.
The samples, kits and patterns are all stored in PROGMEM. Yeah, it was a challenge making all of this fit and still sound half-way decent. Luckily, I dig lo-fi.
If you ever wanted to generate audio with just an Arduino, 16-bit PWM and a low pass filter, this is the code to study!