How to use Raspberry Pi Pico for IoT programming?

Table of contents

1. Raspberry Pi Pico series and functions

2. Alternatives to Raspberry Pi Pico

3. Programming the Raspberry Pi Pico

hardware

software

Step 1: Connect your computer

Step 2: Install MicroPython on the Pico

Step 3: Set up the interpreter for Thonny

Step 4: Write the program

Step 5: Run the program

Four. Summary

As we all know, Raspberry Pi (Raspberry Pi) hardware 1~4 has been well known and widely adopted in the IoT world. The Raspberry Pi Pico also has a special place in the world of IoT due to its small physical size, relatively high processing power and low energy consumption, and has already gained popularity as a microcontroller (MCU). A universal favorite among development enthusiasts and IoT professionals.

Pico has long been supported by the Raspberry Pi Foundation. The Foundation is a registered charity dedicated to education. Raspberry Pi Pico is not only very cheap, but also takes only a few minutes to set up and put into use, so users don't need deep programming knowledge or need to search for enough documents on the Internet to get started. Below, I'll discuss Raspberry Pi Pico programming features, alternatives, and resources.

1. Raspberry Pi Pico series and functions

The Raspberry Pi Pico is more than just a microcontroller, it is a family of high-performance MCU boards. Typical MCU boards include: Raspberry Pi Pico, Pico H, Pico W, and Pico WH. Of these, the Pico and Pico H are nearly identical, with the only major difference being that the Pico H comes with pre-soldered headers (ie, where you attach the wires), while the Pico doesn't. So if you want to connect wires to a regular Pico, you'll have to solder the headers yourself.

Both the Pico W and Pico WH add onboard Wi-Fi modules that allow the device to connect to the internet. The only difference between Pico W and Pico WH is that the WH has pre-soldered joints.

In general, the above four series have the following characteristics:

  1. RP2040 MCU
  2. Dual-core Arm Cortex M0+ processor
  3. Clock speeds up to 133 MHz
  4. 264 KB SRAM and 2 MB Flash
  5. 1 USB 1.1 port
  6. Low Power or Sleep Mode
  7. 26 general-purpose input/output (GPIO) pins
  8. temperature sensor
  9. 2 SPI, 2 I2C, 2 UART, 3 ADC
  10. Eight Programmable I/O State Machines

Also, all four types of Pico have LEDs on board, which is very important for your first IoT project. Of course, if you already have some IoT and  MCU experience , skip to the next section.

Next, let's start with the MCU. Most Raspberry Pi designs ship as single board computers (SBC). They have high processing power for their size and can perform the same basic operations as desktop computers. Typically, they use RaspberryOS, a Linux-based operating system. In contrast, microcontroller boards do not have the capability to run a full operating system. That is, on a desktop or laptop, you can have multiple applications open, whereas the MCU can only run one application at a time.

For example, a standard smart security camera doesn't need a full operating system since its only function is to stream video. In fact, the Raspberry Pi Pico OS is a real-time OS that does one thing at a time.

As far as processors go, ARM processors are very popular in IoT due to their high levels of energy efficiency. And the M0+ processor used by the Raspberry Pi Pico is even more efficient than a typical ARM processor. Typically, a clock speed of 133 MHz isn't particularly fast for how fast a processor can execute instructions. However, despite this, this speed is more than enough for typical IoT applications. For reference, the absolute maximum clock speed of a smart fitness device with multiple sensors and functions is likely to be around 300 MHz.

As far as the device specification is concerned, the data on the 264 KB SRAM is only retained when the device is powered on. Once the device loses power, is turned off, or the battery dies, the information in memory will no longer exist. The data in the 2 MB flash memory is retained in the device's memory even after the power is turned off. In other words, it actually acts as a data repository for the device.

The Raspberry Pi Pico also has 26 GPIO pins. The pins here refer to small holes, which are convenient for you to connect external devices. For example, you can use one of the pins to connect additional LED lights to the board as needed.

Note that SPI, I2C, UART, and ADC are each different types of input protocols/formats. With its great variety, you can use Pico for many different projects and applications. Finally, with programmable I/O state machines as input/output components, you can program for different functions and protocols.

 

2. Alternatives to Raspberry Pi Pico

While the Raspberry Pi Pico has become a great choice for IoT projects, it's not the only one. Below, let's take a look at other alternatives to the Raspberry Pi Pico:

Arduino Uno

The Raspberry Pi Pico clearly has better processing power and more input options than an Arduino Uno board of similar physical size. For example, the Pico has a maximum clock speed of 133 MHz, while the Arduino Uno has a maximum clock speed of only 16 MHz. At the same time, it only has 32 KB of RAM and 16 KB of flash memory. Additionally, the Uno has fewer I/O pins, higher power consumption, and a higher price than the Pico. Of course, the Arduino Uno remains a popular alternative due to its compatibility with Arduino's large open-source tools, code library, and Arduino Integrated Development Environment (IDE).

Raspberry Pi Zero

