Projects

Playing with Pico

Raspberry Pi Pico Powered Temperature Display

Luke Prior

Issue 44, March 2021

This article includes additional downloadable resources.
Please log in to access.

Log in

We take a look at the Raspberry Pi Pico and put it to the test with an easy to build desktop weather station.

BUILD TIME: 90 MINS (+3D PRINT TIME)
DIFFICULTY RATING: INTERMEDIATE

The Raspberry Pi Foundation is most well known for its lineup of single-board computers; however, recently the foundation entered the microcontroller market with the launch of their Raspberry Pi Pico.

We just had to get our hands on a Pico and put it to the test. Instead of doing a simple hands-on review, we thought we would go an extra step and present a project that you can easily build for yourself.

We will be using MicroPython and the Thonny IDE to create a basic program that displays the output from the RP2040’s integrated temperature sensor on a SparkFun 16x2 SerLCD display over I2C.

A CLOSER LOOK AT THE PICO

The Raspberry Pi Pico is a tiny, fast, cheap microprocessor featuring the brand new dual-core Arm RP2040 chip designed specifically for the Pico.

The Raspberry Pi Pico is the Raspberry Pi Foundations’ answer to Arduino, Teensy, Espressif, and similar microcontrollers. While existing Raspberry Pi computers run an operating system like Raspbian and are best suited for advanced software projects, the Raspberry Pi Pico is programmed directly, simplifying hardware projects.

Image Credit: Pi Foundation

The Raspberry Pi Pico can be had for under $6 Aussie from our friends at Core Electronics, which makes it one of the cheapest locally available microcontrollers. This is significantly cheaper than a Raspberry Pi 4 and enables a wide variety of projects. The benefits of the Pico extend beyond cost with a greatly reduced power draw and physical footprint allowing it to be deployed in remote and difficult locations.

The Raspberry Pi Pico uses the new RP2040 microcontroller chip featuring dual Arm Cortex-M0+ cores with a flexible clock running up to 133MHz. The Pico is paired with 264KB on-chip SRAM and 2MB of onboard QSPI Flash memory. The most impressive feature of the new chip would have to be the extensive I/O including the usual I2C, and SPI along with 8 Programmable I/O (PIO) state machines for custom peripheral support.

The board measures just 21 × 51mm and includes a micro-USB connector for programming and power. For projects, the Raspberry Pi Pico exposes 26 of the 30 available multifunction GPIO pins on the RP2040, including 3 of the 4 analog inputs. These pins are available as 0.1”-pitch pads with castellated edges. The RP2040 processor also includes an integrated temperature sensor which we will be using for our first-look.

The Pico features a controllable LED and a single push button which is used to enter USB mass-storage mode when booting, but doubles as a general input for use in programs. The board also features a power supply chip supporting input voltages from 1.8V - 5.5V, allowing for a wide range of power options.

The Raspberry Pi Foundation is well known for the extensive documentation of its products and the new Raspberry Pi Pico with RP2040 processor is no exception. The Foundation has published hundreds of pages covering hardware and software specifications. The Pi Foundation has also guaranteed production until January 2028 which will provide certainty to people looking to integrate the Pico or RP2040 in commercial applications.

SPECIFICATIONS

FORM FACTOR: 21 x 51mm
MICROCONTROLLER: RP2040 by Raspberry Pi
PROCESSOR: Dual-core Arm Cortex-M0+
CLOCK SPEED: Flexible clock running up to 133MHz
SRAM: 264KB on-chip
QSPI FLASH: 2MB on-board
GPIO: 26 multifunction GPIO pins, including 3 analogue inputs
UART: 2
SPI CONTROLLERS: 2
I2C CONTROLLERS: 2
PWM CHANNELS: 16
USB CONTROLLER: USB 1.1 & PHY,with host and device support
PROGRAMMABLE I/O (PIO): 8 for custom peripheral support
INPUT POWER: 1.8 – 5.5V DC
OPERATING TEMP.: 20°C to +85°C

The Raspberry Pi Pico is sold on reels of 400 to Approved Resellers such as Core Electronics who can then offer variants with pins soldered on like the one we are looking at. Supply is constrained (at time of print) so customers are limited to purchasing a single Pico at some retailers, as was the case when the Raspberry Pi Zero launched.

IMAGE CREDIT: Pi Foundation

