ohmio

Circuits / Sensors

Arduino UNO: dimmer with a KY-040 rotary encoder

The encoder counts clicks in either direction and the sketch turns the count into a PWM duty: 12 per click, held between 0 and 20 clicks, so 240 at most. Clockwise brightens the LED, counter-clockwise dims it, the button turns it off.

Arduino UNO: dimmer with a KY-040 rotary encoder, simulated on a breadboard

Tap a part to see its voltages and currents. It is a model, not a measurement.

What do you need?

The AliExpress links are affiliate links: if you buy through them ohmio.dev gets a commission, at no extra cost to you. Affiliate links

This page is a guide: the numbers come from a simulation, and real parts vary. Check your parts before you power the circuit. Safety and responsibility

How does a rotary encoder dim an LED?

The encoder counts clicks of 18° each, up or down. The sketch holds the count between 0 and 20 with constrain() and turns it into a PWM value, count × 12, so 20 clicks reach 240 of the 255 that analogWrite allows. PWM switches D9 on and off 490 times a second, and the share of time it is on is the duty. After 5 clicks (90° in the model) the duty is 60 / 255, 23.5 %. The green LED takes 11.2 mA through R1 (220 Ω) only during that share, so its average is 2.6 mA. At 20 clicks the duty reaches 94 %. Clockwise brightens, counter-clockwise dims, and the push switch sets the count to zero, so the LED goes out. On a real module, if clockwise dims, swap the CLK and DT wires.

Values from the Protoboard ISO simulation: a model, not a bench measurement.