As a full-fledged computer, the Raspberry Pi Zero has an onboard microprocessor (MPU) instead of an MCU. It can run multiple applications simultaneously and has high clock speeds, 512 MB of RAM, 8 GB of flash memory, and 40 I/O pins (the Pico only has 26). It's also the smallest Raspberry Pi MPU in existence. However, it consumes much more power than the Raspberry Pi Pico, and it's much more expensive, running about $15 compared to the Pico's cost of just $4.

ESP32

Espressif's ESP32 MCU series is small in size but has about twice the processing power and speed of the Pico. Although the exact specifications of this family of MCUs vary, they typically feature two extra I/O pins and twice the flash memory compared to the Pico. Coincidentally, their energy consumption and price are also twice that of Pico. It can be seen that Pico may be more suitable for some initial construction projects, while ESP32 is more suitable for large-scale commercial projects.

3. Programming the Raspberry Pi Pico

Let's start with a simple starter project. Just as software programmers first learn a language by writing a simple program to print "Hello World", hardware programmers also need to first practice writing a simple program to make LED lights blink. In this example, we will go through setting up the Raspberry Pi Pico to turn the LED light on and off at set intervals.

Notably, the Raspberry Pi Pico responds to several coding languages, including C, C++, and MicroPython. MicroPython here is a lighter, simpler version of traditional Python and the language most used by Raspberry Pi Pico enthusiasts. So if you already know Python, you can easily start using MicroPython on the Raspberry Pi Pico.

hardware

For this simple project, you don't need anything other than a development board and a USB cable. You can use a computer to provide power and program the device. Of course, if you want to continue using the Pico for other projects, you'll need to connect it to an external power source to continue working away from your computer.

In the meantime, you can get a starter kit for the Raspberry Pi Pico on Amazon or elsewhere. Such kits typically provide a Raspberry Pi Pico MCU board, a USB cable, a breadboard, some LEDs and resistors, and various tutorial resources. The breadboard here is a plastic board with holes. You plug the Pico into it, and then connect other electrical components, including the power supply, to the board.

software

You will need the appropriate software installed on your computer to program the Raspberry Pi Pico. In most projects, developers will choose the Thonny IDE. You can download and install Thonny from the link: Thonny, Python IDE for beginners . Meanwhile, you can download and install MicroPython by linking: MicroPython - Python for microcontrollers .

Step 1: Connect your computer

Please connect the USB cable to the USB port of the device. Then press the small white button on the Raspberry Pi Pico board (i.e. the boot select button). Press and hold the button, plug the other end of the USB into the computer, and then release the button. At this point, in File Explorer, you should see a new connected device named "RP1-RP2".

Step 2: Install MicroPython on the Pico

In the file explorer, please click on the RP1-RP2 device, and paste the downloaded MicroPython file here.

Step 3: Set up the interpreter for Thonny

Open the Thonny software. Please click "Select Interpreter" under the "Run" toolbar, and select "MicroPython (Raspberry Pico)" from each option in the drop-down menu, and then click "OK".

Step 4: Write the program

Please type or paste the following code into the main window of Thonny software:

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

This code will turn the LED on for one second, then off for one second, and loop indefinitely until the device is unplugged.

Step 5: Run the program

Next, click the play button (with the play symbol on it). A pop-up window on your computer screen will ask you where you want to save the file, and a file name. At this point, click on "Raspberry Pi Pico". Note, when naming the file, make sure to add .py at the end so that the MCU recognizes the development language as MicroPython. For example, you could name the file LED.py. Of course, name it main.py if you want the file to run automatically every time the device is plugged in. This will tell Pico to run this program immediately on startup. Remember to click OK.

At this point, if you can see the LED light blinking periodically, then congratulations you have successfully completed your first programming project on the Raspberry Pi Pico.

Four. Summary

All that said, the Raspberry Pi Pico is an inexpensive and easy option for everything from learning to program, to prototyping, to creating IoT devices from scratch. Of course, you can also enable it to play more and richer functions through programming in various flexible and changeable scenarios. 

Related Content Expansion: (Technical Frontier)

In the past 10 years, when even traditional enterprises began to digitize on a large scale, we found that in the process of developing internal tools, a large number of pages, scenes, components, etc. were constantly repeated. This repetitive work of reinventing the wheel wasted a lot of time for engineers.

In response to such problems, low-code visualizes certain recurring scenarios and processes into individual components, APIs, and database interfaces, avoiding repeated wheel creation. Greatly improved programmer productivity.

Recommend a software JNPF rapid development platform that programmers should know, adopts the industry-leading SpringBoot micro-service architecture, supports SpringCloud mode, improves the foundation of platform expansion, and meets the needs of rapid system development, flexible expansion, seamless integration and high Comprehensive capabilities such as performance applications; adopting the front-end and back-end separation mode, front-end and back-end developers can work together to be responsible for different sections, saving trouble and convenience.

Experience official website: https://www.jnpfsoft.com/?csdn

If you haven't understood the low-code technology, you can experience and learn it quickly!

Guess you like

Origin blog.csdn.net/Z__7Gk/article/details/132317788
Recommended