When it comes to programming the Raspberry Pi Pico, the easiest option is MicroPython with the Thonny IDE. The Pi Foundation has also included a C SDK, GCC-based toolchain, and Visual Studio Code integration for advanced users. The Pico has even received a port of Google’s TensorFlow Lite for machine-learning applications.

We will be using MicroPython and the Thonny IDE on Windows 10 for the purposes of this review. We will be creating a basic program that displays the output from the RP2040’s integrated temperature sensor on a SparkFun 16x2 SerLCD display over I2C.

SOFTWARE PREPARATION

We will need a micro-USB cable to program and power the Pico. We can attach the device to our computer’s USB port while holding down the white BOOTSEL push button on the Pico. Once the device has been detected as RPI-RP2 we can release the boot button and should see the Pico as a mass storage device in Windows Explorer.

Because we're using MicroPython with our Raspberry Pi Pico, we will need to reprogram the board so that it can accept and run Python code. To do this we will need to download the MicroPython UF2 file from Raspberry Pi. The file can be found here: https://www.raspberrypi.org/documentation/pico/getting-started/

Once we have the MicroPython UF2 file downloaded, we can load it to our Raspberry Pi Pico by copying it onto the RPI-RP2 storage device in Windows Explorer. The file will transfer over and the Raspberry Pi Pico will reboot. The device is now running MicroPython and is ready to program.

To program our Pico with MicroPython we will need an IDE. The officially recommended Thonny IDE will allow us to write MicroPython code and upload it to the Pico. We can download and install Thonny from https://thonny.org/.

With Thonny installed we can configure it for use with the Raspberry Pi Pico. Selecting the python version number in the bottom right of the Thonny window will bring up a list of available interpreters. We will select MicroPython (Raspberry Pi Pico) so that Thonny correctly uploads our code to the Pico.

PROGRAMMING

We are now ready to start programming the Pico. First, we will start with a basic program to flash the onboard LED before moving on to displaying the temperature. Select the script area in Thonny and enter the following code which can also be downloaded from our website:

import machine
import utime
led_onboard = machine.Pin(25, machine.Pin.OUT)
while True:
 led_onboard.value(1)
 utime.sleep(0.5)
 led_onboard.value(0)
 utime.sleep(0.5)

The first two lines of our code import the machine and utime python modules. The machine module provides functions directly related to the Raspberry Pi Pico such as I/O access. The utime library is used to provide sleep functionality.

We then define boardLED as the onboard LED which is attached to pin 25 of the Raspberry Pi Pico. We set the pin to OUT so we can send commands to control the LED. The program then loops indefinitely, setting the boardLED to 1, waiting 0.5 seconds, then setting the boardLED to 0 and waiting another 0.5 seconds. This process will continue until the program is ended and will result in a blinking onboard LED.

To upload the program to our Pico we can choose Save from the Thonny IDE and select Raspberry Pi Pico as the save location. The file name must end in .py so that the device knows it’s a python script. We can call the file boot.py as this will cause the program to run whenever the Raspberry Pi Pico is powered on. The onboard LED should begin blinking. To stop the program we can select stop from the Thonny IDE.

The Build: Temperature Display

PARTS REQUIRED:JaycarAltronicsCore Electronics
1 x Raspberry Pi Pico (with Soldered Headers)*--CE07589
1 x SparkFun 16x2 SerLCD (Qwiic)--LCD-16397
1 x Qwiic Cable - Female Jumper--CAB-14988

PARTS REQUIRED:

OPTIONAL:JaycarAltronicsCore Electronics
1 x Raspberry Pi Pico (without Soldered Headers)--CE07564

OPTIONAL:

* We used the Pico with soldered headers for added convenience.

We wanted an easy first project to showcase the features of the Raspberry Pi Pico, so we decided to create a temperature display utilising the integrated thermometer and a SparkFun I2C LCD. The Pico includes two I2C controllers. We will be using one of these to interface with the SparkFun 16x2 SerLCD display.

The screen will display the temperature recorded from the RP2040 internal temperature sensor. Note: Due to the position of the sensor, our reading won’t be extremely accurate but should be more than sufficient as a basic desktop temperature monitor.

WIRING

Follow the wiring diagram here to wire your circuit together.

The SerLCD I2C display requires four connections to the Pico. We can use the QWIIC connector on the rear of the display to eliminate the need for a breadboard.

PROGRAMMING

With our wiring complete, we can attach the Pico to our computer and open up Thonny.

