Grid sequencer on an Arduino Uno
An 8-step melody sequencer on an 8x8 LED matrix: a dot per step, its height the pitch. Up and down buttons change the note, next moves on, and a knob sets the tempo. The whole build, an Arduino Uno and 7 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.
Advanced About 30 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $36. On Mokxi they're free.
What you'll learn
- Sound
- Displays
- Controllers
- Reading a button
- Reading a knob with an analog pin
- Difficulty: Advanced
- Time: About 30 minutes
- Board: Arduino Uno R3
- Parts: 9
How it works
An 8-step melody sequencer on an 8x8 LED matrix: a dot per step, its height the pitch. Up and down buttons change the note, next moves on, and a knob sets the tempo.
Arduino Uno R3 runs the sketch below. The circuit around it: LED matrix, 8x8, Pushbutton, Piezo speaker, Potentiometer. pinMode sets UP_PIN as an input with its pull-up on. digitalRead reads whether pin is HIGH or LOW. analogRead measures the voltage on A0 as a number. Serial.begin opens the serial port at 9600 baud. tone plays a note on PIEZO_PIN.
Parts list
9 parts, plus the jumper wires. They retail for about $36; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × LED matrix, 8x8
- 4 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
Kits that can build it
This starter kit has 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 |
|---|---|
| LED matrix, 8x8 pin GND | Arduino Uno R3 pin GND |
| 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 |
| Piezo speaker pin 2 | Arduino Uno R3 pin GND |
| Potentiometer pin 1 | Arduino Uno R3 pin GND |
| Pushbutton (4) pin 1a | Arduino Uno R3 pin 12 |
| LED matrix, 8x8 pin CLK | Arduino Uno R3 pin 11 |
| LED matrix, 8x8 pin CS | Arduino Uno R3 pin 10 |
| Piezo speaker pin 1 | Arduino Uno R3 pin 9 |
| LED matrix, 8x8 pin DIN | 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 matrix, 8x8 pin VCC | Arduino Uno R3 pin 5V |
| Potentiometer pin 3 | Arduino Uno R3 pin 5V |
| Potentiometer pin 2 | Arduino Uno R3 pin A0 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Grid sequencer: draw an eight-note melody on an 8x8 LED matrix.
//
// Wiring:
// matrix MAX7219: DIN -> pin 5, CLK -> pin 11, CS -> pin 10, VCC -> 5 V, GND
// up, down pins 2 and 3 to GND: raise or lower the selected step's note
// next pin 4 to GND: select the next step
// rest pin 12 to GND: turns the selected step into a rest and back
// tempo potentiometer wiper -> A0, ends to 5 V and GND
// piezo pin 9 -> piezo -> GND
//
// The matrix is a graph. Each of the eight columns is one step of the melody, and
// the row of the dot in that column is the pitch: low notes at the bottom, high
// ones at the top. The top row is a playhead: one dot that walks along as the
// steps play. The melody is stored in an array of eight numbers, one row number
// for each column (or REST).
//
// To edit, press up or down: the selected step's dot moves up or down and plays
// its note so you can hear it. The selected step blinks. Press next to move to the
// following step. The rest button turns the selected step into silence and back.
//
// The notes are the major pentatonic scale, C D E G A C D, which sounds good in
// any order: it has no notes that clash.
//
// Things to try. Make a rising run, then a falling one. Put rests in. Slow it down.
#include <LedControl.h>
const uint8_t UP_PIN = 2, DOWN_PIN = 3, NEXT_PIN = 4, REST_PIN = 12;
const uint8_t PIEZO_PIN = 9;
LedControl matrix(5, 11, 10, 1); // DIN, CLK, CS, one module
const uint8_t STEPS = 8;
const uint8_t REST = 255;
const uint8_t TOP_NOTE = 6; // rows 0 to 6 are notes, row 7 is the playhead
const uint16_t NOTE_HZ[7] = {262, 294, 330, 392, 440, 523, 587};
const char *const NOTE_NAME[7] = {"C4", "D4", "E4", "G4", "A4", "C5", "D5"};
// one row number per column: the melody
uint8_t melody[STEPS] = {0, 2, 4, 3, 5, 4, 2, 1};
uint8_t remembered[STEPS] = {0, 2, 4, 3, 5, 4, 2, 1};
uint8_t cursor = 0;
uint8_t step = 0;
uint32_t nextStep = 0;
uint32_t noteOffAt = 0;
bool sounding = false;
uint32_t previewUntil = 0;
bool upWas = false, downWas = false, nextWas = false, restWas = false;
uint32_t upAt = 0, downAt = 0, nextAt = 0, restAt = 0;
static bool pressed(uint8_t pin, bool &was, uint32_t &at)
{
bool down = digitalRead(pin) == LOW;
uint32_t now = millis();
if (down != was && now - at > 20) {
was = down;
at = now;
return down;
}
return false;
}
// Send the picture one row at a time: eight transfers, not sixty-four, so
// drawing takes well under a millisecond and never makes a step late.
static void draw(bool blinkOn)
{
for (uint8_t row = 0; row < 8; row++) {
uint8_t bits = 0;
for (uint8_t col = 0; col < STEPS; col++) {
bool on = false;
if (row == 7) {
on = col == step; // the playhead
} else if (melody[col] == row) {
on = col != cursor || blinkOn;
}
if (on) {
bits |= 1 << (7 - col); // column 0 is the left-hand end
}
}
matrix.setRow(0, 7 - row, bits); // music row 0 is the bottom of the picture
}
}
static void preview(uint8_t note)
{
if (note != REST) {
tone(PIEZO_PIN, NOTE_HZ[note]);
previewUntil = millis() + 140;
}
}
void setup()
{
Serial.begin(9600);
pinMode(UP_PIN, INPUT_PULLUP);
pinMode(DOWN_PIN, INPUT_PULLUP);
pinMode(NEXT_PIN, INPUT_PULLUP);
pinMode(REST_PIN, INPUT_PULLUP);
pinMode(PIEZO_PIN, OUTPUT);
matrix.shutdown(0, false);
matrix.setIntensity(0, 6);
matrix.clearDisplay(0);
nextStep = millis() + 300;
draw(true);
Serial.println("Grid sequencer ready: 8 steps");
}
void loop()
{
uint32_t now = millis();
// up and down change the pitch of the selected step
int8_t dir = 0;
if (pressed(UP_PIN, upWas, upAt)) {
dir = 1;
} else if (pressed(DOWN_PIN, downWas, downAt)) {
dir = -1;
}
if (dir != 0) {
int8_t note = melody[cursor] == REST ? 3 : melody[cursor] + dir;
if (note < 0) {
note = 0;
}
if (note > TOP_NOTE) {
note = TOP_NOTE;
}
melody[cursor] = note;
remembered[cursor] = note;
Serial.print("Step ");
Serial.print(cursor + 1);
Serial.print(" set to ");
Serial.println(NOTE_NAME[note]);
preview(note);
}
if (pressed(NEXT_PIN, nextWas, nextAt)) {
cursor = (cursor + 1) % STEPS;
Serial.print("Selected step ");
Serial.println(cursor + 1);
}
if (pressed(REST_PIN, restWas, restAt)) {
melody[cursor] = melody[cursor] == REST ? remembered[cursor] : REST;
Serial.print("Step ");
Serial.print(cursor + 1);
Serial.println(melody[cursor] == REST ? " is a rest" : " is a note again");
}
// the clock: eighth notes, 80 to 200 BPM from the knob
if (now >= nextStep) {
uint16_t bpm = 80 + (uint16_t)((analogRead(A0) * 120UL) / 1023);
nextStep += 30000UL / bpm;
if (nextStep < now) {
nextStep = now + 30000UL / bpm;
}
step = (step + 1) % STEPS;
uint8_t note = melody[step];
if (note == REST) {
Serial.print("Step ");
Serial.print(step + 1);
Serial.println(" rest");
} else {
if (previewUntil == 0 || now >= previewUntil) {
tone(PIEZO_PIN, NOTE_HZ[note]);
sounding = true;
noteOffAt = now + 30000UL / bpm * 8 / 10;
}
Serial.print("Step ");
Serial.print(step + 1);
Serial.print(" ");
Serial.print(NOTE_NAME[note]);
Serial.print(" ");
Serial.print(NOTE_HZ[note]);
Serial.print(" Hz ");
Serial.print(bpm);
Serial.println(" BPM");
}
}
if (sounding && now >= noteOffAt) {
noTone(PIEZO_PIN);
sounding = false;
}
if (previewUntil != 0 && now >= previewUntil) {
previewUntil = 0;
if (!sounding) {
noTone(PIEZO_PIN);
}
}
// the picture: the selected step blinks
static uint32_t lastDraw = 0;
static bool blink = true;
if (now - lastDraw >= 120) {
lastDraw = now;
blink = !blink;
draw(blink);
}
}
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);
pinMode(UP_PIN, INPUT_PULLUP);
pinMode(DOWN_PIN, INPUT_PULLUP);
pinMode(NEXT_PIN, INPUT_PULLUP);
pinMode(REST_PIN, INPUT_PULLUP);
pinMode(PIEZO_PIN, OUTPUT);
matrix.shutdown(0, false);
matrix.setIntensity(0, 6);
matrix.clearDisplay(0);
nextStep = millis() + 300;
draw(true);
Serial.println("Grid sequencer ready: 8 steps");
}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();
// up and down change the pitch of the selected step
int8_t dir = 0;
if (pressed(UP_PIN, upWas, upAt)) {
dir = 1;
} else if (pressed(DOWN_PIN, downWas, downAt)) {
dir = -1;
}
if (dir != 0) {
int8_t note = melody[cursor] == REST ? 3 : melody[cursor] + dir;
if (note < 0) {
note = 0;
}
if (note > TOP_NOTE) {
note = TOP_NOTE;
}
melody[cursor] = note;
remembered[cursor] = note;
Serial.print("Step ");
Serial.print(cursor + 1);
Serial.print(" set to ");
Serial.println(NOTE_NAME[note]);
preview(note);
}
if (pressed(NEXT_PIN, nextWas, nextAt)) {
// ... 61 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The matrix is blank after upload. LED matrix
On power up the MAX7219 is in shutdown mode with intensity 0. Wake it, set intensity, set scan limit to all 8 digits, and clear the display. - Dim, flickering, or wrong rows. LED matrix
Set the ISET resistor right for your LEDs (about 10 kohm for 40 mA). Power from 5 V with a 0.1 uF capacitor, and chain DOUT to the next DIN. - 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 reading jumps around or only covers part of the range. Potentiometer
Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range. - Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.