Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

Programming Languages - CircuitPython for Microcontrollers

Circuit Python is based on the Python programming language optimised for low power microcontroller devices. This is similar to Micro Python, although there are some differences in the way that the hardware is referenced, and code written for Circuit Python will not necessarily work with Micro Python or vice versa.

Installing CircuitPython on a Raspberry Pi Pico

You can download circuit python from the CircuitPython website.

The UF2 file needs to be copied to the Raspberry Pi Pico. To do this you first need to connect the Pico with the boot sel button pressed and then copy the file to the removable drive RPI-RP2.

To add additional drivers you can first download the appropriate version from Circuit Python Libraries download page.

The libraries is very big so you'll just want to install the relevant libraries. Unzip the libraries file. On the Pico flash drive create a directory called libs and then copy the appropriate library files or folders to the libs directory mentioned earlier. For example for the NeoPixel libraries (used later in this project) copy the file neopixel.mpy to Pico.

Raspberry Pi Pico NeoPixel Circuit

The image below shows a breadboard layout for a Raspberry Pi Pico controlling NeoPixels. This does not include any kind of level-shifter, for more details see: Electronics MOSFET level-shifter circuit designs.

Breadboard circuit Raspberry Pi Pico with NeoPixels controlled with CircuitPython

Controlling NeoPixels with CircuitPython

The code example below gives an example of how CircuitPython works and how it can be used to control NeoPixels. This is a simple example which changes the entire string of NeoPixels at the same time.



"""

NeoPixel example for Pico using CircuitPytyon.

Turns the NeoPixels to different colours.

"""

import board

import neopixel

import time



# Update this to match the number of NeoPixel LEDs connected to your board.

num_pixels = 44

brightness = 0.5



pixels = neopixel.NeoPixel(board.GP22, num_pixels, pixel_order = "GRB")



pixels.brightness = 0.5



while True:

    print ("Red")

    pixels.fill((255, 0, 0))

    time.sleep (10)

    print ("Green")

    pixels.fill((0, 255, 0))

    time.sleep (10)

    print ("Blue")

    pixels.fill((0, 0, 255))

    time.sleep (10)

One point to be aware of is that the GPIO port is references using board (not machine as it is in MicroPython) and the pin is board.GP22. The neopixel library imported needs to be installed on the CircuitPython device as mentioned previously under the install instructions.

Example 2 - Updating individual NeoPixels with CircuitPython

The second example turns all the LEDs off, but then lights up one LED. The position of the lit LED moves from left to right and back again. This code is going to be the basis of my reaction based game.

There are some extra options used on the NeoPixel constructor. The main option is auto_write. By setting this to False, means that the NeoPixels won’t be updated automatically, and instead I need to call pixels.show to actually update the LEDs. This means I can prepare the colour for different LEDs before actually updating the actual NeoPixels.



"""

NeoPixel demo2

"""

import board

import busio

import neopixel

import time





# NeoPixels

neopixel_pin = board.GP22



# NeoPixel info

num_pixels = 44

brightness = 0.5



# Setup neopixels

pixels = neopixel.NeoPixel(neopixel_pin, num_pixels, pixel_order = "GRB", auto_write = False, brightness = 0.5)

# Where the pixel is when scanning

pixel_position = 0

# Is pixel going left to right or right to left

# (Actual depends upon direction of strip) - forward is starting from pixel 0

pixel_forward = True

pixel_delay = 0.5





def main ():

    global pixel_delay, pixel_position, pixel_forward

    

    while True:

        if pixel_forward:

            pixel_position += 1

            if (pixel_position >= num_pixels):

                pixel_position = num_pixels -1

                pixel_forward = not pixel_forward

        else:

            pixel_position -= 1

            if (pixel_position <= 0):

                pixel_position = 0

                pixel_forward = not pixel_forward

        # Light up this pixel

        pixels.fill((0,0,0))

        pixels[pixel_position] = (255,255,255)

        pixels.show()

        if (pixel_delay > 0):

            time.sleep(pixel_delay)





if __name__ == '__main__':

    main()

More information

More information about CircuitPython.

See apractical example with my LED reaction game created in CircuitPython.

See my other programming guides at:

Blog links and videos on programming

Previous Access Flash Memory on Arduino RP2040
Access Flash Memory on Arduino RP2040
Next Badger 2040 drawing circles
Badger 2040 drawing circles