0% found this document useful (0 votes)
65 views

Raspberry Pi Pico As An Iot Device

The document discusses using the Raspberry Pi Pico as an IoT device in educational settings. It describes the features and capabilities of the Pi Pico, provides examples of interfacing digital and analog sensors, and shows how to program the Pi Pico using MicroPython. Applications discussed include temperature sensing, LED control, and interfacing a LCD display.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Raspberry Pi Pico As An Iot Device

The document discusses using the Raspberry Pi Pico as an IoT device in educational settings. It describes the features and capabilities of the Pi Pico, provides examples of interfacing digital and analog sensors, and shows how to program the Pi Pico using MicroPython. Applications discussed include temperature sensing, LED control, and interfacing a LCD display.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Paper ID #37487

Raspberry Pi Pico as an IoT Device


Mr. David R. Loker, Pennsylvania State University, Behrend College
David R. Loker received the M.S.E.E. degree from Syracuse University in 1986. In 1984, he joined
General Electric (GE) Company, AESD, as a design engineer. In 1988, he joined the faculty at Penn State
Erie, The Behrend College. In 2007, he became the Chair of the Electrical and Computer Engineering
Technology Program. His research interests include wireless sensor networks, data acquisition systems,
and communications systems.

©American Society for Engineering Education, 2023


Raspberry Pi Pico as an IoT Device

Abstract

The Raspberry Pi Pico is an inexpensive embedded processor board that can be used for
introductory programming and embedded systems courses in engineering and engineering
technology programs. For Internet of Things (IoT) applications, the Pi Pico contains a system-on-a-
chip (SoC) device (the RP2040 microcontroller) that is capable of performing the responsibilities of
a computer on a single chip. Some key features of the Pi Pico include digital peripherals (e.g., 2 SPI,
2 I2C, 2 UART, and 16 PWM), 23 GPIO pins for digital I/O, 3 ADC inputs, and an on-board LED
and temp sensor. Although programming the Pi Pico can be performed in C, MicroPython, which is
a subset of the Python standard library, is optimized to run on a variety of embedded
microcontrollers including the Pi Pico. Thonny is a free download software development
environment for writing Python code and downloading it to the Pi Pico.

Applications for the Pi Pico are broad enough to encompass both electrical and computing
disciplines. There are several goals for this paper. For embedded courses, control of the I/O pins on
the Pi Pico will be shown. This includes digital I/O, analog input, PWM output, UART, SPI, and
I2C. Devices interfaced to the Pi Pico include an analog temperature sensor, a serial LCD display, a
digital-to-analog converter, and an accelerometer. A WiFi device will also be interfaced to the Pi
Pico to show the capabilities for embedded IoT applications. Examples of student assessments will
be shown. For ABET, it will be shown how these projects can be used to assess student outcomes.

Introduction

In engineering and engineering technology programs, there are a variety of programming and
embedded systems courses. C/C++ programming is often used as a primary component to these
courses. Examples of devices used within embedded hardware and software courses include the
Programmable System-on-Chip (PSoC 5LP) and the BeagleBone Black (BBB) [1-3].
Alternatively, MicroPython is a subset of the Python standard library, and it is optimized to run on a
variety of microcontrollers for embedded applications [4]. One such device is the Raspberry Pi Pico
[5]. The Pi Pico is an inexpensive embedded processor board that can be used in a variety of
courses.

Embedded Courses

In electrical and computing disciplines, curriculum is set up so that programming and embedded
systems are taught through a variety of courses. These courses can include programming
(structured procedural design and object-oriented design), Digital Design, and microprocessors
and microcontrollers (introduction, intermediate, and advanced). Additionally, systems-oriented
courses (e.g., Communication Systems, Control Systems, Senior-level Project-based courses,
etc.) typically include embedded systems as an integral component.

C/C++ programming is often used with embedded systems courses as the core component. An
introductory microprocessors and microcontrollers course may utilize C programming on an
“Arduino” type computer board. Students learn ‘C’ programming, and with the availability of
API/library calls, they can ignore the low-level register programming. For intermediate
embedded courses, students can design software that interfaces directly to the hardware. Course
topics can include interrupts, timers, ADC and DAC interfacing, and digital filtering.

