In learning any programming language, you often begin with some sort of
Hello, World! program. In CircuitPython, Hello, World! is blinking an LED. Blink is one of the simplest programs in CircuitPython. It involves three built-in modules, two lines of set up, and a short loop. Despite its simplicity, it shows you many of the basic concepts needed for most CircuitPython programs, and provides a solid basis for more complex projects. Time to get blinky!
The little red LED is located next to the USB connector, near the #13 label on the board.
In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory CircuitPython_Templates/blink/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.
Your CIRCUITPY drive should now look similar to the following image:
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT """CircuitPython Blink Example - the CircuitPython 'Hello, World!'""" import time import board import digitalio 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)
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.
import three modules:
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
board module, and save it to the variable
led. Then, you tell the pin to act as an
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!