Skip to content

Instantly share code, notes, and snippets.

@Fordi
Last active March 10, 2018 21:56
Show Gist options
  • Save Fordi/8006b7980fde79a75e5f20a1daa057a7 to your computer and use it in GitHub Desktop.
Save Fordi/8006b7980fde79a75e5f20a1daa057a7 to your computer and use it in GitHub Desktop.
Instructions for building a simple light-up toggle button in a small space
// the number of the pushbutton pin
const int buttonPin = PB0;
// the number of the LED pin
const int ledPin = PB1;
int ledState = 0;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the button
int btn = digitalRead(buttonPin);
// when button state has changed and is pressed
if (btn != buttonState && btn == HIGH) {
// toggle the logical LED state
if (ledState == HIGH) {
ledState = LOW;
} else {
ledState = HIGH;
}
// and make the logical real
digitalWrite(ledPin, ledState);
}
buttonState = btn;
}

Parts list

Tools:

One-time

Consumable

  • Solder
  • Hook-up wire

Parts

Set-up

  1. Install Arduino IDE - https://www.arduino.cc/en/Main/Software
  2. Install USBTinyISP drivers - https://cdn.sparkfun.com/datasheets/Dev/AVR/usbtinyisp_libusb_1.2.6.0.zip
  3. Install ATTiny board definitions - http://highlowtech.org/?p=1695

Programming

  1. Open button.ino in Arduino IDE
  2. Insert ATTiny85 into Tiny AVR programmer
  3. Click "Upload" button, or Sketch/Upload

Assembly

Note: I'm a flake. When in doubt, follow the schematic.

  1. Find a good place for the parts in your wand/sword/whatever
  2. Wire a positive battery terminal ATTiny's VCC
  3. Wire a negative terminal to ATTiny's GND
  4. Solder-tie a short lead on the button to the long lead on the button not marked with '+', then wire the bundle to GND
  5. Wire the other short lead on the same side of the button to ATTiny pin 5 (PB0, bottom-right), and to a 10kΩ resistor
  6. Wire the other side of the resistor to the positive battery terminal
  7. Wire the long lead marked '+' to one side of the 100Ω resistor
  8. Wire the resistor to ATTiny pin 6 (PB1, just above pin 5)

More information

@Fordi
Copy link
Author

Fordi commented Mar 10, 2018

Notes:

  • I may have the pull-up/pull-down logic inverted for the button. This is OK; it means it'll toggle on button release instead of button press. If I've done it wrong, and the misbehavior bothers you, just swap "HIGH" for "LOW" in button.ino line19.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment