ATtiny85 dimmer
A knob on PB2 dims an LED on PB0, out of an eight-pin chip in the breadboard. No board, no USB, just the chip and its two rails. The whole build, an ATtiny85 and 3 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.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $7.45. On Mokxi they're free.
What you'll learn
- Reading a knob with an analog input
- Dimming an LED with PWM
- Scaling a 0 to 1023 reading to 0 to 255
- Running an eight-pin chip with just power and ground
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: ATtiny85
- Parts: 6
How it works
A potentiometer has its ends on 5 V and ground, and its middle pin (the wiper) goes to PB2. Turning the knob slides the voltage on that pin anywhere from 0 V up to the supply.
The sketch reads that voltage about every 20 ms and uses it to set the LED's brightness on PB0. The LED is dimmed by the chip's timer hardware, which switches the pin on and off 488 times a second. The sketch only has to give it a new number.
Parts list
6 parts, plus the jumper wires. They retail for about $7.45; on Mokxi they're free.
- 1 × ATtiny85
- 1 × Full-size breadboard
- 1 × Power, 5 V
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, amberIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
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 |
|---|---|
| Potentiometer pin 3 | ATtiny85 pin RESET (5 V) |
| ATtiny85 pin VCC | ATtiny85 pin RESET (5 V) |
| Potentiometer pin 1 | ATtiny85 pin GND |
| LED, amber pin C | ATtiny85 pin GND |
| Potentiometer pin 2 | ATtiny85 pin PB2 |
| Resistor, 220 Ω pin 1 | ATtiny85 pin PB0 |
| LED, amber pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Knob dimmer: a potentiometer on PB2 (A1, package pin 7) sets the
// brightness of an LED on PB0 (pin 5).
//
// The pot's two ends go to VCC and GND and its wiper to PB2, so turning it
// sweeps the pin from 0 V to the supply. analogRead gives 0..1023 against VCC
// and analogWrite takes 0..255, so the reading is shifted down by two.
//
// PB0 is OC0A: timer 0's compare output, the same timer that counts
// milliseconds, at 488 Hz. The LED is dimmed by the hardware, not by the
// sketch, so the loop is free to do nothing for 20 ms between readings.
void setup()
{
pinMode(0, OUTPUT);
}
void loop()
{
int reading = analogRead(A1);
analogWrite(0, reading >> 2);
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Only one job: pinMode(0, OUTPUT) makes pin 0 (PB0) an output so it can drive the LED. The knob pin needs no setup because analogRead takes care of it.
void setup()
{
pinMode(0, OUTPUT);
}loop sketch.ino
analogRead(A1) gives a number from 0 to 1023. analogWrite wants 0 to 255, so the code shifts the reading right by two bits (reading >> 2), which divides it by four. Then it waits 20 ms and repeats.
void loop()
{
int reading = analogRead(A1);
analogWrite(0, reading >> 2);
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED is always bright or always dark This project
Check the potentiometer: pin 1 to ground, pin 3 to 5 V, and the middle pin 2 to PB2. - Nothing runs at all This project
The RESET pin must be tied to 5 V, along with VCC. Ground the GND pin too. - 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. - 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. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.