Due to its open-source popularity, Python can be used in curriculums to supplement C/C++
course content. Examples include utilizing MicroPython on the Digi XBee3 module and on the
Raspberry Pi Pico [4-5]. Python and MicroPython are very similar, the main difference being
that MicroPython does not include the data analysis and graphing libraries of standard Python.
However, students will learn to use hardware libraries associated with embedded programming.

There are several goals for this paper. For embedded courses, control of the I/O pins on the Pi Pico
will be shown. This includes digital I/O, analog input, PWM output, UART, SPI, and I2C. Devices
interfaced to the Pi Pico include an analog temperature sensor, a serial LCD display, a digital-to-
analog converter, and an accelerometer. A WiFi device will also be interfaced to the Pi Pico to show
the capabilities for embedded IoT applications. Thonny, the Python IDE, will be used for software
development [6].

Thonny IDE

An IDE for developing Python code is called Thonny [6]. Thonny allows a Windows PC to
seamlessly integrate with a Raspberry Pi-Pico. Thonny’s console interface can display a
program’s text output, and Thonny has a mechanism to single step and debug Python programs
running on the Pico. During development, programs can be saved onto the PC or the Pi Pico. It
has a script area for writing code and a Python Shell for running programs. A simple example is
shown in Figure 1. This program prints the value of a variable called “resistor” to the console.

Figure 1. Python print command

MicroPython

MicroPython is a subset of the Python standard library, and it is optimized to run on


microcontrollers (e.g., Pi Pico, etc.) for embedded applications [7-8]. The interface to its hardware
I/O is accomplished via functional specific libraries. Some examples include Bluetooth, machine,
and network communication. The machine module implements specific classes of functions
related to the hardware on embedded boards. Examples of various classes include:

• Pin for I/O control pins


• ADC for analog to digital conversion
• PWM for pulse width modulation
• UART for serial communication
• SPI for serial peripheral interface protocol
• I2C for a two-wire serial protocol

Raspberry Pi Pico

The Raspberry Pi Pico board has the following features [9-10].

• 264 KB SRAM in six independent banks


• No internal Flash or EEPROM memory (after reset, the bootloader loads firmware from
either the external flash memory or USB bus into internal SRAM)
• QSPI bus controller, which supports up to 16 MB of external Flash memory
• On-chip programmable LDO to generate core voltage
• 2 on-chip PLLs to generate USB and core clocks
• 30 GPIO pins, of which 4 can optionally be used as analog inputs
• Processor can be clocked up to 133 MHz

Figure 2 contains the pinouts for the Raspberry Pi Pico board.

Figure 2. Pi Pico Pinout [9]


Digital I/O on the Pi Pico

Each of the Pi Pico I/O pins are identified by a GPIO number. On the bottom of the board, each
pin has silk screening that details this number (along with power and ground). One of the GPIO
pins is special (GP-25), since it connects to the on-board LED which is available to be used by
the program. All I/O pins can be used as digital I/O pins, and a subset of these pins can be
configured for PWM (pulse-width modulation), A-to-D inputs, UART, and SPI communication
pins. These pinouts are shown in Figure 2.

Digital output pins can drive up to 3.3 volts at 8 milliamps. Input pins also have programmable
pull-up and pull-down resistors. To configure a pin, the machine library is imported which
contains the functions needed to configure and interface to the I/O pins. Examples are shown in
Figure 3.

Figure 3. Digital I/O Code Examples.

GPIO25 connects to the on-board LED. An example of blinking this LED on and off every 0.5
seconds is shown in Figure 4.
Figure 4. Blinking LED Code.

Analog Input

The Pi-Pico has 3 external analog-to-digital converter (ADC) inputs (pins 31, 32 and 34) and can
monitor any voltage from 0 to 3.3 volts. Each connects to a 12-bit ADC. The 12-bit value is
converted to 16 bits (i.e., the ADC reads 0-4095 but represents it as 0-65535). An example is an
LM35 temperature sensor connected to an ADC input [11]. The temperature coefficient for the
LM35 is 10mV/ºC. The Pi Pico reads the analog input from the LM35 on ADC2 (pin 34). The
program is shown in Figure 5.

