How to Create CircuitPython Projects on a Chromebook

0

There is no denying that the Raspberry Pi Pico had a big impact in 2021. The $4 microcontroller was quickly adopted by many communities and maker groups and one of them was CircuitPython. CircuitPython, a fork of MicroPython which is a version of Python 3 for microcontrollers, is backed by Adafruit which is one of the partners who launched their own line of RP2040 boards. Since its inception in 2018, CircuitPython has seen massive community investment, with many projects and libraries focused on the language.

CircuitPython is the best way to introduce Python to microcontrollers because it’s easy to use with devices that appear in the host operating system as USB drives. We write our code in the code.py file and we can activate external sensors, displays and inputs through a dazzling array of libraries.

So how can we work with the Raspberry Pi Pico and CircuitPython? Typically, we connect our Pico to a PC and start writing code, but we can also create projects with a Chromebook or other Chrome OS device. With an easy-to-use programming language, an inexpensive microcontroller, and an easy-to-use operating system, we have the perfect platform for creativity no matter our age or ability.

In this guide, we’ll show you how to set up your Raspberry Pi Pico for CircuitPython, install the software to write code and communicate with your Pico, and finally we’ll build a temperature sensor project.

For this project you will need

Install CircuitPython on the Raspberry Pi Pico

The Raspberry Pi Pico can run multiple different languageseven a version of a Language over 50 years old. We focus on CircuitPython, and installing CircuitPython on a Pico is very easy to do.

1. Download the latest version of CircuitPython for the Raspberry Pi Pico. At the time of writing, the latest version was 7.3.0.

(Image credit: Tom’s Hardware)

2. Press and hold the BOOTSEL button on the Raspberry Pi Pico, then connect your Pico to the Chromebook via USB cable.

3. Copy the downloaded UF2 file from Downloads to the RPI-RP2 drive in Files. The process will take less than a minute and when complete, a new CIRCUITPY player will appear in the file browser.

Ideally, we would have a single application that could do everything, but for now, alas, we have to install two. The first is a text editor, Caret, which we can use to write CircuitPython code directly on the CIRCUITPY player. The second is Beagle Term, a serial terminal emulator for Chrome OS.

1. Install Caret Text Editor through the Chrome Web Store.

(Image credit: Tom’s Hardware)

2. Install the term Beagle through the Chrome Web Store.

(Image credit: Tom’s Hardware)

Write our CircuitPython Code test on Chromebook

(Image credit: Tom’s Hardware)

Before we start any electronics, let’s check that we can control and communicate with the Raspberry Pi Pico. Our demo code is a simple Hello World that prints to the Python shell every second.

1. Open the Caret text editor using the Search key (the spyglass where Caps Lock normally is) and type Caret. Press Enter to open.

(Image credit: Tom’s Hardware)

2. Click File >> Open and select code.py found on your CIRCUITPY drive. This drive is your Raspberry Pi Pico running CircuitPython.

3. Delete any code in the file.

4. Import the time module. We’ll use this to control how fast our code loops.

import time

5. Create a while loop True to continuously run the code inside.

while True:

6. Use a print function to print “Hello World”. Note that the code is indented by pressing the TAB key or four spaces. Don’t mix tabs and spaces because Python will throw an error. This is a classic code test example. It is a simple way that we can use to confirm that we have control and communication with a device.

   print(“Hello World”)

seven. Add a one second pause

This gives us a second to read the Hello World message.

   time.sleep(1)

8. Save your code to code.py. CircuitPython will automatically run the code every time we save.

List of test codes

import time
while True:
    print(“Hello World”)
    time.sleep(1)

Using the Beagle Term Serial Emulator

(Image credit: Tom’s Hardware)

To see the output of our code, we need to run the Beagle Term serial terminal emulator. This app will connect to the Python console, running over a USB to serial connection. CircuitPython has a Python REPL (Read, Eval, Print, Loop) console that can be used to work interactively with the board and can send information directly to the console.

1. Open the Beagle Term app using the Search key (the spyglass where Caps Lock normally is) and type Caret. Press Enter to open.

