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 LED is located between the USB C port and the mounting hole.
Blinking an LED
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.
# 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)
In the editor window in your browser, click the Open button to view the file dialog. Then, click the Upload button and select Upload Files.
You'll be asked if you want to overwrite the previous code.py with the new code.py file from the Project Bundle. Click OK.
You'll see a new code.py file appear in the file browser. Select it and click Open to view it in the code editor.
You'll see the LED blink code.py file contents. Click Restart above the Serial monitor to run the LED blink code.
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 board
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!
Text editor powered by tinymce.