Figure 5. LM35 Temp Sensor Code and Output.

PWM Output

All of the digital output pins can also be configured for pulse-width modulation (PWM). This
means that you can configure the pin to output a square wave at a specific frequency and duty
cycle. See Figure 2 for a full listing of available I/O pins.

An application would be varying the brightness of an LED by changing the duty cycle of a
square wave. The PWM frequency should be set high enough (e.g., 5 KHz) to eliminate “flicker”
at low duty cycles. An example of controlling the brightness of the on-board LED (GPIO25) is
shown below in Figure 6. In this example, the duty cycle is ramped up linearly from 2% to
65.5% in steps of 1% (delays 2mS between steps), pauses for 2 seconds, then ramps down
linearly from 65.5% to 2%.

Figure 6. Code for On-board LED Brightness Control using PWM.

UART/RS-232

The Pi Pico has two UART’s, which can be mapped to a variety of GPIO pins. See Figure 2 for a
full listing of available I/O pins.

An application would be interfacing to a serial LCD display [12]. From the datasheets, the
manufacturer lists approximately 25 commands that the device can perform. Each command
begins with the same prefix byte (0xFE), followed by 1 or 2 additional bytes. By storing the
command sequences in byte arrays, a single instruction can be used to send commands to the
display. Some LCD write examples are shown below in Figure 7.
Figure 7. Code for the serial LCD.

An example would be to display the temperature from the on-board temp sensor to both the
console and LCD display. The on-board temperature sensor (base-emitter junction of a biased
bipolar diode) is connected to the internal analog-to-digital converter (ADC4). The ADC has a
full-scale voltage of 3.3V with 12 bits of resolution. From the datasheets, Vbe = 0.706V at 27
degrees C, with a slope of -1.721mV/degree. Hardware connections would be as shown in Figure
8. An image of the LCD display output is shown in Figure 9. Code for displaying temperature to
both the console and LCD is shown in Figure 10.

Figure 8. Hardware connections for the serial LCD.

Figure 9. On-board Temp Sensor Serial LCD Display.


Figure 10. On-board Temp Sensor Code and Output.

SPI

The Serial Peripheral Interface (SPI) refers to a 4-wire serial interface which includes the signals:
MOSI or SPI-TX, MISO or SPI-RX, SCLK or SPI-SCK and CS. See Figure 2 for a full listing of
available I/O pins. SPI initialization (from the online documentation) is found from the online
SPI Python library [13].

An application would be interfacing an MCP4901 DAC to the Pi Pico. Connections would be as


follows. Datasheets for the MCP4901 DAC are available online [14].
• Pi Pico pin 14 (SPI1 SCK – GPIO10) to 4901 pin 3 (SCK)
• Pi Pico pin 15 (SPI1 TX – GPIO11) to 4901 pin 4 (SDI)
• Pi Pico pin 17 (SPI1 CS – GPIO13) to 4901 pin 2 (𝐶𝐶𝐶𝐶)

An example of a program is shown below in Figure 11. With VCC=3.3V, the output shown on a
DMM is in Figure 12.

Figure 11. SPI DAC Code.

Figure 12. SPI DAC DMM Display.

I2C

I2C is similar to SPI. However, the same data is sent and received to the peripheral using only
two wires (SDA and SCL). The SDA wire has both the transmit and receive data, and it moves
data to/from the peripheral using a half-duplex mode. There is no chip select (CS) as the
components ID is placed within the message. Like the other protocols, specific pins are allocated
for this type of transmission. See Figure 2 for a full listing of available I/O pins. I2C
initialization (from the online documentation) is found from the online I2C Python library [15].

An application would be interfacing an ADXL345 accelerometer to the Pi Pico. Connections


would be as follows. Datasheets for the ADXL345 using a breakout board are available online
[16].
• Pi Pico pin 21 (I2C0 SCL – GPIO16) to 345 breakout board (SDA)
• Pi Pico pin 22 (I2C0 SCL – GPIO17) to 345 breakout board (SCL)
• 345 breakout board (CS) to VCC
• 345 breakout board (SDO) to GND