We need to upload the CODE2.py to our device (The code can also be found on our website). Select the Thonny script area and copy the code, save the file to the Pico as boot.py, and reboot.

import machine
import utime
sda=machine.Pin(0)
scl=machine.Pin(1)
i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000)
utime.sleep(1) # REQUIRED to allow the SerLCD to initialise, code will not run on boot without this delay
i2c.writeto(114, 'x7C') #enter settings mode
i2c.writeto(114, 'x18') #change contrast
i2c.writeto(114, 'xAA') #set contrast to 170/255
i2c.writeto(114, 'x7C') #enter settings mode
i2c.writeto(114, 'x2B') #change backlight RGB values
i2c.writeto(114, 'x00') #set red value to 170/255
i2c.writeto(114, 'xFF') #set green value to 170/255
i2c.writeto(114, 'x00') #set blue value to 170/255
i2c.writeto(114, 'x7C') #enter settings mode
i2c.writeto(114, 'x2D') #clear display
adc = machine.ADC(4) #setup analogue-to-digital converter on channel 4 for temperature sensor
conversion_factor = 3.3 / (65535) # convert raw value to voltage value
while True:
    reading = adc.read_u16() * conversion_factor # read temperature sensor
    temperature = 27 - (reading - 0.706)/0.001721 #convert voltage value to degrees celsius
    i2c.writeto(114, 'x7C') #enter settings mode
    i2c.writeto(114, 'x2D') #clear display
    out_string = "   DIYODE MAG     TEMP: " + str(round(temperature, 1)) + "xDFC" #construct string to print
    i2c.writeto(114, out_string) #print text to display
    utime.sleep(1) 

The code configures and initialises our I2C connection on pins 0 and 1 to talk with our display. We had some issues with connecting to our SerLCD, which we determined were due to the lack of time for the device to boot. We added a 1-second delay which solved this behaviour.

We then configure the display by setting the contrast, backlight colour, and clearing any text. The commands used are taken from the official SparkFun SerLCD Python documentation:

The onboard temperature sensor is connected to pin 4, and provides a raw value. We need to convert this value to a voltage value which is defined by the constant conversion_factor.

With everything defined, the code enters into the main loop where the temperature value is read and converted to degrees celsius by the equation defined in the RP2040 documentation. This value is then printed to the display before sleeping for 1 second and repeating.

3D PRINTED CASE

We designed and 3D printed a basic case for the Raspberry Pi Pico and SerLCD which allows for a basic friction mount along with a small bit of adhesive. We printed the case on an Ender 3 Pro but it should print perfectly fine on any 3D printer with a large enough print bed. The files can be found on our website.

POWER & CODE

The Pico can be powered from any USB source so we hooked ours up to a portable battery bank which we could place anywhere.

The code is well documented so it would be easy to configure the output or screen colour/contrast. You could make the display backlight change colour depending on the recorded temperature or add more devices.

WHERE TO FROM HERE?

The Raspberry Pi Foundation has released the RP 2040 to their partners at Adafruit, Arduino, and Sparkfun to create a variety of boards featuring the chip. Adafruit has announced two new boards; one in their Feather ecosystem with 4 MB onboard flash, STEMMA QT I2C connector, and USB-C; their other board, the ItsyBitsy RP 2040 is a tiny board designed for driving NeoPixels and other RGB LEDs with its 5V logic pin.

Arduino will also be releasing the Nano RP2040 Connect featuring a RP 2040 chip, 9-axis IMU, microphone, WiFi/Bluetooth module, and ATECC608 crypto chip. Arduino has also confirmed that they will port the Arduino core so that everyone with RP 2040 boards can take full advantage of the Arduino ecosystem.

Sparkfun has announced three new RP 2040 based products, the first being a new version of the Thing Plus with 18 GPIO pins, SD card slot, 16MB flash memory, battery circuitry, and Qwiic connector. The next board from Sparkfun is the MicroMod RP2040 Processor which is designed for use with MicroMod carrier boards. Sparkfun is also releasing the compact Pro Micro RP2040 with a WS2812B addressable LED, Qwiic connector, and USB-C.

The RP 2040 bare chip will also be available to purchase in Q2 2021 which will allow experienced makers to design custom boards for specific applications. The RP 2040 naming indicates that this chip may not be the only model from the Raspberry Pi Foundation with the potential for others in the future.