Pulsing LED with micro:bit

Bonus, because I kinda like pulsing LEDs. I blame Terminator 2, when Arnie is re-routing aux power so that he can finish off the T1000...ah memories!



So this morning while working on some code for a future micro:mag project I stumbled upon the analog write command, a way to control the brightness of an LED.

So using micropython and Mu...I made this

from microbit import *
while True:
    for i in range(0,1023,10):
        pin0.write_analog(i)
        sleep(10)
    for i in range(1023,0, -10):
        pin0.write_analog(i)
        sleep(10)

This imports the micro:bit micro python library, then creates a loop to continually run the code.

The first for loop starts at 0 (which is off for a GPIO pin) and goes up to 1023 (the GPIO pin is fully on) in steps of 10. Each time the for loop iterates, the value of i is increased by 10, making the LED get brighter and brighter. The sleep value pauses for 1/100 of a second each time the loop goes round.

The second for loop works in the same manner as the first, in that the value of i changes each time the loop iterates. But this time the for loop counts down from 1023 to 0 in steps of -10

This creates a rather pleasing LED pulse, but it can also be used to precisely control a motor via a motor controller... ;)