Lit button on an Arduino Uno
A 30 mm arcade button with an LED inside: the lamp breathes while it waits, goes full bright while held, and every press is counted. The whole build, an Arduino Uno and 1 more part, 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.
The parts retail for about $30. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Lit button: a 30 mm arcade button with an LED inside. The lamp breathes
// while the button waits, goes full bright while it is held, and each press
// is counted.
//
// button 2 -> pin 7, 1 -> GND (the microswitch)
// L+ -> pin 5, L- -> GND (the lamp, a 5 V one)
//
// The two halves of the button are separate: pressing it does not light it.
// The switch is read with the pin's own pull-up, so a press reads LOW. The
// lamp has its resistor built into the holder, so a 5 V lamp goes straight
// on a pin; a red one draws about 19 mA, inside what a pin can give, and
// analogWrite() dims it with PWM.
//
// Things to try. Hold the button and let go. Then open the button's
// properties and change the color: a white or blue lamp drops more voltage
// than a red one, so on the same 5 V it is dimmer.
const int BUTTON_PIN = 7;
const int LAMP_PIN = 5;
bool wasDown = false;
unsigned long presses = 0;
unsigned long lastChange = 0;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LAMP_PIN, OUTPUT);
Serial.println("Lit button ready");
}
void loop() {
bool down = digitalRead(BUTTON_PIN) == LOW;
unsigned long now = millis();
// A microswitch barely bounces, but 10 ms of patience costs nothing.
if (down != wasDown && now - lastChange > 10) {
wasDown = down;
lastChange = now;
if (down) {
presses++;
Serial.print("Press ");
Serial.println(presses);
}
}
if (wasDown) {
analogWrite(LAMP_PIN, 255);
} else {
// A two second breath: up for a second, down for a second.
int phase = (now / 4) % 500;
int level = phase < 250 ? phase : 499 - phase;
analogWrite(LAMP_PIN, level / 2 + 4);
}
delay(5);
}
Parts list
2 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.
- 1 × Arduino Uno R3
- 1 × LED arcade button
How it is wired
4 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; LED arcade button pin 1
- Arduino Uno R3 pin 7; LED arcade button pin 2
- Arduino Uno R3 pin 5; LED arcade button pin L+
- Ground: LED arcade button pin L-
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.