An example of a portion of the program code and the output results are shown in Figures 13 and
14. Additional details regarding the program can be found in this reference [4].

Figure 13. ADXL345 Results Code.

Figure 14. ADXL345 Console Output Results.


WiFi Module for Web Server

An inexpensive WiFi module (ESP8266) with an integrated TCP/IP stack is available for
embedded applications [17]. The ESP8266 WiFi module can be connected to an ESP-01
breakout board for interfacing to the Pi Pico. Connections would be as follows.
• Pi Pico pin 2 (UART0 RX – GPIO1) to ESP-01 breakout board (TXD)
• Pi Pico pin 1 (UART0 TX – GPIO0) to ESP-01 breakout board (RXD)
• Pi Pico pin 36 (3.3V) to ESP-01 breakout board (VCC)
• Pi Pico pin 36 (3.3V) to ESP-01 breakout board (CHPD)
• Pi Pico pin 3 (GDN) to ESP-01 breakout board (GND)

A tutorial showing the above connections along with code to configure the ESP8266 as a TCP
web server is available online [18].

An example of a program is shown below in Figures 15(a-d). This program reads the voltage
from an LM35 temperature sensor, converts the voltage to temperature in degrees C, and serially
transmits (using UART0) the data to the ESP8266 WiFi Module which is configured as a TCP
server. Once the SSID and Password for the access point are provided, the local IP address for
the WiFi module is assigned. Then, using this local IP address, a web browser is opened to
access the web server. The web server console output and an image of the web browser output
are shown in Figures 16 and 17.

Figure 15(a). Web Server Initialization Code.


Figure 15(b). Web Server Functions Code.

Figure 15(c). Web Server WiFi Module Configuration Code.


Figure 15(d). Web Server Data Code.
Figure 16. Web Server Console Output.

Figure 17. Web Browser Image.

Internet of Things (IoT) Projects

Technology used in IoT projects can include devices for:

• Wireless applications (ZigBee, Bluetooth, WiFi, etc.)


• Sensing applications (temperature, pressure, moisture)
• Control applications (motor)
• Displays (LCD, LED OLED)

There are many possible examples of student IoT projects for a 2-3 week lab suitable for
embedded systems courses in engineering and engineering technology programs [5]. These
projects required MicroPython with the Pi Pico. Project deliverables included the following.
• Executive summary of the results (Word file):
o Written description (1-2 paragraphs) of the hardware design (also include OrCAD
PSpice schematics)
o Written description (1-2 paragraphs) of the software design (also include a flowchart
created in Word, ppt, or other s/w)
o Testing procedure (numbered step by step testing procedure for each engineering
requirement)
o Results (1-2 paragraphs)
o Signed academic integrity statement
• 2-3 minute video (posted on YouTube) demonstrating successful completion of the lab
project
• Upload to Canvas the following:
o Word file that contains the executive summary
o All software source code
o Link to video

Some additional possible examples of student projects are as follows.


• Automated plant monitoring and control system
• Concussion Protocol Color Test System
• Remote weather station
• Home Security System
• Home Automation System
• Remote vehicle speed monitoring

Automated Plant Monitoring and Control System

The overall objective is to automate a plant-growing system that can be sustained all year round.

For this project, several engineering requirements were as follows:


• Moisture sensors will be used to monitor the moisture content of the soil
• Lights will be used to control the growth rate
• Pumps will be used to control food and water into the soil
• System components:
o 2 analog moisture sensors
o 4 channel 5V relay module
o 2 Peristaltic Dosing Pumps (controls water inlet and feed)

The software application used the following classes.


• Pi Pico
• ADC for reading the analog moisture sensors
• Pins for relay control and pumps

The student’s flowchart for this project is shown in Figure 18.


Figure 18. Student Flowchart for Plant Monitoring and Control System

The YouTube video showing the student’s project is found here [19].

Remote Vehicle Speed Monitoring System

