Raspberry Pi Pico Tutorial

Product Description

Raspberry Pi Pico is an official Raspberry Pi designed low-cost, high-performance microcontroller development board with a flexible digital interface. Hardware, using Raspberry Pi official self-developed RP2040 microcontroller chip, equipped with ARM Cortex M0 + dual-core processor, up to 133MHz running frequency, built-in 264KB of SRAM and 2MB of memory, and up to 26 multi-functional GPIOs onboard pins on board. The software can be developed with the C/C++ SDK provided by Raspberry Pi or with MicroPython, and there is a comprehensive development material tutorial to facilitate quick start development and embedding into the product. This is a Raspberry Pi Pico tutorial.

Product Features

Adopts the RP2040 microcontroller chip designed by Raspberry Pi

  • Dual-core ARM Cortex M0+ processor running at 133MHz flexible clock
  • Built-in 264KB SRAM and 2MB on-chip Flash
  • Stamped hole design for direct solder integration into user-designed backplanes
  • 1 host and device support
  • Supports low power sleep and hibernate modes
  • Drag-and-drop program download via USB recognition for mass storage
  • Up to 26 multi-functional GPIO pins
  • 2 SPI, 2 I2C, 2 UART, 3 12-bit ADCs, 16 controllable PWM channels
  • Precise on-chip clock and timer
  • Temperature sensors
  • On-chip accelerated floating-point library
  • 8 programmable I/O (PIO) state machines for custom peripheral support

Pinouts


Get Started Quickly

Instructions For Use

  • Download the test firmware to your computer and unzip it.
  • There are two uf2 files, one of which is the pico_micropython_20210121.uf2 file is MicroPython firmware
  • Press and hold the button on the Pico board, connect the pico to the USB port of the computer via the Micro USB cable, and then release the button.
  • Once connected, the computer will automatically recognize a removable disk (RPI-RP2)


  • Copy and drag the firmware file downloaded earlier to the RPi-RP2 mobile disk.
  • After the copy is complete, the Raspberry Pi Pico will automatically reboot. After the automatic reboot, the pico will be recognized as a virtual serial port.


Note: If the mobile disk is not automatically recognized after accessing the pico.

  1. check whether the BOOTSEL button is not pressed or released in the middle.
  2. The Micro USB cable used must be a data cable, only the power supply USB cable can not be used.

Software Environment Configuration (WIndows)

To facilitate the development of the Pico board using MicroPython on your computer, it is recommended to download the Thonny IDE.

  • Download the Thonny IDE and follow the steps to install it.
  • Thonny IDE download link (Windows version).
  • Once the installation is complete, configure the language and motherboard environment for the first time. Since we are using Pico, take care to select the Raspberry Pi Option.


Control LED Routine

  • Plug the Pico into your computer (without pressing a button), if you have not flashed MicroPython before, follow the flash firmware procedure.
  • Select Tools -> Options… -> Interpreter.
  • Select Raspberry Pi Pico in the Interpreter option (be careful to download the latest version of Thonny, otherwise, there is no such option).
  • After selecting the port to access the Pico, the COM port recognized by the computer is good.
  • Then confirm.
  • After confirming, you can see that there will be more than one Pico information in the command-line interface, now you can enter the MicroPython program here to control the pico.


Thonny Settings


Thonny Set Up The Port


Thonny Pico Interface

  • Looking at Pico’s pin diagram we know that the control pin for Pico’s onboard LED is GPIO25, here we try to control the onboard LED.
  • In Thonny, run the following code in sequence.

>>> from machine import Pin

>>> led = Pin(25, Pin.OUT)

>>> led.value(1)

>>> led.value(0)

After running the code in sequence, you can see the LEDs on the Pico board are lit and then turned off

Note

If you want to know more about the functions of Raspberry Pi Pico Micropython, you can check the Pico Python SDK manual.

•  The default serial port of Raspberry Pi is used for serial terminal debugging, if you want to use the serial port normally, you need to modify the Raspberry Pi settings. If you turn off the serial terminal debugging function, you can no longer access the Raspberry Pi through the serial port, you need to turn it on again before you can control the Raspberry Pi through the serial port.

I. Release The Serial Port

Execute the following command to enter the Raspberry Pi configuration

sudo raspi-config

Select Interfacing Options ->Serial ->no -> yes to disable the serial debugging function and open the serial port.

•  Open the /boot/config.txt file and find the following configuration statement to enable the serial port, if not, you can add it at the end of the file.

enable_uart=1

II. Use Minicom To Debug The Serial Port

•  After setting up the serial port can be used normally, you can test whether the Raspberry Pi UART is working properly, Pioneer600 expansion board with USB to UART function, use the USB cable to connect to the computer. minicom is a simple and useful tool. minicom is a Linux platform serial debugging tool. It is equivalent to a serial debugging assistant on windows.

1、Minicom Installation

sudo apt-get install minicom

2、Minicom Start

minicom -D /dev/ttyS0

•  The default baud rate is 115200, if you want to set the baud rate to 9600, add the parameter -b 9600, -D stands for port, /dev/ttyS0 is similar to COM1 in windows.


Also, open the serial port assistant in windows. Set the baud rate to 115200 and select the corresponding serial port number.

3、Serial Data Transmission

•  You can send data through the serial port by typing directly in the minicom console, and the typed data will be received in the windows, serial assistant. Similarly, the data sent in the windows serial assistant will be displayed on the minicom console. If minicom is open back (Ctrl+A, then E), you can observe the output content in the console, if back off minicom console will not display your input content. First Ctrl+A, then Q, exit minicom.

#include 
#include 
#include 
int main()
{
    int fd; 
    if(wiringPiSetup() < 0)return 1;
    if((fd = serialOpen("/dev/ttyS0",115200)) < 0)return 1;
    printf("serial test start ...\n");
    serialPrintf(fd,"Hello World!!!\n");
    while(1)
    {   
        serialPutchar(fd,serialGetchar(fd));
    }   
    serialClose(fd);

    return 0

}

Compile and execute, open the serial assistant under the window and receive “Hello World!!!”, sending data will return to the display window.

 

gcc -Wall uart.c -o uart -lwiringPi
sudo ./uart

III. Python

First, run the following command to install the python serial extension library.

sudo apt-get install python-serial

Writing programs

#!/usr/bin/python
# -*- coding:utf-8 -*-
import serial
ser = serial.Serial("/dev/ttyS0",115200)
print('serial test start ...')
ser.write("Hello Wrold !!!\n")
try:
    while True:
        ser.write(ser.read())
except KeyboardInterrupt:
    if ser != None:
        ser.close()

Execute the program and the experimental results are the same as above.

sudo python uart.py

Notes

(1) ser = serial.Serial(“/dev/ttyAMA0”,115200) Open serial port with baud rate of 115200.

(2) ser.write(ser.read()) Receive characters and send back.

(3) ser.close() close the serial port.

The above is a Raspberry Pi Pico tutorial, and we will post more tutorials on Raspberry Pi and STONE HMI screens and development (Raspberry pi Pico and STONE LCD display to develop an RGB dazzling light control solution) in the future.  Stay tuned.

Sign In or Register to comment.