Four-key keyboard on an Arduino Uno
Four buttons share A0 through a ladder of 10k resistors, so each key reads a different number, and a piezo plays C, D, E or F. The whole build, an Arduino Uno and 9 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.
Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
Build it as the Starter Projects Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $48, free on Mokxi.
What you'll learn
- How a resistor ladder lets one analog pin read many buttons
- Voltage dividers and why each key reads a different number
- Using a band of values instead of an exact match
- Playing a note with
toneand stopping it withnoTone
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 11
How it works
Four buttons share one analog pin, A0. One side of each button goes to 5V. The other sides go to different points on a ladder of 10k resistors, and A0 also has a 10k resistor to ground.
Each pressed button makes a voltage divider with that bottom 10k. The first button gives about 1023, the second about 511, the third about 341 and the fourth about 256. The more resistors above A0, the lower the reading.
The sketch sorts the reading into a band and plays that key's note on the piezo on pin 8: C, D, E or F. The note plays as long as the button is held, and stops when you let go.
Parts list
11 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
- 4 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 4 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine 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 |
|---|---|
| Resistor, 10k Ω (4) pin 1 | Arduino Uno R3 pin GND |
| Piezo speaker pin 2 | Arduino Uno R3 pin GND |
| Piezo speaker pin 1 | Arduino Uno R3 pin 8 |
| Pushbutton (1) pin 1a | Arduino Uno R3 pin 5V |
| Pushbutton (2) pin 1a | Arduino Uno R3 pin 5V |
| Pushbutton (3) pin 1a | Arduino Uno R3 pin 5V |
| Pushbutton (4) pin 1a | Arduino Uno R3 pin 5V |
| Pushbutton (1) pin 2b | Arduino Uno R3 pin A0 |
| Resistor, 10k Ω (1) pin 1 | Arduino Uno R3 pin A0 |
| Resistor, 10k Ω (4) pin 2 | Arduino Uno R3 pin A0 |
| Resistor, 10k Ω (1) pin 2 | Pushbutton (2) pin 2b |
| Resistor, 10k Ω (2) pin 1 | Pushbutton (2) pin 2b |
| Resistor, 10k Ω (2) pin 2 | Pushbutton (3) pin 2b |
| Resistor, 10k Ω (3) pin 1 | Pushbutton (3) pin 2b |
| Resistor, 10k Ω (3) pin 2 | Pushbutton (4) pin 2b |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Four-key keyboard: four buttons share one analog pin through a ladder of
// resistors, and a piezo plays a note for each.
//
// every button one side to 5V
// button 1 its other side straight to A0
// button 2 through one 10k resistor to A0
// button 3 through two 10k resistors in a row to A0
// button 4 through three 10k resistors in a row to A0
// A0 also a 10k resistor to GND
// pin 8 the piezo, its other leg to GND
//
// Each button makes a voltage divider with the 10k at the bottom, and the
// further along the ladder it is, the more resistance sits above A0 and the
// lower the reading:
//
// button 1 5V straight in about 1023
// button 2 10k over 10k about 511
// button 3 20k over 10k about 341
// button 4 30k over 10k about 256
//
// One pin, four keys. The sketch sorts the reading into a band and plays that
// key's note for as long as it is held.
const int PIEZO = 8;
const int NOTES[4] = {262, 294, 330, 349}; // C4, D4, E4, F4
const char *NAMES[4] = {"C", "D", "E", "F"};
int lastKey = -1;
int keyFor(int reading) {
if (reading > 1000) return 0;
if (reading >= 450 && reading <= 570) return 1;
if (reading >= 300 && reading <= 400) return 2;
if (reading >= 220 && reading <= 290) return 3;
return -1;
}
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: four-key keyboard");
}
void loop() {
int reading = analogRead(A0);
int key = keyFor(reading);
if (key != lastKey) {
lastKey = key;
if (key < 0) {
noTone(PIEZO);
} else {
tone(PIEZO, NOTES[key]);
Serial.print("key ");
Serial.print(key + 1);
Serial.print(": ");
Serial.print(NAMES[key]);
Serial.print(" (reading ");
Serial.print(reading);
Serial.println(")");
}
}
delay(10);
}
Code walkthrough
The key functions of the sketch, and what each one does.
keyFor sketch.ino
Turns an analogRead number into a key number, 0 to 3, or -1 for none. Each key has a band, like 450 to 570 for the second one, rather than one exact value. That leaves room for small differences.
int keyFor(int reading) {
if (reading > 1000) return 0;
if (reading >= 450 && reading <= 570) return 1;
if (reading >= 300 && reading <= 400) return 2;
if (reading >= 220 && reading <= 290) return 3;
return -1;
}setup sketch.ino
Starts the serial port at 9600 and prints a title. The piezo pin needs no setup because tone takes care of it.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: four-key keyboard");
}loop sketch.ino
Reads A0 and asks keyFor which key it is. It acts only when the key changes: noTone for no key, or tone with the right note. It also prints the key and the reading to the serial monitor.
void loop() {
int reading = analogRead(A0);
int key = keyFor(reading);
if (key != lastKey) {
lastKey = key;
if (key < 0) {
noTone(PIEZO);
} else {
tone(PIEZO, NOTES[key]);
Serial.print("key ");
Serial.print(key + 1);
Serial.print(": ");
Serial.print(NAMES[key]);
Serial.print(" (reading ");
Serial.print(reading);
Serial.println(")");
}
}
delay(10);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- No key makes a sound. This project
Check that A0 also has the 10k resistor to ground, and that every button has one side on 5V. - Two keys play the same note. This project
Check the ladder: the resistors must be in a row, so each key adds one more 10k. - A key gets no sound but others work. This project
The reading may fall outside that key's band inkeyFor. Print it and compare. - 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. - 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. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.