PyRaspBeta

← All practice problems

Raspberry Pi practice problem · LEDs & PWM

Colour wheel

HardBudget $4Python · gpiozero

Sweep an RGB LED (common cathode; red GPIO17, green GPIO27, blue GPIO22, each with its own resistor) smoothly around the colour wheel, at full saturation and brightness, once every 6 s, forever.

The hue is 60° × t (t in seconds since the start): red at 0 s, yellow at 1 s, green at 2 s, cyan at 3 s, blue at 4 s, magenta at 5 s, red again at 6 s, and blended linearly in between — e.g. at 0.5 s (hue 30°) the colour is red 1.0, green 0.5, blue 0.

The test reads each pin's PWM duty at eight moments (±0.08). Update at least every 0.05 s, and compute the colour from the elapsed time so the wheel doesn't drift.

Tip: each channel is a trapezoid over the hue: red is full from 300° to 60°, ramps down 60°→120°, is off 120°→240° and ramps up 240°→300°; green and blue are the same shape shifted by 120° and 240°.

More in LEDs & PWM