PyRaspBeta

← All practice problems

Raspberry Pi practice problem · LEDs & PWM

Three-phase glow

HardBudget $5Python · gpiozero

Three PWM LEDs, A on GPIO5, B on GPIO6, C on GPIO13, breathe on smooth sine waves with a 3 s period, each a third of a cycle behind the one before — like the three phases of mains power.

With t the time in seconds since the start and i = 0, 1, 2 for A, B, C:

duty_i = 0.5 + 0.5 × sin(2π × (t / 3 − i / 3))

So at 0 s A is at 0.5, B at 0.07 and C at 0.93; at 0.75 s A peaks at 1.0.

The test reads each duty at eight moments (±0.08). Update at least every 0.05 s and compute from the elapsed time (time.monotonic()), so the waves stay in step.

More in LEDs & PWM