Lab: a knob that dims the LED
The TK07 thumbwheel on A0 sets the brightness of the TK01 XL LED on D3. A reading of 0 to 1023 becomes a duty cycle of 0 to 255, and squaring it on the way, rather than dividing by four, makes equal turns of the wheel look like equal steps of light.
Two modules, one line of arithmetic between them. The wheel is a voltage divider, read by analogRead; the LED is driven by PWM. Both modules have their own books, the TK07 and the TK01, and this lab does not repeat them.
Where they plug in
| Module | Socket | Pin |
|---|---|---|
| TK07 disc potentiometer | D9/A0 | A0 |
| TK01 XL LED | D2/D3 | D3 |
A module is on the second pin printed beside its socket. D3 matters: it is
one of the UNO's six pins marked ~, the ones analogWrite can fade. D3's PWM
comes from Timer2 at about 490 Hz, too fast to see.
A straight line looks bent
The obvious dimmer divides the reading by four: 1023 becomes 255, and every four counts of wheel is one step of duty. The LED does what it is told, but the eye does not see duty cycles in a straight line. It is far more sensitive to changes at the dim end, so half the duty already looks nearly three quarters as bright.
Pick Straight and turn the wheel to 50 %: the LED looks about 73 % bright, and the whole top half of the wheel changes very little. Pick Squared: 50 % of the wheel now looks about half as bright. A square is close to the curve the XL LED's own book uses for the eye, a power of 2.2, and it costs one multiplication.
The sketch
The two lines that matter:
long n = analogRead(KNOB_PIN); // 0 to 1023
int duty = n * n / SQUARE_DIV; // 0 to 255, squaredn is a long because the square of 1023 is 1 046 529, far past the 32 767
an int can hold on this chip. Declared int, the product overflows and the
LED jumps about at random over the top part of the wheel. SQUARE_DIV is
4104 because 1023 × 1023 / 4104 is exactly 255.
The integer division has a side effect worth keeping. Any reading under 65 squares to less than 4104, so the bottom 6 % of the wheel gives a duty of 0. The LED is properly off at that end even if the wheel's stop reads a few counts above zero.
The sketch prints only when the duty changes, so the Serial Monitor shows each step as the wheel crosses it rather than a wall of identical lines.
Make it yours
The code
Read the wheel, square the reading, write it to the LED's PWM pin. The Serial Monitor prints the reading and the duty whenever the duty changes.
/*
A knob that dims the LED TinkerBlock UNO kit, /manuals/tkbu
The TK07 disc potentiometer sets the brightness of the TK01 XL LED.
The reading is squared before it becomes a duty cycle, so equal
turns of the wheel look like equal steps of brightness.
Wiring, on the TinkerBlock UNO shield:
TK07 disc potentiometer in the D9/A0 socket, so it is on A0
TK01 XL LED in the D2/D3 socket, so it is on D3
A module is on the second pin printed beside its socket.
Arduino IDE
Tools > Board: Arduino Uno
Tools > Port: the CH340 port
No library needed. Serial Monitor at 115200.
*/
const int KNOB_PIN = A0; // TK07 SIGNAL
const int LED_PIN = 3; // TK01 SIGNAL: a ~ pin, so it can PWM
// 1023 x 1023 / 4104 = 255: the top of the wheel is full brightness.
const long SQUARE_DIV = 4104;
int lastDuty = -1;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
long n = analogRead(KNOB_PIN); // 0 to 1023
int duty = n * n / SQUARE_DIV; // 0 to 255, squared
analogWrite(LED_PIN, duty);
// Print only when something changed, so the monitor stays readable.
if (duty != lastDuty) {
Serial.print("knob ");
Serial.print(n);
Serial.print(" duty ");
Serial.println(duty);
lastDuty = duty;
}
delay(20);
}Change the line that computes duty to n / 4 to see the straight version: most of the change happens in the first quarter of the wheel. The bottom 6 % of the wheel is off on purpose, so off is easy to find.
View on GitHub · kits/tkbu/knob-dimmer/Knob_Dimmer/Knob_Dimmer.ino @ v1.14When it does not work
The LED is on a pin that cannot PWM. The XL LED must be in the D2/D3 socket, which puts it on D3, one of the UNO's six ~ pins. In the D4/D5 socket it would also fade; in D6/D7 it would be on D7, where analogWrite can only give fully off or fully on.
Open the Serial Monitor at 115200 and watch the reading. If it never moves, the TK07 is not in the D9/A0 socket or its cable is half in. If it moves but the LED does not, check the TK01 is in the D2/D3 socket and its cable is seated.
With the straight rule a reading of 4 already gives a duty of 1, and the TK07's end stop may read a few counts above 0. The squared rule in the sketch keeps every reading under 65 at a duty of 0, which absorbs that.
The reading moves by a count or two with nobody touching it, and near a step boundary that moves the duty by one. It is too small to see on the LED; the monitor shows it because it prints every change. The TK07 book's smoothing article averages it away.
The light sensor switches the LED on at dusk, with the knob setting when.
Lab: a night light →Edit this page — content/books/tkbu/knob-dimmer.mdx
Questions about this product
See what other owners have asked, and read their solutions.
TinkerBlock UNO R3 Starter Kit, 12 Modules
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.