The overall objective is to remotely monitor and display vehicle speed. The engineering
requirements are:
• Simulate the pulses generated by road sensors
• Calculate vehicle speed based on the time between the pulses
• Allow the vehicle speed to be monitored using a web browser

The web server portion of the project was based on the online script for the ESP8266 [18].

The hardware components for this project consisted of:


• Pi Pico
• WiFi Module with Breakout Board
• National Instruments USB DAQ

The software application used the following classes.


• ADC for reading the pulses generated by the USB DAQ
• UART for serially receiving and transmitting
• UOS for returning information about the system

The student’s flowchart for this project is shown in Figure 19.


Figure 19. Student’s Flowchart for Vehicle Speed Monitoring System

The YouTube video showing the student’s project is found here [20].
Assessing Student Outcomes

From the 2022-2023 Criteria for Accrediting Engineering Technology Programs, student
outcome 3 is listed below [21].

• (Associate Degree): An ability to apply written, oral, and graphical communication in


well-defined technical and non-technical environments; and an ability to identify and use
appropriate technical literature.
• (Baccalaureate Degree): An ability to apply written, oral, and graphical communication
in broadly-defined technical and non-technical environments; and an ability to identify
and use appropriate technical literature.

where:
• Well-defined activities or problems are practical, narrow in scope, use conventional
processes and materials in traditional ways, and require knowledge of standard operating
processes
• Broadly-defined activities or problems are practical, broad in scope, relatively complex,
and involve a variety of resources; use new processes, materials, or techniques in
innovative ways; and may require extension of standard operating procedures.

Analytic rubrics provide repeatable performance scales to inform the assessment process. These
scales de-couple the assessment process from student grades, providing the opportunity for
insight to be gained into overall trends in student performance, independent of the grading
schemes used in the various classes.

The same rubrics can be used to assess the associate level students and the bachelor level
students. To achieve this, the rubrics can be organized in a “staggered developmental” manner,
with overlapping or cascading performance scales at the top. An example of a rubric for
assessing student outcome 3 is shown below in Table 1. The rubric lists the Baccalaureate and
Associate Student Outcome, performance indicators for the Outcome, and three performance
levels for each performance indicator. These performance levels are:
• Developing
• Meets Expectations
• Exceeds Expectations

A fourth level (N/A) is used when it is not possible to use the rubric with the performance
indicator for this assessment method.

A similar rubric can be used for assessing student outcome 3 based on the 2022-2023 Criteria for
Accrediting Engineering Programs [22].

Based on the above student projects, both the written reports and the YouTube videos can be
used as instruments for assessing student outcome 3.
Table 1. Rubric for Assessing Student Outcome 3

Performance level
Developing Meets Exceeds
(AS) Expectations Expectations
(AS) (AS)
Performance Developing (BS) Meets Exceeds
Indicator Expectations Expectations
(BS) (BS)
a. Ability to Technical Technical Technical Technical
apply written, communications communications communications communications
oral, and have little include properly properly employ exceptionally
graphical content that labelled graphs graphs, figures &clear & concise:
communication distinguishes and figures equations advance
in technical them from that suitable for knowledge
environments for a general audience. beyond
audience. classroom
content
b. Ability to Non-technical Non-technical Non-technical Non-technical
apply written, communications communications communications communications
oral, and are poorly are well- are well- exceptionally
graphical organized and organized, organized, clear, explain
communication presented, grammatical, grammatical, and engineering
in non-technical difficult to and avoid avoid jargon. topics from
environments comprehend. jargon. Graphs Graphs and across the
and figures figures easily curriculum to
easily understood by non-technical or
understood by non-technical non-college
non-technical reader. Both audience.
reader. communicate
content from BS
level.
c. Ability to Technical Technical Technical Technical
identify and use communications communications communications communications
appropriate limited to uses datasheets, identifies and identifies &
technical popular or textbook uses datasheets, incorporates
literature introductory content, basic thorough journal article
sources. familiarity with application of results, patent
industry codes, industry codes, research, or
specifications & specifications, novel techniques.
standards and standards.

Summary

