CircuitPython Basics

Let's take a look at the Blink program.

import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

Imports & Libraries

Each CircuitPython program you run needs to have a lot of information to work. The reason CircuitPython is so simple to use is that most of that information is stored in other files and works in the background. The files built into CircuitPython are called modules, and the files you load separately are called libraries. Modules are built into CircuitPython. Libraries are stored on your CIRCUITPY drive in a folder called lib.

import board
import digitalio
import time

The import statements tell the board that you're going to use a particular library or module in your code.

Module is basically a bunch of related code saved in a file with the extension .py

In this example, you imported three modules: board, digitalio, and time. All three of these modules are built into CircuitPython, so no separate library files are needed.

Each of these three modules has a purpose. The first one,board,gives you access to the hardware on your board. The second, digitalio, lets you access that hardware as inputs/outputs. The third, time, lets you control the flow of your code in multiple ways, including passing time by 'sleeping'.

Setting Up The LED

The next two lines setup the code to use the LED.

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

Your board knows the red LED as LED. So, you initialise that pin, and you set it to output. You set led to equal the rest of that information so you don't have to type it all out again later in our code.

Loop-de-loops

The third section starts with a while statement. while True: essentially means, "forever do the following:". while True: creates a loop. Code will loop "while" the condition is "true" (vs. false), and as True is never False, the code will loop forever. All code that is indented under while True: is "inside" the loop.

Inside our loop, you have four items:

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

First, you have led.value = True. This line tells the LED to turn on. On the next line, you have time.sleep(0.5). This line is telling CircuitPython to pause running code for 0.5 seconds. Since this is between turning the led on and off, the led will be on for 0.5 seconds.

The next two lines are similar. led.value = False tells the LED to turn off, and time.sleep(0.5) tells CircuitPython to pause for another 0.5 seconds. This occurs between turning the led off and back on so the LED will be off for 0.5 seconds too.

Then the loop will begin again, and continue to do so as long as the code is running!

So, when you changed the first 0.5 to 0.1, you decreased the amount of time that the code leaves the LED on. So it blinks on really quickly before turning off!

What Happens When My Code Finishes Running?

When your code finishes running, CircuitPython resets your microcontroller board to prepare it for the next run of code. That means any set up you did earlier no longer applies, and the pin states are reset.

For example, try reducing the code snippet above by eliminating the loop entirely, and replacing it with led.value = True. The LED will flash almost too quickly to see, and turn off. This is because the code finishes running and resets the pin state, and the LED is no longer receiving a signal.

To that end, most CircuitPython programs involve some kind of loop, infinite or otherwise.

What if I Don't Have the Loop?

If you don't have the loop, the code will run to the end and exit. This can lead to some unexpected behavior in simple programs like this since the "exit" also resets the state of the hardware.

References and Acknowledgement

This page is an edited version of Exploring Your First CircuitPython Program published by Kattni Rembor in the Welcome to CircuitPython guide on the Adafruit website.

Last updated