Looper on an Arduino Uno
Four pads and a four-second loop on an Uno: record a rhythm, hear it come round again, add more on top, and snap your taps to the beat with quantize. The whole build, an Arduino Uno and 12 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 40 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- Sound
- Lights
- Controllers
- Reading a button
- Choosing and reading a resistor
- Difficulty: Intermediate
- Time: About 40 minutes
- Board: Arduino Uno R3
- Parts: 14
How it works
Four pads and a four-second loop on an Uno: record a rhythm, hear it come round again, add more on top, and snap your taps to the beat with quantize.
Arduino Uno R3 runs the sketch below. The circuit around it: Pushbutton, Piezo speaker, Resistor, 220 Ω, LED, red, LED, green. pinMode sets PAD_PIN[i] as an input with its pull-up on. digitalRead reads whether pin is HIGH or LOW. Serial.begin opens the serial port at 9600 baud. tone plays a note on PIEZO_PIN. millis reads the time since power-up, so the sketch can time things without stopping.
Parts list
14 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 7 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 2 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, greenIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Kits that can build it
These 10 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Pushbutton (1) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (2) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (3) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (4) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (5) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (6) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (7) pin 2a | Arduino Uno R3 pin GND |
| Piezo speaker pin 2 | Arduino Uno R3 pin GND |
| Pushbutton (7) pin 1a | Arduino Uno R3 pin 12 |
| Piezo speaker pin 1 | Arduino Uno R3 pin 10 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 9 |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 8 |
| Pushbutton (6) pin 1a | Arduino Uno R3 pin 7 |
| Pushbutton (5) pin 1a | Arduino Uno R3 pin 6 |
| Pushbutton (4) pin 1a | Arduino Uno R3 pin 5 |
| Pushbutton (3) pin 1a | Arduino Uno R3 pin 4 |
| Pushbutton (2) pin 1a | Arduino Uno R3 pin 3 |
| Pushbutton (1) pin 1a | Arduino Uno R3 pin 2 |
| LED, red pin C | Arduino Uno R3 pin GND |
| LED, green pin C | Arduino Uno R3 pin GND |
| LED, red pin A | Resistor, 220 Ω (1) pin 2 |
| LED, green pin A | Resistor, 220 Ω (2) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Looper: play four pads, record a rhythm, and hear it come round again.
//
// Wiring:
// pads pins 2 to 5, each to GND (C E G C)
// record pin 6 to GND: starts and stops recording
// clear pin 7 to GND: forgets the loop
// quantize pin 12 to GND: snaps recorded taps to the nearest eighth note
// rec LED pin 8 -> 220 ohms -> red LED -> GND
// beat LED pin 9 -> 220 ohms -> green LED -> GND (blinks on each quarter of the loop)
// piezo pin 10 -> piezo -> GND
//
// The loop is four seconds long and always going round, like a looper pedal. The
// green LED blinks four times each time round, so you have a beat to play along
// to. Press record, tap the pads in time, press record again to stop. The taps are
// stored and played back every time the loop comes round. Press record again to
// add more taps on top (overdub).
//
// What is stored is only a list: for each tap, which pad and how many
// milliseconds after the start of the loop it happened. Playing it back is
// watching the clock and starting a note when the time passes a tap's mark.
// Because the clock wraps back to zero at the end of the loop, the sketch checks
// both for a tap between the last time it looked and now, and for one that falls
// across the wrap.
//
// Quantizing rounds each tap to the nearest eighth of the loop (500 ms). It
// makes a loose rhythm tight, and a deliberately loose one too tight.
//
// One piezo makes one note at a time. If you play a pad while the loop is
// sounding a note, yours wins. That is the limit of a single voice.
//
// Things to try. Record a kick-like pattern on the low pad, then add a melody.
// Record with quantize off, then clear and record again with it on.
const uint8_t PADS = 4;
const uint8_t PAD_PIN[PADS] = {2, 3, 4, 5};
const uint16_t PAD_HZ[PADS] = {262, 330, 392, 523};
const uint8_t REC_PIN = 6;
const uint8_t CLEAR_PIN = 7;
const uint8_t QUANT_PIN = 12;
const uint8_t REC_LED = 8;
const uint8_t BEAT_LED = 9;
const uint8_t PIEZO_PIN = 10;
const uint16_t LOOP_MS = 4000;
const uint16_t GRID_MS = 500; // an eighth of the loop
const uint16_t NOTE_MS = 160; // how long a played-back tap sounds
const uint8_t MAX_TAPS = 32;
struct Tap {
uint16_t at; // milliseconds from the start of the loop
uint8_t pad;
};
Tap taps[MAX_TAPS];
uint8_t count = 0;
uint32_t loopStart = 0;
uint16_t lastPos = 0;
bool recording = false;
bool quantize = false;
int8_t heldPad = -1; // the pad being held now
uint32_t playUntil = 0; // when the loop's own note ends
bool loopSounding = false;
// button edges
bool wasDown[PADS + 3];
uint32_t changedAt[PADS + 3];
static bool pressed(uint8_t slot, uint8_t pin, bool *released = nullptr)
{
bool down = digitalRead(pin) == LOW;
uint32_t now = millis();
if (down != wasDown[slot] && now - changedAt[slot] > 20) {
wasDown[slot] = down;
changedAt[slot] = now;
if (released) {
*released = !down;
}
return down;
}
if (released) {
*released = false;
}
return false;
}
static void record(uint8_t pad, uint16_t pos)
{
if (quantize) {
pos = ((pos + GRID_MS / 2) / GRID_MS * GRID_MS) % LOOP_MS;
}
if (count == MAX_TAPS) {
Serial.println("Loop is full");
return;
}
taps[count].at = pos;
taps[count].pad = pad;
count++;
Serial.print("Rec pad ");
Serial.print(pad + 1);
Serial.print(" at ");
Serial.print(pos);
Serial.println(" ms");
}
void setup()
{
Serial.begin(9600);
for (uint8_t i = 0; i < PADS; i++) {
pinMode(PAD_PIN[i], INPUT_PULLUP);
}
pinMode(REC_PIN, INPUT_PULLUP);
pinMode(CLEAR_PIN, INPUT_PULLUP);
pinMode(QUANT_PIN, INPUT_PULLUP);
pinMode(REC_LED, OUTPUT);
pinMode(BEAT_LED, OUTPUT);
pinMode(PIEZO_PIN, OUTPUT);
loopStart = millis();
Serial.println("Looper ready: 4 pads, 4 second loop");
}
void loop()
{
uint32_t now = millis();
uint16_t pos = (now - loopStart) % LOOP_MS;
// the beat light: on for the first 80 ms of each quarter of the loop
digitalWrite(BEAT_LED, (pos % 1000) < 80);
// the pads
for (uint8_t i = 0; i < PADS; i++) {
bool released;
if (pressed(i, PAD_PIN[i], &released)) {
heldPad = i;
tone(PIEZO_PIN, PAD_HZ[i]);
loopSounding = false;
if (recording) {
record(i, pos);
}
} else if (released && heldPad == i) {
heldPad = -1;
noTone(PIEZO_PIN);
}
}
if (pressed(PADS, REC_PIN)) {
recording = !recording;
digitalWrite(REC_LED, recording);
Serial.println(recording ? "Recording on" : "Recording off");
if (!recording) {
Serial.print("Loop has ");
Serial.print(count);
Serial.println(" taps");
}
}
if (pressed(PADS + 1, CLEAR_PIN)) {
count = 0;
Serial.println("Loop cleared");
}
if (pressed(PADS + 2, QUANT_PIN)) {
quantize = !quantize;
Serial.println(quantize ? "Quantize on" : "Quantize off");
}
// playback: any tap whose mark was passed since the last look
for (uint8_t i = 0; i < count; i++) {
uint16_t at = taps[i].at;
bool crossed = pos >= lastPos ? (at > lastPos && at <= pos) : (at > lastPos || at <= pos);
if (crossed) {
if (heldPad < 0) {
tone(PIEZO_PIN, PAD_HZ[taps[i].pad]);
loopSounding = true;
playUntil = now + NOTE_MS;
}
Serial.print("Play pad ");
Serial.print(taps[i].pad + 1);
Serial.print(" at ");
Serial.print(at);
Serial.println(" ms");
}
}
if (loopSounding && now >= playUntil) {
noTone(PIEZO_PIN);
loopSounding = false;
}
lastPos = pos;
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup()
{
Serial.begin(9600);
for (uint8_t i = 0; i < PADS; i++) {
pinMode(PAD_PIN[i], INPUT_PULLUP);
}
pinMode(REC_PIN, INPUT_PULLUP);
pinMode(CLEAR_PIN, INPUT_PULLUP);
pinMode(QUANT_PIN, INPUT_PULLUP);
pinMode(REC_LED, OUTPUT);
pinMode(BEAT_LED, OUTPUT);
pinMode(PIEZO_PIN, OUTPUT);
loopStart = millis();
Serial.println("Looper ready: 4 pads, 4 second loop");
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop()
{
uint32_t now = millis();
uint16_t pos = (now - loopStart) % LOOP_MS;
// the beat light: on for the first 80 ms of each quarter of the loop
digitalWrite(BEAT_LED, (pos % 1000) < 80);
// the pads
for (uint8_t i = 0; i < PADS; i++) {
bool released;
if (pressed(i, PAD_PIN[i], &released)) {
heldPad = i;
tone(PIEZO_PIN, PAD_HZ[i]);
loopSounding = false;
if (recording) {
record(i, pos);
}
} else if (released && heldPad == i) {
heldPad = -1;
noTone(PIEZO_PIN);
}
}
if (pressed(PADS, REC_PIN)) {
recording = !recording;
digitalWrite(REC_LED, recording);
Serial.println(recording ? "Recording on" : "Recording off");
// ... 38 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead. - Sound is very quiet or the pin seems overloaded. Piezo speaker
Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.