There isn’t any denying that the Raspberry Pi Pico made a huge impact again in 2021. The $4 microcontroller was shortly adopted by most of the maker communities and teams and a kind of was CircuitPython. CircuitPython, a fork of MicroPython which is a model of Python 3 for microcontrollers, is backed by Adafruit who’re one of many companions that launched its personal RP2040 vary of boards. Since its creation again in 2018, CircuitPython has seen a large funding from the group, with many initiatives and libraries being ported to the language.
CircuitPython is the most effective means to introduce Python on microcontrollers, as a result of it’s straightforward to make use of with gadgets showing within the host OS as USB flash drives. We write our code into the code.py file and we will allow exterior sensors, screens and inputs through a stunning array of libraries.
So how can we work with the Raspberry Pi Pico and CircuitPython? Sometimes we join up our Pico to a PC and begin writing code, however we will additionally construct initiatives with a Chromebook or different Chrome OS machine. With an easy-to-use programming language, low-cost microcontroller and an easy-to-use OS we now have the proper platform for creativity, regardless of our age or capacity.
On this tips on how to we’ll present you tips on how to arrange your Raspberry Pi Pico for CircuitPython, set up the software program to jot down code and talk together with your Pico, and eventually we will construct a temperature sensor mission.
For this mission you will want
Set up CircuitPython onto the Raspberry Pi Pico
The Raspberry Pi Pico can run many totally different languages, even a model of a 50+ 12 months previous language. Our focus is on CircuitPython, and putting in CircuitPython on a Pico is tremendous straightforward to do.
1. Obtain the most recent model of CircuitPython for the Raspberry Pi Pico. On the time of writing the most recent model was 7.3.0.
2. Press and maintain the BOOTSEL button on the Raspberry Pi Pico, then join your Pico to the Chromebook through a USB cable.
3. Copy the downloaded UF2 file from Downloads to the RPI-RP2 drive in information. The method will take underneath a minute and when full a brand new drive CIRCUITPY will seem within the Recordsdata browser.
Ideally we’d have only one software which may do every thing, however for now, alas we have to set up two. The primary is a textual content editor, Caret, which we will use to jot down CircuitPython code on to the CIRCUITPY drive. The second is Beagle Time period, a serial terminal emulator for Chrome OS.
1. Set up the Caret Textual content Editor through the Chrome Internet Retailer.
2. Set up Beagle Time period through the Chrome Internet Retailer.
Writing our Take a look at CircuitPython Code on Chromebook
Earlier than we begin any electronics, let’s test that we will management and talk with the Raspberry Pi Pico. Our demo code is a straightforward Hi there World that prints to the Python shell each second.
1. Open the Caret textual content editor utilizing the Search key (the spyglass the place caps lock usually resides) and kind Caret. Press Enter to open.
2. Click on on File >> Open and choose code.py discovered in your CIRCUITPY drive. This drive is your Raspberry Pi Pico working CircuitPython.
3. Delete any code within the file.
4. Import the time module. We’ll use this to manage the velocity at which our code loops.
5. Create some time True loop to repeatedly run the code inside.
6. Use a print perform to print “Hi there World”. Observe that the code is indented by one TAB keypress or 4 areas. Don’t combine tabs and areas as Python will throw an error. It is a traditional instance of testing code. It’s a easy signifies that we will use to verify that we now have management of and communication with a tool.
print(“Hi there World”)
7. Add a one second pause
This provides us a second to learn the Hi there World message.
8. Save your code to code.py. CircuitPython will mechanically run the code every time we save.
Take a look at Code Itemizing
import time whereas True: print(“Hi there World”) time.sleep(1)
Utilizing the Beagle Time period Serial Emulator
To see the output of our code we have to run the Beagle Time period serial terminal emulator. This app will connect with the Python console, working over a USB to serial connection. CircuitPython has a REPL (Learn, Eval, Print, Loop) Python console which can be utilized to interactively work with the board and it could possibly output data on to the console.
1. Open the Beagle Time period software utilizing the Search key (the spyglass the place caps lock usually resides) and kind Caret. Press Enter to open.
2. Set your Port to your Raspberry Pi Pico. It is a little little bit of a trial and error method as we have no idea the title of the port. In our checks we noticed /dev/ttyACM1 for our Raspberry Pi Pico.
3. Set the Bitrate to 9600. That is the velocity at which our Chrome OS machine and Raspberry Pi Pico will talk.
4. Set the Knowledge Bit to eight bit, Parity to none, Cease Bit to 1 bit, and lastly set Move Management to none.
5. Click on Join to start out the serial connection to your Raspberry Pi Pico. Hi there World needs to be scrolling down the display. If not, press CTRL+C to cease the code working, after which press CTRL+D to restart the code.
Constructing a Temperature Sensor Challenge
The DHT22 has 4 pins, however we will solely want to make use of three of them. The sensor is comparatively straightforward to work with. It wants 3V to energy it, and the information output pin is pulled excessive utilizing a 10K Ohm resistor. By pulling the pin excessive we make sure that the information from the sensor is learn, because the pin is at all times “on”.
1. Insert your Raspberry Pi Pico into the breadboard in order that the microUSB port is on the left aspect of the board.
2. Run a jumper wire from the 3V3 out (pink wire) to the + rail of the breadboard. This gives a connection to the three.3V pin to your entire + rail. Run one other wire from the GND pin to the – rail.
3. Insert the DHT22 into the breadboard.
4. Join 3.3V (pink wire) and GND (black wire) to pins 1 and 4 of the DHT22. The pin numbers go from left to proper, as we face the plastic “body” of the sensor.
5. Join the three.3V rail to pin 2 of the DHT22 utilizing a 10K Ohm resistor. That is our pull-up resistor for the information pin.
6. Join the information pin (pin 2) of the DHT22 to GP15 of the Raspberry Pi Pico. Your breadboard ought to seem like this.
Coding the Challenge
1. Obtain the CircuitPython Libraries bundle on your model of CircuitPython.
2. Go to your Downloads folder. Proper click on and extract the ZIP file.
3. Open the adafruit-circuitpython-bundle folder and navigate to the lib sub-folder.
4. Copy adfafruit_dht.mpy to the /lib/ folder of your CIRCUITPY drive. This library permits our Raspberry Pi Pico to work with the DHT11 and DHT22 sensors.
5. Open code.py in your Raspberry Pi Pico utilizing the Caret textual content editor. Delete the take a look at code from the file.
6. Import three modules to allow our code to entry the GPIO (board), use the temperature sensor(adafruit_dht) and to manage the velocity of the loop (time).
import board import adafruit_dht import time
7. Create an object, dht to attach the code to the DHT22 sensor related to GP15 on the Raspberry Pi Pico.
dht = adafruit_dht.DHT22(board.GP15)
8. Create a loop to run the code.
9. Retailer the present temperature in a variable known as temp. The temperature is saved in Celsius.
temp = dht.temperature
10. Create an object, textual content and in there retailer a string which can print “The temperature is Celsius”. You’ll discover :>8, it is a string format which can embrace eight areas within the heart of the sentence. This provides us a transparent area to position the temperature knowledge.
textual content = "The temperature is :>8 Celsius."
11. Use the print perform together with the textual content format to insert the temperature knowledge into the hole.
12. Lastly add a one second pause. This permits the loop to slowly repeat.
13. Save the mission to code.py in your CIRCUITPY drive.
14. Open Beagle Time period and connect with your Raspberry Pi Pico. You need to see the temperature knowledge scroll throughout the display. If not, press CTRL + C, then CTRL+D to restart the code.
Full Code Itemizing
import board import adafruit_dht import time dht = adafruit_dht.DHT22(board.GP15) whereas True: temp = dht.temperature textual content = "The temperature is :>8 Celsius." print(textual content.format(temp)) time.sleep(1)