The Circuit Playground Express and Bluefruit have a little red LED next to the USB port. It's labeled D13. Though the images are of the Circuit Playground Express, the LED is in the same location on the Bluefruit. The first thing we're going to do is turn on that red LED.
First, we need to add the following code to code.py. Remember, if you need help with this, check here.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """This example turns on the little red LED.""" from adafruit_circuitplayground import cp while True: cp.red_led = True
Red LED!
Now let's look at the code.
First we import the library with from adafruit_circuitplayground import cp
.
Then we have a while
statement. while True:
essentially means, "Forever do:". while True:
creates a loop. When there is a loop, the code will forever go through the code inside the loop. All code that is indented under while True:
is "inside" the loop.
For the red LED, "on" and "off" are states referred to as True
and False
respectively. So, if you want to turn on the LED, you set it to True
. If you want to turn it off, you set it to False
. We want to turn on the LED. So let's set it to True
by saying cp.red_led = True
.
And that's it! You should be rewarded by the little red LED next to your USB connector turning on! But why stop there? Let's try something a little more fun.
Blinky!
In any programming language, the first piece of code any programmer writes is a program called "Hello, world!" that prints exactly that. The idea behind it is it's an excellent introduction to the language and programming environment. In CircuitPython, our Hello, world!
is called Blinky. Instead of simply writing code that prints out hello
, we write code that blinks the LED! So, to welcome you to the world of programming, we're going to blink the little red LED. Let's take a look!
Add the following code to your code.py.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """This is the "Hello, world!" of CircuitPython: Blinky! This example blinks the little red LED on and off!""" import time from adafruit_circuitplayground import cp while True: cp.red_led = True time.sleep(0.5) cp.red_led = False time.sleep(0.5)
It blinks!
In this program, we need another library as well: time
. So, we import time
and cp
.
The first line inside our while True:
loop is the same as the first line of our last program. We're turning on the red LED with cp.red_led = True
. Next, we have time.sleep(0.5)
. This tells the code to pause in the current state for 0.5 seconds. In other words, we're turning on the red LED and waiting with it on for 0.5 seconds. The next line, cp.red_led = False
, turns the LED off. And the last line, time.sleep(0.5)
, again tells the code to wait, this time with the LED off. Then it repeats forever - remember we're inside our while
loop! And, when the LED turns on for 0.5 seconds and then off for 0.5 seconds, we have a blinking LED!
Try changing the numbers in the time.sleep(0.5)
lines to change the speed of the blinking. You can slow down the blinking by replacing both 0.5
's with a higher number, such as 1: time.sleep(1)
. You can speed it up by replacing both 0.5
's with a lower number, such as 0.1: time.sleep(0.1)
. Or, try setting them to different times to give it a funky rhythm!
Red LED On = Red LED Off
There's an even shorter way to do the same thing. Add the following code to your code.py.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """This is the "Hello, world!" of CircuitPython: Blinky! This example blinks the little red LED on and off! It's a shorter version of the other Blinky example.""" import time from adafruit_circuitplayground import cp while True: cp.red_led = not cp.red_led time.sleep(0.5)
This code simply tells the LED to cycle back and forth between on and off, or True
and False
, every 0.5 seconds. You can change the time.sleep(0.5)
to a higher or lower number to slow down or speed up the blinking. That's it!
Page last edited January 22, 2025
Text editor powered by tinymce.