In this tutorial I'll introduce you the Pico:ed and the Smart Cutebot giving also some tips about CircuitPython. Those products are manufactured by ELECFREAKS, a company who produces and sells electronics kits for STEM. We will use those 2 objects together for making a tiny Robot for educational purposes using the CircuitPython programming language.
The Pico:edPico:ed is a small development board (his shape always remembers me about Pac-Man ghosts!) made to be (partially) compatible with the most famous Micro:bit but having a RaspberryPI RP2040 as MicroController Unit (MCU).
You can notice I've used the word partially: you know I like to be accurate so this term is due.
At first I've already written we've a different MCU on the Pico:ed: an RP2040 (Dual Core Cortex-M0+ @133MHz, 2MB flash expandable, 265kB RAM) instead of a Nordic nRF51822 (Cortex-M0 @16MHz, 256kB flash, 16kB RAM) so, numbers talking, the MCU used on the Pico:ed is more capable and powerful. On the Micro:bit Microsoft Makecode is used: this allows a graphical Scratch-like enviroment, Python or Javascript. On the Pico:ed we can use a common RP2040 Python fork (MicroPython or CircuitPython) or C++.
CircuitPython is the best choice since ELECFREAKS and Adafruit cooked a CircuitPython firmware with all libraries we need on this board.In order to contain costs and so make this board available for the most at an accessible price, some features are missing comparing the Pico:ed to the Micro:bit. Those features are the IMU (Inertial Measuring Unit) and the Bluetooth radio.
Little personal consideration: Bluetooth on Micro:bit is a feature of the MCU used since the Nordic nRF51822 is built with an embedded bluetooth module. If you need those features consider you can always add an IMU or a radio module using external boards.
On the other side Pico:ed has other cool things that Micro:bit doesn't. The led matrix display on the Micro:bit is made using 5x5 (25) leds, while on the RP2040 brother we have a 7x17 (119) led matrix obtained with smaller (and more dense) leds; additionally these leds are driven by an IS31FL3731 ic that can PWM independently every single led: this will bring to more accurate and readable scrolling texts and animations and also greyscale images! Finally, on the Pico:ed we've also a cool buzzer for making sound and music.
The edge connector at bottom is in Micro:bit form-factor so this board is compatible with most of Micro:bit boards.
The Smart CutebotThe Smart Cutebot is a tiny robotic platform in a classical configuration: differential drive with a rear pivot point (the ballcaster). This platform is designed to be paired with a Micro:bit or a Pico:ed for making a small Robot that is perfect for kids and educational purposes (or just for learning how to use Python in real-world applications - really you don't need to be a kid for playing with such stuff!).
Cutebot features are:
- Edge connector for Micro:bit or Pico:ed
- 2 fully enclosed gearmotors + rear Ball-Caster
- 2 big (10mm) RGB leds on front
- 2 RGB leds (Neopixel) under the chassis, in front of the wheels
- Detachable HC-SR04 ultrasonic sensor
- 2 IR photoreflector on bottom for Line-Following
- an InfraRed receiver for controlling the robot using a Remote
- expansion headers on front (I2C) and back of the robot for connecting accessories
- Powered using a battery holder for 3xAAA batteries (there is an optional add-on for using a LiPo)
- In the box there is also a paper track (black line on white background) that can be used for the Line Follower mode
Cutebot comes already assembled, you must only insert the microcontroller board into the slot, the ultrasonic sensor and the battery holder. When I published posts of this Robot on Instagram some people contacted me for say that an already mounted board doesn't give that fun coming out from the assembling phase. Yes, that's a right point, but consider that the whole robot weights about 120g (with batteries and microcontroller board) and fits in an hand.
In order to achieve this volume the robot is made using SMD components and would not be easy for in-home or in-school assembling (considering also malfunction due to incorrect operations). As last feature: Cutebot is really quiet! You almost cannot hear it when moving.
Preparing the Pico:edAt first you must upload the CircuitPython interpreter on the Pico:ed board. Take the Pico:ed and keep the Bootsel button pressed. Bootsel button is the one on the left of the micro-USB connector on the back of the board:
While keeping that button pressed, connect the board to computer with the USB cable: wait some seconds and a new storage unit, called RPI-RP2 must appear.
Bonus infoThis is the way all Raspberry Pi RP2040 boards works. The only difference between programming it with a Python fork and C++ is that with Python you upload the python interpreter only the first time and then you can upload your source codes (many of them) as .py files (not in this RPI-RP2 unit but another circuitpython interpreter will create), while programming in C++ you'll compile the code to obtain an .uf2 file to be uploaded in bootloader mode (C/C++ is a compiled language).
Now you must upload the CircuitPython interpreter. You can download if from the official CircuitPython website. Here is the link.
Bonus InfoUsing this official Adafruit Circuitpython interpreter for the Pico:ed you'll not need to upload libraries for using some features such as the led driver IS31FL3731, or the buzzer for example: all you need for this specific board is included. Those features are called FrozenModules : they are libraries already embedded with the CircuitPython interpreter. Included modules are: adafruit_is31fl3731, adafruit_motor, adafruit_ticks, asyncio, elecfreaks_music, neopixel, picoed
Once downloaded the .uf2 file you must simply drag-and-drop that file on the RPI-RPI2 storage unit on your computer. Wait some seconds: the RPI-RPI2 unit will disappear and a new unit, called CIRCUITPY will appear.
At startup (I mean without pressing bootsel button), the .uf2 you uploaded in bootloader mode starts: in this case will be the CircuitPython interpreter that will make the board visible as the CIRCUITPY unit: here you can upload your circuitpython source codes having the .py extension. After the CircuitPython interpreter starts it will search for a particular named file (we will see this later) for making it start, otherways it will do nothing.
Now you can start to writing your codes in Circuitpython language and upload them on the board.
Even if you can do this with a simple text editor (python programs are simple text files) and then upload the file using the drag'n drop, more powerful tools were developed for doing this in a secure way avoiding to corrupt the content of the flash memory (remember that unsecure storage unit removing will damage the code) and using a feature called REPL (Read Evaluate Print Loop) that will allows you to make some debug and start programs you want from the command line.
I said you can upload your .py files on the created memory storage unit, but files won't execute automatically at power-up. If you want to make a program automatically start when you power the board you must call it code.txt or code.py or main.py or main.txt - this is the exact priority sequence. If you put a file called code.py and another called main.py, only the code.py will start automatically. If you name the file differently than this, you can start it using the REPL feature (we will see this soon), so this platform is very useful for storing many software experiments on the same board and for debugging.
I like to use Thonny IDE: it's probably the most used for Python / Python forks and I like the way it works (another similar editor is Mu. Mu is adviced from Adafruit).
Download Thonny (the link I put above is the Github Release page - there you can download a portable version doen't need to be installed, anyway you can go also on the official Thonny page), install/start it; now we'll do some quick settings for making it work with our Pico:ed.
Click on Tools/Options:
Click Interpreter and select CircuitPython (generic) from the list below, then press OK:
Now make the Files and Shell windows visible since those features will be very useful for uploading/downloading file to/from the board and for interact with the REPL. Click on the View menu and then select both Files and Shell:
Thonny settings are all done!
To be sure Pico:ed is connected to Thonny, a writing similar to this must appear in the shell window (connect the board and press the Stop button on Thonny):
If some errors are showed, go to the Troubleshooting paragraph in this tutorial.
Those 3 >>> in the shell are the prompt of the REPL, waiting for commands or ready to write something
Well. Even if the CircuitPython interpreter we've uploaded it's made for using all the Pico:ed features, now we must upload the libraries needed for using the Cutebot features (for driving motors, for using the ultrasonic sensor and so on).
I've said we have already some FrozenModules, yes: this is valid for the specific board. The Smart Cutebot is an accessory board connected to the Pico:ed so there are no frozen modules for this, then we must to add them manually.
Download the cutebot library (it's a single file called cutebot.py) from this link.
That link will download the library in zip format from the Github repository where is located and will be always the most recent upload. The Github Repository for that library is located at this link.
If we want to use the IR remote feature too (note: an IR remote is NOT included in the package) we can download also the Adafruit Circuitpython IR remote library from this link.
Let's unzip files, the only files we need from the downloads are called cutebot.py and adafruit_irremote.py: copy them in a folder on your computer or an external drive so you can find them quickly in the next passages.
We must upload those 2 files in a special folder on the Pico:ed called lib from where libraries will be imported in our code.
From Thonny right click on the circuitpythondevice window and select NewDirectory
Write lib in the next window and then press OK. Now double click on the lib folder so you now are located in that folder on the Pico:ed.
From the above window (Files) where computer content is showed, search the folder where you put cutebot.py and adafruit_irremote.py select them and right-click. Select Upload to /lib
Wait some seconds to allow upload. Now you'll see those libraries in the CircuitPythondevice window. Now you can exit from the lib folder on the device for returning in the root so you can save your programs here: simply click on the CircuitPythondevice writing.
Those particular two libraries we've uploaded are a single file libraries. Will happen that other libraries are made of more files in a folder: in that case you'll upload the entire folder so in the /lib folder there'll be subfloders.
Well. It's all ready. Let's write a simple program to be uploaded on the Pico:ed, just to see all is working correctly.
On Thonny click File and then New. An empty editor window will appear. Copy and paste this code:
from picoed import display
while True:
display.scroll("HACKSTER.IO")
Press the Play button:
Since this file is new, not saved, Thonny will ask you to save it, so a window will appear asking where to save: if on computer or on the CircuitPython device, we will press che CircuitPythonDevice button.
Then you must enter a name: write scroll.py and then ok
The program will start showing the writing HACKSTER.IO on the led matrix.
Simple, don't?
We can press the Save button for saving the file without starting it. Let's point some things out: - A very common error is to not put the .py extension at the end of the file: even if pressing the play button file will be executed anyway, the syntax highlight in the editor won't work.- If you want the file start automatically on power on, you must call it as I said above: code.py or main.py for example, in all other cases you can make the program start pressing the Play button from Thonny after you wrote it or loaded (double click on the.py file) in the editor
Well. Now It's time to assemble the Cutebot.
Preparing the Smart CutebotHere there are few things to do:
Insert the Ultrasonic sensor. There is a 4x2 female header on front (the front of the robot is where the two RGB leds are located. Ultrasonic sensor header has to be inserted on the most-front holes:
Put the self-adhesive tape on the back of the battery holder
Connect the battery holder connector in his socket at centre of the robot, pass cables between motors and then stick the battery holder on the motors enclosure having care to remain clearance from the Pico:ed slot: better if you align the center of the battery holder with wheels axis as in the pictures:
Assure ON/OFF switch is in the most-right position (OFF) and then put 3xAAA batteries (not included) in the battery holder.
Insert the Pico:ed in his slot facing LED matrix on front
Assembly is done! Let's make a first program for testing the linefollower
Programming examplesOn ELECFREAKS wiki there is a step-by-step tutorial for using the Smart Cutebot with the Pico:ed. Evey lesson teaches how to use one element per time. I advice you to follow their tutorial first and then try my Line Follower example below.
Line FollowerI wrote my own line-follower program trying to put into it most of the features of the Pico:ed and Cutebot: I used the buzzer, the dot matrix display and RGB leds on front.
This line follower programs works in this way:
- Once turned on the robot, wait for the 'excited' face appearing on the dot-matrix display
- Put the robot on the paper track taking care both small smd blue leds on front are turned on indicating the black track is just under the IR sensors
- Press the 'A' button on Pico:ed (it's the one on your left having the robot face in front of you)
- After pressed the button, frontal BIG leds will flash in blue and white and a sound will be played resembling a police car
- Robot starts to move following the track. Big leds on front turns green. Moving in a straight line a smile appears on the dot matrix display (face). When robot turns, the face depicted on the display looks right or left depending the direction
- If Robot goes out of the track, leds turns red, face changes and robot turns in circle trying to recover the line
- If you press 'B' button, Robot stops, leds turns red e face changes. Press 'A' button to restart
You can download my example in my Cutebot Github Repository, here.
Here are some notes and tips:
- In my example, robot runs slower than the official ELECFREAKS example. This is a wanted thing since I'm using also the dotmatrix display and even if one can't notice it, there is some delay while updating the dotmatrix display and this leads to line tracking operations delays making robot go out of track continuously, so the quickest solution was making robot move slowly.
- Using the ultrasonic sensor module along line tracking is challenging since ultrasonic module requires time and cannot be called continuously (you must allow at least 500mS between measurements) so you cannot simply put the distance reading in the tracking loop otherwise strange errors will happen making robot stuck in an infinite cycle. The best way imho is to use a separate/async task for doing this (this is a pro challenge for you).
- 2 neopixel under the chassis can be managed using the Adafruit Neopixel library for CircuitPython (it's a frozen module).
Sometimes you'll notice Thonny IDE get stuck and won't find the board giving error messages. In this case you must try to (from first thing to do to last):
- Press the STOP button
- Detach and Re-attach the USB cable, Wait 10 seconds and then press the STOP button
- Close and restart Thonny IDE repeating all above
- On old Microsoft Windows versions sometimes the CIRCUITPY unit is visible in the computer's device even if is not attached: in this case you must only restart the PC. I have always this malfunction on Windows 7 while this doens't happens on Windows 10 and 11. Anyway better to safe disconnect the Circuitpy unit after code uploading.
In other cases you can see your main.py won't execute: this happens when there are errors in code that stuck the python interpreter leading to an infinite loop. In this case attach the board to the computer, open thonny, load the program and execute the program pressing the play button so you can read the errors in the terminal window.
In other cases you simply named the file differently than main.py or code.py but you want anyway it will self-start at power on: it cannot!
Very common programming errors:
- Python works on indentations (no curled brackets!), so check your indentation please
- Error I make often is not to put the colons after the IF statements. I really make this mistake always so I put a sticky note on my monitor saying: THE COLONS AFTER THE IF!
- Variables declared outside functions are global, but if you want to use them in function you must use the keyword global otherwise you'll have a not previously declared variable error or something similar
Comments