led.value = False
time.sleep(0.5)
The built-in LED begins blinking!
Note that the code is a little less "Pythonic" than it could be. It could also be written as
led.value = not led.value with a single time.sleep(0.5) . That way is more
difficult to understand if you're new to programming, so the example is a bit longer
than it needed to be to make it easier to read.
It's important to understand what is going on in this program.
First you import three modules: time , board and digitalio . This makes these
modules available for use in your code. All three are built-in to CircuitPython, so you
don't need to download anything to get started.
Next, you set up the LED. To interact with hardware in CircuitPython, your code must
let the board know where to look for the hardware and what to do with it. So, you
create a digitalio.DigitalInOut() object, provide it the LED pin using the boar
d module, and save it to the variable led . Then, you tell the pin to act as an
OUTPUT .
Finally, you create a while True: loop. This means all the code inside the loop will
repeat indefinitely. Inside the loop, you set led.value = True which powers on the
LED. Then, you use time.sleep(0.5) to tell the code to wait half a second before
moving on to the next line. The next line sets led.value = False which turns the
LED off. Then you use another time.sleep(0.5) to wait half a second before
starting the loop over again.
With only a small update, you can control the blink speed. The blink speed is
controlled by the amount of time you tell the code to wait before moving on using
time.sleep() . The example uses 0.5 , which is one half of one second. Try
increasing or decreasing these values to see how the blinking changes.
That's all there is to blinking an LED using CircuitPython!
Digital Input
The CircuitPython digitalio module has many applications. The basic Blink
program sets up the LED as a digital output. You can just as easily set up a digital
input such as a button to control the LED. This example builds on the basic Blink
©Adafruit Industries Page 128 of 263