Before we get started with the Project, it is necessary to know the details of the Hardware. Slowly, we'll move ahead and get you started with Pico with all required instructions
The Raspberry Pi Pico is the first microcontroller board based on the RP2040. It looks a lot like other microcontroller boards with the MCU in the center, a micro-USB connector on one end, and a row of contacts along each side.
Raspberry Pi Pico Pinout
This is a top view of the pinouts on the Raspberry Pi Pico. The pin labels are on the bottom of the board.
There are 40-pin on the Raspberry Pi Pico. Out of those 40 pins, 26 pins are Input-Output (IO Pins). All those 14 pins are analog, digital, and other Serial Pins.
Install MicroPython on Raspberry Pi Pico1. Press and hold the BOOTSEL button on the Pico, and immediately connect the Pico Board to your computer using a micro USB cable. Release BOOTSEL once the drive RPI-RP2 appears on your computer. (When the Pico is connected to the PC, the BOOTSEL must be PRESSED) - This opens the Flash Memory of the Pico as a Drive on the PC.
2. Open the RPI-RP2 drive that appears in the Drives tab.
3. Visit the Raspberry Pi Pico Official Documentation page from here: Raspberry Pi Documentation
4. Download the MicroPython UF2 file from this link
5. Drag and drop the UF2 file onto the RPI - RP2 drive. The Raspberry Pi Pico with RP will reboot and run MicroPython.Raspberry Pi has provided similar steps already, in the Micropython Documentation
QuickRecap -
Now, that we have our Pico Ready to be programmed with micropython, let's get started. To program the Raspberry Pi Pico using Micropython, you can either use:
1. Thonny IDE
2. VisualStudio IDE-ampy(module)- Eg: Visual Studio
Pico using MicroPython on Thonny IDE1. First, you have to download Thonny from the https://thonny.org/
2. Connect the Raspberry Pi Pico to your computer. Then from Thonny go to Tools > Options and click on the Interpreter tab.
From the interpreter dropdown list, select MicroPython (Raspberry Pi Pico). The port dropdown menu can be left to automatically detect the Pico. Click Ok to close.
3. When you plugin the pico Board, a firmware installation tab will appear for raspberry pi pico. Click on Install & some files will be downloaded.
4. After successful installation, the MicroPython version and Raspberry board will appear in the Python Shell.
To test we can write a quick print function to say “Hello World”. Press Enter to run the code. You will get Hello World as a response.
The onboard LED on the Raspberry Pi Pico is connected to GPIO25. Copy the following code and paste it on the editor window.
from machine import Pin
import utime
led = Pin(25, Pin.OUT)
led.value(0)
while True:
led.value(1)
print("On")
utime.sleep(1)
led.value(0)
print("Off")
utime.sleep(1)
Save the program on the computer and give a unique name like blink.py
The program will run and you can see the LED toggling in the board
Now unplug the USB of Raspberry Pi Pico and plug back in again. You will see the LED isn’t blinking. This is because the program is saved in the computer, not in the Raspberry Pi Pico Board.
To make the system work even after plugging and unplugging the USB cable, you need to save the program to the Pico Board. To do that open a new tab and paste the same program and click on save. This time save it on the Pico Board.
While saving give it a name main.py as shown in the image below.
This time the LED will blink even after removing from the USB port of PC (plug it to any external power adapter). This is because the program is saved in the Pi Board.
Pico using MicroPython on Visual Studio Code1. First, you have to download and Install VS Code from https://code.visualstudio.com/download
2. Open VSCode > Extensions > Search "Python" > Install the Python - IntelliSense (Pylance) Extension.
3. Go to Terminal > New Terminal. This should open the terminal of your VSCode from whichever folder you have opened.
To be precise, you can store all your programs ina particular folder and easily access it from the VS Code.
For that, simply go to File > Open Folder. Select/Create the folder you want to store all the programs. It makes it easier to access the files as well.
Now, that you have your terminal open, it should look somewhat like this on your terminal.
On the terminal, type the below command to install adafruit-ampy.
>pip install adafruit-ampy
Incase of an OSError on your terminal, use the below command (user access)
>pip install --user adafruit-ampy
Ampy is a Micropython tool, to be able to write programs on python, for Micropython based boards. So, we can write similar codes on python and helps us with integrating with the vast collection of open-source python libraries.
(VS Code) Blink program for the onboard LED of PicoCreate a new file 'blink.py' > Paste the below code
from machine import Pin
import time
led = Pin(25, Pin.OUT)
led.value(0)
while True:
led.value(1)
print("On")
time.sleep(1)
led.value(0)
print("Off")
time.sleep(1)
This code defines an 'led' variable to the GPIO25 pin. Similar to the code on Thonny IDE. Notice the difference in the 'time' module for thonny's micropython and general python module.
5. To run the code, we require the PORT our Pico is connected with.
Search and Open 'Device Manager'.
Go to 'Ports' and then Plug-In the Pico. You'll see a new port added with 'USB Serial Device' (most cases). In my case, the port my Pico is connected with, to my laptop is COM4.
6. Run the Code by entering the below command on the terminal.
>ampy --port COM4 run blink.py
This command will run the python file on Micropython. And you'll see similar results as on Thonny IDE.
In case you have multiple python39 directories on your device (Windows User), you may get an error - 'ampy : The term 'ampy' is not recognized as the name of a cmdlet, function, script file, or operable program'.
Make sure to find out in which directory adafruit-ampy's ampy.exe is present. (during installation the directory is mentioned as 'Python39\site-packages' directory). You can find the ampy.exe in the 'Python39\Scripts' directory.
Then you can run the code as below
>& "path\Python39\Scripts\ampy.exe" -p COM4 run blink.py
Now that you are able to Run Micropython both from a Terminal and Thonny IDE, you can write your own codes to gather data or control the pins.
Project - 1 (Control Pico's inbuilt LED from Bolt IoT Cloud)In this project, I'll write a code for Pico that reads the serial interface. Whenever there is an 'ON' or 'OFF' message on the interface, it executes and controls the GPIO25.
1. Here, we use the Bolt API for SerialWrite and send data 'ON' and 'OFF' to the Bolt Module. This means, that the UART pins (RX and TX) of the module executes and serial write through those pins. And on the other side, RX of Bolt is connected with TX of Pico. Make the connections as mentioned below.
(Bolt) RX -> TX (Pico)(Bolt) TX -> RX (Pico)
In our case, we'll be using software serial, which means, Pico can use any of its pins for UART communication. Therefore, I used PIN 0 as TX and PIN 1 as RX.
Connection as per circuit diagram -
2. Now, that we have the connection ready, below is the Code -
#From Pico - Serial.write("1")
#From Bolt - Serial Write, value = b'1'
from machine import UART, Pin
import time
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
led = Pin(25, Pin.OUT)
time.sleep(0.1)
on="ON"
off="OFF"
led.value(0)
rxData = bytes()
print("Controlling Inbuild LED")
while True:
rxData = uart.read()
#print(rxData)
print(".")
if rxData:
data = rxData.decode('utf-8')
#print(data)
if data == on:
print("ON")
led.value(1)
if data == off:
print("OFF")
led.value(0)
time.sleep(1)
3. On the Bolt IoT's API section > Select SerialBegin > Build API Request > Test API
This initializes the Serial Communication Baudrate of 9600 with the Pico Board.
4. On the API section, Select Serial Write. Enter the Command you want to send to the Pico Board. In our case, it could be 'ON' or 'OFF' to control the LED.
When clicking on TEST API, it will send the data to the Pico board. This can be seen on the Terminal of the VS Code as well.
Repeating the same by sending 'ON' value, turns ON the LED on the Pico.
You can use the below code on the Bolt Products to create 2 buttons to control the Pico.
Select GPIO and Output devices on the Initial setup.
Keep the Hardware Config empty (default), and in the Code (extension - html) section -
<!DOCTYPE html>
<html>
<head>
<title>Bolt IoT Platform</title>
<script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltCommands.js"></script>
<script>
setKey('{{ApiKey}}','{{Name}}');
</script>
</head>
<body>
<center>
<button onclick="serialWrite('ON');">ON</button>
<br>
<br>
<button onclick="serialWrite('OFF');">OFF</button>
</center>
</body>
</html>
Now, let's go ahead and control multiple external LEDs.
Project - 2 (Control LEDs with Pico from Bolt IoT Cloud)1. In this project, we'll send '0', '1' and'2' messages on the interface, it executes and to turn OFF both, turn ON GPIO3 and GPIO8 respectively, on Pico Board from the Bolt Cloud.
Below is a Circuit Diagram for the same.
1. Below is the code -
from machine import UART, Pin
import time
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
led1 = Pin(3, Pin.OUT)
led2 = Pin(8, Pin.OUT)
time.sleep(0.1)
rxData=str()
a="1"
b="2"
c="0"
led1.value(0)
led2.value(0)
rxData = bytes()
print("Controls 1 - GPIO3 | 2 - GPIO8 | 0 - Off Both")
while True:
rxData = uart.read(1)
#print(rxData)
print(".")
if rxData:
data = rxData.decode('utf-8')
#data = rxData
#print(data)
if data == a:
print("RED")
led1.value(1)
led2.value(0)
if data == b:
print("GREEN")
led1.value(0)
led2.value(1)
if data == c:
print("OFF")
led1.value(0)
led2.value(0)
time.sleep(0.5)
2. On the Bolt's Serial Write Interface -
Enter '1' to turn ON GPIO3 - '2' to turn ON GPIO8 - '0' to turn OFF both
You can use the below code on the Bolt Products to create 3 buttons to control the Pico.
Select GPIO and Output devices on the Initial setup.
Keep the Hardware Config empty (default), and in the Code (extension - html) section -
<!DOCTYPE html>
<html>
<head>
<title>Bolt IoT Platform</title>
<script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltCommands.js"></script>
<script>
setKey('{{ApiKey}}','{{Name}}');
</script>
</head>
<body>
<center>
<button onclick="serialWrite('1');">LED - 1</button>
<br>
<br>
<button onclick="serialWrite('2');">LED - 2</button>
<br>
<br>
<button onclick="serialWrite('0');">OFF both</button>
<br>
<br>
</center>
</body>
</html>
Great! We have successfully controlled the Pico Board from the Bolt Serial. But How to Get the Data from the Pico Board and use it to view on our Bolt's Graph and Dashboard?
Do not Worry! I have everything ready for you!
In this project, I'll write a code for Pico that reads analog data from its ADC pin GPIO26 - GPIO27 and displays the same data on Bolt Cloud as a Graph.
1. Here, we wait for a response of 'RD\r' from Bolt Cloud on the Serial Interface (as per https://github.com/Inventrom/boltiot-arduino-helper), as the Bolt Graph sends this when using UART to share data with Cloud. After this, I am using the Pico to send data to the Bolt module on the same interface.
In our case, from Pico the data printed on the Serial Interface is somewhat like 'val1, val2\n'. Where val1 and val2 are the analog data from the Pico Board. We can attach any kind of sensor on pins 26 and 27 to send multiple analog data to the Bolt Cloud from the Pico.
2. Below is the code for the same -
from machine import UART, Pin
import time
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
analog1 = machine.ADC(26)
analog2 = machine.ADC(27)
time.sleep(0.1)
read="R"
rxData = bytes()
def map(sensor_val, in_from, in_to, out_from, out_to):
out_range = out_to - out_from
in_range = in_to - in_from
in_val = sensor_val - in_from
val=(float(in_val)/in_range)*out_range
out_val = out_from+val
return out_val
print("Reading Analog Inputs")
while True:
rxData = uart0.read()
#print(rxData)
print(".")
if rxData:
analog1 = machine.ADC(26)
analog2 = machine.ADC(27)
data = rxData.decode('ascii')
#print(data)
if data.startswith(read):
print("DATA Sent")
val1 = analog1.read_u16()
val2 = analog2.read_u16()
#print(analog)
val1 = map(val1, 0, 65535, 0, 1023)#Convert 2^16 bit data to 2^10 bit
val2 = map(val2, 0, 65535, 0, 1023)
#print(val1)
#print(val2)
send = str(val1) + "," + str(val2) + "\n"
print(send)
uart0.write(send)
send = ""
time.sleep(1)
Now, let us set up the Bolt Product to display the graphs for the data received from the Pico board.
During creating a New Product, select UART as the mode to collect data.
In Hardware Config, make the below configurations -
In code, you can write the below code (extension - js) to view Multiple Graphs -
var lineGraph = new boltGraph();
lineGraph.setChartType("lineGraph");
lineGraph.setAxisName('Time','Analog 1');
lineGraph.plotChart('time_stamp','val1');
var barGraph = new boltGraph();
barGraph.setChartType("barGraph");
barGraph.setAxisName('Time','Analog 2');
barGraph.plotChart('time_stamp','val2');
Now, let's Run the Code -
When we View the Bolt Device, we see the Graph forming. Below is a snap of the data after 1 hour of analog values (random) -
Now you have a brief Idea on How to use Raspberry Pico to send and get data from Bolt IoT Module. Indirectly making your Pico part of an IoT System.
Your limitation at this point is the knowledge of MicroPython. Refer to this documentation - https://docs.micropython.org/en/latest/rp2/quickref.html - It has a list of Commands you can use along with your Pico Board.
Comments
Please log in or sign up to comment.