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 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. Free.
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);
}
Parts list
11 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 4 × Pushbutton
- 4 × Resistor, 10k Ω
- 1 × Piezo speaker
How it is wired
7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; Resistor, 10k Ω (4) pin 1; Piezo speaker pin 2
- Arduino Uno R3 pin 8; Piezo speaker pin 1
- Arduino Uno R3 pin 5V; Pushbutton (1) pin 1a; Pushbutton (2) pin 1a; Pushbutton (3) pin 1a; Pushbutton (4) pin 1a
- Arduino Uno R3 pin A0; Pushbutton (1) pin 2b; Resistor, 10k Ω (1) pin 1; Resistor, 10k Ω (4) pin 2
- Pushbutton (2) pin 2b; Resistor, 10k Ω (1) pin 2; Resistor, 10k Ω (2) pin 1
- Pushbutton (3) pin 2b; Resistor, 10k Ω (2) pin 2; Resistor, 10k Ω (3) pin 1
- Pushbutton (4) pin 2b; Resistor, 10k Ω (3) pin 2
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.