2. Set your port on your Raspberry Pi Pico. It’s a bit of a trial and error approach as we don’t know the name of the port. In our tests, we saw /dev/ttyACM1 for our Raspberry Pi Pico.

3. Set the bit rate to 9600. This is the speed at which our Chrome OS device and Raspberry Pi Pico will communicate.

(Image credit: Tom’s Hardware)

4. Set data bit to 8 bit, parity to none, stop bit to 1 bit, and finally, set flow control to none.

5. Click Connect to start the serial connection to your Raspberry Pi Pico. Hello World should scroll down the screen. If not, press CTRL+C to stop code execution, then press CTRL+D to restart the code.

Build a temperature sensor project

The DHT22 has four pins, but we’ll only need to use three. The sensor is relatively easy to use. It needs 3V to power it, and the data output pin is pulled high using a 10K Ohm resistor. By pulling the pin up, we make sure that the data from the sensor is read, because the pin is always “on”.

1. Insert your Raspberry Pi Pico into the breadboard so the microUSB port is on the left side of the board.

(Image credit: Tom’s Hardware)

2. Run a jumper wire from the 3V3 output (red wire) to the + rail of the breadboard. This provides a 3.3V pin connection to the entire + rail. Run another wire from the GND pin to the – rail.

(Image credit: Tom’s Hardware)

3. Insert the DHT22 into the breadboard.

(Image credit: Tom’s Hardware)

4. Connect 3.3V (red wire) and GND (black wire) to pins 1 and 4 of the DHT22. The pin numbers go from left to right, when we face the plastic “frame” of the sensor.

(Image credit: Tom’s Hardware)

5. Connect the 3.3V rail to pin 2 of the DHT22 using a 10K Ohm resistor. This is our pull-up resistor for the data pin.

(Image credit: Tom’s Hardware)

6. Connect the data pin (pin 2) of the DHT22 to the GP15 of the Raspberry Pi Pico. Your breadboard should look like this.

(Image credit: Tom’s Hardware)

Project coding

1. Download CircuitPython Libraries Bundle for your version of CircuitPython.

2. Navigate to your Downloads folder. Right click and extract the ZIP file.

3. Open the adafruit-circuitpython-bundle folder and navigate to the library subfolder.

(Image credit: Tom’s Hardware)

4. Copy adfafruit_dht.mpy to the /lib/ folder of your CIRCUITPY drive. This library allows our Raspberry Pi Pico to work with DHT11 and DHT22 sensors.

(Image credit: Tom’s Hardware)

5. Open code.py on your Raspberry Pi Pico using the Caret text editor. Delete the test code from the file.

6. Import three modules to allow our code to access the GPIO (board), use the temperature sensor (adafruit_dht), and control the loop speed (time).

import board
import adafruit_dht
import time

seven. Create an object, dht to connect the code to the DHT22 sensor connected in GP15 on the Raspberry Pi Pico.

dht = adafruit_dht.DHT22(board.GP15)

8. Create a loop to run the code.

while True:

9. Stores the current temperature in a variable called temp. The temperature is recorded in degrees Celsius.

   temp = dht.temperature

ten. Create an object, text and store a string in it that will print “The temperature is Celsius”. You will notice {:>8}, this is a string format that will include eight spaces in the middle of the sentence. This gives us a free space to place the temperature data.

   text = "The temperature is {:>8} Celsius."

11. Use print function with text formatt to insert the temperature data into space.

   print(text.format(temp))

12. Finally add a one-second pause. This allows the loop to repeat slowly.

   time.sleep(1)

13. Save the project in code.py on your CIRCUITPY player.

14. Open Beagle Term and connect to your Raspberry Pi Pico. You should see the temperature data scroll across the screen. Otherwise, press CTRL+C, then CTRL+D to restart the code.

(Image credit: Tom’s Hardware)

Complete list of codes

import board
import adafruit_dht
import time
dht = adafruit_dht.DHT22(board.GP15)

while True:
    temp = dht.temperature
    text = "The temperature is {:>8} Celsius."
    print(text.format(temp))
    time.sleep(1)

Share.

About Author

Comments are closed.