CircuitPython code on the ItsyBitsy

We can build circuits all day long, using sensors like the potentiometer to control outputs like the brightness of an LED, but changing the functionality of the circuit often takes a lot of redesign and building. Instead, we can use code on a microcontroller, wired to simple circuits, to accomplish our objectives, and make it easy to change the functionality on the fly and prototype rapidly.

Installations

You may have noticed that when you plug the ItsyBitsy into your computer a memory drive, like a thumb drive, is created. There are files on the drive that contain the Python code that runs on the ItsyBitsy. To change the program, all you need to do is open code.py in a text editor and make changes, then when you save the code it starts to run!

So technically you don't need any special IDE program to write code for the ItsyBitsy, just a text editor. But Mu has some nice extra features, like a window for Repl and a plotter, so we'll use Mu again, which you should already have installed from https://codewith.mu/en/.

Open Mu and see what is already there

The ItsyBitsy comes with a program installed that runs through some of the cool things that it can do. Open Mu, change the Mode to Adafruit CircuitPython, and Open the code.py file from the CircuitPython drive.

Original code.py

That's a lot of code! We'll break it down over the next few exercises.

Blink the LED

There is already an LED soldered onto the ItsyBitsy on pin 13. Let's mke it blink!

The top of the code includes the libraries necessary to control the pin:

import board
import time
import gc
from digitalio import DigitalInOut, Direction, Pull

Then we can define a variable called led that represents pin 13, and make it an output:

led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

Then in an infinite while loop we can blink the LED forever

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

Delete the contents of code.py and write some code that makes the LED blink once per second. When you save the code, verify that the LED blinks. Then change it so that it blinks 10 times per second. How long should each delay be? At what rate can you no longer tell that the LED is blinking, it just looks like it is always on?

Solution

Debugging with Repl

Do not expect your code to work the first time. To debug, you can enter Repl mode and see what the errors are.

To get into Repl mode, press the Serial button, then press Ctrl-c, and then press any key.

To get out of Repl mode, press Ctrl-d, and your code will start running again.

Buttons and LEDs

Edit the code so that the ItsyBitsy can read a button and control an LED. When the button is pressed, the LED should be on, and when the button is not pressed, the LED should be off. Here is the circuit diagram for the LED and button, using pins 9 and 7.

Image of button and LED teensy circuit diagram The ItsyBitsy button and LED circuit diagram

Note that the button circuit is a little weird. What voltage does the ItsyBitsy read when the button is pressed? When the button is not pressed?

Build an LED circuit, turn on when button is pressed

You'll need to make sure the button pin is an input:

but = DigitalInOut(board.D7)
but.direction = Direction.INPUT

And you can read the value of the button like:

if(but.value==0):
    # do something

Solution