The Raspberry Pi Pico is an inexpensive board suitable for a variety of courses in engineering and
engineering technology programs. For embedded courses, applications for the Pi Pico can include
IoT. MicroPython is optimized to run on microcontrollers, such as the Pi Pico. An IDE for
developing Python code is called Thonny, and it is available for free.

The projects shown in this paper were designed to introduce students to the I/O capabilities of the Pi
Pico. Devices interfaced to the Pi Pico included an analog temperature sensor, a serial LCD display,
a digital-to-analog converter, an accelerometer, and a WiFi device. Two student projects were
shown to illustrate examples of IoT applications. Methods of using results from these student
projects to assess student outcomes was also shown.

Future Work

The Raspberry Pi Pico is now available with WiFi capability [23]. The authors will be evaluating
the usage of this device for IoT applications.
References

[1] S. Strom and D. Loker, "Programmable System-On-Chip (PSoC) Usage in an Engineering Technology Program,"
Annual Meeting, American Society for Engineering Education, 2016.
[2] D. Loker and S. Strom, "Programmable System-On-Chip (PSoC) Usage in Embedded Programming Courses,"
Annual Meeting, American Society for Engineering Education, 2020.
[3] S. Strom and D. Loker, "BeagleBone Black for Embedded Measurement and Control Applications," Annual Meeting,
American Society for Engineering Education, 2018.
[4] D. Loker, "MicroPython in a Wireless Communications Systems Course," Annual Meeting, American Society for
Engineering Education, 2021.
[5] D. Loker, “Embedded Systems using the Raspberry Pi Pico,” Annual Meeting, American Society for Engineering
Education, 2022.
[6] Thonny. [Online]. Available: https://thonny.org/
[7] MicroPython Documentation. [Online]. Available: https://docs.micropython.org/en/latest/
[8] MicroPython. [Online]. Available: http://www.mircropython.org
[9] Raspberrypi.com. [Online]. Available: https://datasheets.raspberrypi.com/pico/pico-datasheet.pdf
[10] G. Halfacree and B. Everard, Get Started with MicroPython on Rapsberry Pi Pico, Raspberry Pi Trading Ltd, 2021.
[11] Analog Temperature Sensor (Part number: LM35DZ). [Online]. Available:
https://www.digikey.com/en/products/detail/texas-instruments/LM35DZ-NOPB/32489
[12] Serial LCD Display (Part number: NHD-0420D3Z-NSW-BBW-V3). [Online]. Available:
https://www.digikey.com/en/products/detail/newhaven-display-intl/NHD-0420D3Z-NSW-BBW-
V3/2626390?s=N4IgTCBcDaIHIAkAiBaADAFjGpBmAWinAMoDqKAQheQGq5FIgC6AvkA
[13] SPI Python Library. [Online]. Available: https://docs.micropython.org/en/latest/library/machine.SPI.html
[14] MCP4901 Datasheets. [Online]. Available: https://ww1.microchip.com/downloads/en/DeviceDoc/22248a.pdf
[15] I2C Python Library. [Online]. Available: https://docs.micropython.org/en/latest/library/machine.I2C.html
[16] ADXL345 Breakout Board. [Online]. Available: https://www.sparkfun.com/products/9836
[17] ESP8266 WiFi Module. [Online]. Available: https://www.sparkfun.com/products/17146
[18] MicroPython Script for ESP8266. [Online]. Available: https://microcontrollerslab.com/esp8266-wifi-module-
raspberry-pi-pico-web-server/
[19] YouTube Video. [Online]. Available: https://www.youtube.com/watch?v=Wjs0kJOb5-w
[20] YouTube Video. [Online]. Available: https://www.youtube.com/watch?v=he4y5hctvNs
[21] Criteria for Accrediting Engineering Technology Programs, 2022 – 2023. [Online]. Available:
https://www.abet.org/wp-content/uploads/2022/01/2022-23-ETAC-Criteria.pdf
[22] Criteria for Accrediting Engineering Programs, 2022 – 2023. [Online]. Available: https://www.abet.org/wp-
content/uploads/2022/01/2022-23-EAC-Criteria.pdf
[23] Raspberry Pi Pico W. [Online]. Available:
https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html

You might also like