viernes, 8 de noviembre de 2024

Getting Started with Raspberry Pi Pico W



https://lastminuteengineers.com/getting-started-with-raspberry-pi-pico-w/



Getting Started with Raspberry Pi Pico W

In early 2021, the Raspberry Pi Foundation, best known for producing its line of popular single-board computers (SBC), entered the silicon sales and microcontroller game all at once with the Raspberry Pi Pico – ‘Pico’ for short. Its small size, powerful features, and low cost ($4 only) have all contributed to Pico’s rapid rise to popularity.

The company has now stepped up their game by releasing Raspberry Pi Pico W – a variant of the Pico with built-in WiFi and Bluetooth. This wireless version of Raspberry Pi’s powerful Pico microcontroller board costs only $6, making it a worthy competitor of the ESP32.

Surely you have at least one of them by now, or maybe you don’t have one yet but it’s on your list; in any case, this tutorial will introduce you to this powerful new WiFi-enabled microcontroller board and show you how to install MicroPython to program it.

Introducing the Raspberry Pi Pico W

The Raspberry Pi Pico W is the newest member of the Raspberry Pi Family. It features the same attributes as the Raspberry Pi Pico, aside from the inclusion of an Infineon CYW43439 wireless chip. The following features are the primary reasons why Raspberry Pi Pico W has a legion of fans:

dualcore ico

Dual-core Processor

The Pico W is powered by Raspberry Pi’s own RP2040 system-on-chip (SoC) based on a dual-core ARM Cortex-M0+ processor that runs up to 133Mhz.

power ico

Ultra-Low Power Consumption

With its advanced power-saving technologies, Pico W achieves ultra-low power consumption, making it ideal for use in mobile devices, wearable electronics and IoT applications.

wifi ico

Wi-Fi Capabilities

The Pico W incorporates an 802.11b/g/n Infineon CYW43439 WiFi chip, allowing it to connect to a Wi-Fi network to access the internet (Station mode) or to create its own Wi-Fi wireless network (Soft access point mode) to which other devices can connect.

bluetooth ico

Dual-mode Bluetooth

The Pico W supports Bluetooth® 5.2 (BLE/Bluetooth Smart) as well as Bluetooth Classic (BT), making it suitable for a wide range of IoT applications.

ico io

Programmable I/O

Programmable I/O, or PIO for short, is a unique and exciting feature of the Pico/RP2040 that allows you to create custom hardware interfaces. For example, the RP2040 doesn’t have a CAN peripheral, but you can write code for the RP2040’s PIO engine to receive and send CAN packets.

peripherals ico

Rich set of Peripherals

The Pico W includes a plethora of built-in peripherals such as ADC, UART, SPI, I2C, PWM, and much more.

ways ico

Many Development Environments

There are several development environments available for Pico W, including MicroPython, C, and C++.

lowcost ico

Low Cost

It is one of the cheap microcontrollers, with prices beginning at $6; this makes them accessible to a wide audience.

Hardware Overview

Let’s take a deep dive into the hardware overview of the Raspberry Pi Pico W.

The RP2040 Microcontroller

The size of a stick of gum, Raspberry Pi Pico packs a surprising amount of power thanks to the chip at the heart of the board: an RP2040 microcontroller. This is the first microcontroller developed in-house by Raspberry Pi to power the Pico W and other microcontroller-based devices.

raspberry pi pico w rp2040 chip

It has a dual-core Cortex M0+ processor, 264kB multi-bank high performance SRAM, 30 multi-function general purpose I/Os (four of which can be used for ADC), various digital peripherals, and two programmable I/O (PIO) blocks.

The RP2040 is built on a modern 40nm silicon process, which allows it to provide excellent performance, low dynamic power consumption, low leakage, and a variety of low-power modes to enable extended battery operation.

The Infineon CYW43439 Chip

The Pico W incorporates an Infineon CYW43439 wireless chip that supports IEEE 802.11 b/g/n wireless LAN, and Bluetooth® 5.2.

raspberry pi pico w infineon cyw43439 chip

The CYW43439 is connected to the RP2040 via SPI. The antenna is an onboard antenna licensed from ABRACON (formerly ProAnt).

Peripherals and I/O

In many ways, the Pico W is a well-appointed microcontroller board. It exposes 26 of the 30 possible RP2040 GPIOs by routing them straight out to header pins.

raspberry pi pico w gpio pins

These pins can be assigned a variety of peripheral duties, including:

  • 3 ADC channels – 3 channels of 12-bit SAR ADC with a sampling rate of 500 Kilosample(s) per second. There’s also a fourth ADC channel, which is connected to a temperature sensor built into RP2040.
  • 2 UART interfaces – 2 UART interfaces with programmable hardware flow control
  • 16 PWM outputs – 16 PWM pins to control things like motor speed or LED brightness.
  • 2 SPI, and 2 I2C – 2 SPI and 2 I2C interfaces for connecting various sensors.
  • 1 RTC – 1 Real-time Clock provides time in human-readable format and can be used to generate interrupts at specific times.
  • 1 Timer with 4 alarms – The timer has 4 alarms, and outputs a separate interrupt for each alarm.

Power

Because the RP2040 operates at 3.3V, the board includes a buck-boost SMPS (which, as the name suggests, can seamlessly switch from buck to boost mode) that can generate the required 3.3 volts (to power the RP2040 and external circuitry) from a wide range of input voltages (~1.8 to 5.5V). This gives you a lot of options for power sources, such as a single Lithium-Ion cell or three AA cells connected in series.

raspberry pi pico w buck boost smps schottky diode

The Pico W board is typically powered by the on-board micro USB connector. Alternatively, if you have a ~1.8 to 5.5V power supply, you can use the VSYS pin to power the Pico W directly.

On the board, there is a schottky diode (OR-ing diode) that enables Pico W to be powered from an external source while still being connected to a computer through its micro USB port.

With WiFi turned on, the Pico W consumes about 50mA. It has also been observed that in some instances, consumption can spike to 150mA. It provides two low-power modes, dormant mode and sleep mode. The dormant mode uses even less power but requires an external trigger to wake.

As with most other ARM microcontrollers, there are separate clock dividers for nearly every peripheral, and they can be turned on and off individually for additional power savings.

Memory

RP2040 has embedded ROM and SRAM, and access to external Flash via a QSPI interface.

raspberry pi pico w sram rom flash

ROM: The RP2040 has 16kB of on-chip read-only memory (ROM). The contents of the ROM are fixed at the time the silicon is manufactured. It includes an initial startup routine, a flash boot sequence, flash programming routines, a USB mass storage device with UF2 support, and utility libraries such as fast floating point.

SRAM: The RP2040 has 264 kB of on-chip SRAM, which is plenty. Internally, the RAM is divided into four striped 64 kB banks for fast parallel access. Two additional 4 kB banks are non-striped and should be used as per-core stack memory.

FLASH: While the RP2040 has plenty of on-chip RAM, it lacks FLASH memory. Instead, an external 2MB QSPI FLASH chip is used to store and run programs, as well as any file storage required by MicroPython. When using C/C++, you have access to the entire FLASH memory; when using MicroPython, you’ll have about 1 MB remaining for code, files, images, fonts, etc.

Boot Selection Switch

Just below the micro USB port is a small button labeled ‘BOOTSEL’ – short for ‘Boot Selection’, which switches your Pico W between two start-up modes when it’s first switched on.

raspberry pi pico w boot selection switch

The RP2040’s internal ROM contains the UF2 bootloader, which means that when you want to upload a new program to Pico W, you can hold down the BOOTSEL button while plugging it into USB and it will appear as a USB disk drive onto which you can drag your code. There is no need for a programmer.

Debug Pads

At the bottom of your Pico W are three pads labeled ‘DEBUG’.

raspberry pi pico w debug pads

These are intended for debugging, or finding errors in programs running on the Pico W, using a special tool called a debugger. It may come in handy when writing larger and more complex programs.

Test Points

The Pico W has six test points (TP1-TP6) on the back that can be accessed if necessary, such as when using the board as a surface-mount module.

raspberry pi pico w test points

These are:

  • TP1 – Ground (close-coupled ground for differential USB signals)
  • TP2 – USB DM
  • TP3 – USB DP
  • TP4 – WL_GPIO1/SMPS PS pin (do not use)
  • TP5 – WL_GPIO0/LED (not recommended to be used)
  • TP6 – BOOTSEL

These test points are also used by Raspberry Pi engineers to quickly check that the Pico W is working after it has been assembled at the factory.

Documentation

For more information about the Raspberry Pi Pico W, please refer to the datasheet below.

Raspberry Pi Pico W Pinout

The Raspberry Pi Pico W has 40 pins in total. Pins are labeled on the underside of the board, with three pins also labeled on the top of the board: Pin 1, Pin 2, and Pin 39. These top labels will help you remember how the numbering works.

The following is the pinout. For convenience, pins with similar functionality are grouped together.

raspberry pi pico w pinout

VBUS is the micro-USB input voltage, which is connected to micro-USB port pin 1. This is nominally 5V and can be used to power hardware which needs more than 3.3 V.

VSYS is the main system input voltage. It can range from 1.8V to 5.5V. The on-board buck-boost SMPS uses this voltage to generate 3.3V for the RP2040 and its I/O.

3V3 is the main 3.3V supply to the RP2040 and its I/O, generated by the on-board buck-boost SMPS. You can use this pin to power external circuitry. The maximum current this pin can supply is dependent on the RP2040 load and the VSYS voltage; therefore, it is advised to keep the load on this pin below 300mA.

3V3_EN is connected to the on-board SMPS enable pin and is pulled high by a 100k resistor. Pulling this pin low disables the SMPS, which consequently turns off the Pico W.

GPIO Pins Pico W exposes 26 of the 30 possible RP2040 GPIOs by routing them straight out to header pins. The GPIO pins available for your program are labeled ‘GP0’ through ‘GP28’. Pins GP0 through GP22 are digital only, while pins GP26 through GP28 can be used either as digital GPIO or as ADC inputs (software selectable). It is important to note that all of the GPIO pins are 3.3V logic and are not 5V-safe! These pins have round pads for easy identification.

UART Pins There are two UART interfaces, UART0 and UART1. For convenience each UART is routed to multiple GPIO. The default Pico W UART_TX pin is pin GP0, and the UART_RX pin is pin GP1.

I2C Pins There are two hardware I2C buses, I2C0 and I2C1, that are multiplexed across the GPIO. Check the pinout for what pins can be set to which.

SPI Pins Pico W has two identical SPI controllers, SPI0 and SPI1, that are multiplexed across the GPIO. Check the pinout for what pins can be set to which.

ADC Pins Pins GP26 through GP28 can be used either as digital GPIO or as ADC inputs – but not both at the same time.

ADC_VREF is a special input pin which sets a reference voltage for any analogue inputs. This pin can be used with an external reference if better ADC performance is required.

AGND is a special ground connection for use with the ADC_VREF pin.

GND is a ground connection, used to complete a circuit connected to a power source. These pins have square pads for easy identification.

RUN is the Pico W enable pin, and has an internal (on-chip) pull-up resistor to 3.3V of about ~50kΩ. To reset Pico W, short this pin low.

Some internal peripheral connections appear in multiple places to allow some system level flexibility. To further explain, there are two I2C peripherals (I2C0 and I2C1), two SPI peripherals (SPI0 and SPI1), and two UART peripherals (UART0 and UART1) available. You can assign any of these to the pins on which they are available.

So, for example, you can use GP0/GP1 for I2C0, and simultaneously use GP2/GP3 for I2C1, but you cannot use GP0/GP1 together with GP4/GP5 for I2C use, because they are both usable only with I2C0.

Pico W Development Environments

There are several development environments available for programming the Pico W.

It can be programmed in Assembly, C/C++, FreePascal, Rust (a fairly new but robust programming language developed at Mozilla), Go (a statically typed programming language designed at Google), and MicroPython (implementation of Python 3 for microcontrollers).

The Pico W is also powerful enough to run TensorFlow Lite (a framework designed to run machine learning models on microcontrollers).

If you’re just getting started with the Pico W, we recommend starting with MicroPython, which we’ll cover in this tutorial.

What is MicroPython?

Since its launch in 1991, Python has become one of the most popular programming languages in the world. It was designed for computer systems like desktops, laptops, and servers, but microcontroller boards like Raspberry Pi Pico W are smaller, simpler, and have significantly less memory to run Python.

That’s where MicroPython comes in. It is a Python-compatible programming language developed specifically for microcontrollers.

Installing MicroPython

Let’s install MicroPython on your Pico W.

1. Start by plugging a micro USB cable into the Pico W’s micro USB port.

2. Press and hold the ‘BOOTSEL’ button on the top of your Pico, then while still holding it down, connect the other end of the micro USB cable to one of your computer’s USB ports. Count to three and then release the ‘BOOTSEL’ button.

3. Your Pico should now appear as a removable drive, just as if you had connected a USB flash drive or external hard drive.

pico as a removable drive

4. In the File Explorer, you’ll see two files on your Pico: INDEX.HTM and INFO_UF2.TXT. The second file contains information about your Pico, such as the bootloader version it is currently using. The first file, INDEX.HTM, is the one you want. Double-click it to open it in your browser.

pico drive contents

5. It should load the official Raspberry Pi Pico documentation page. Click on the MicroPython option.

pico documentation page

6. Download the correct MicroPython UF2 file for your board. Once it has downloaded, close your browser window.

micropython uf2 file for pico w

7. Locate the file you just downloaded in the Downloads folder; it will have the extension ‘uf2’.

uf2 file in downloads folder

8. Copy this UF2 file to the Pico’s removable storage drive.

9. Within seconds, your Pico W will disappear from File Explorer, and the Windows system tray will report that it is Setting up a device. When the installation is finished, Windows will report that the Device is ready.

setting up a device notification
device is ready notification

When you copy the UF2 file to your Pico, you instruct it to flash the firmware onto its internal storage. To accomplish this, your Pico exits USB mass storage device mode, flashes the new firmware, and loads it.

Congratulations, your Pico W is now running MicroPython!

Installing Thonny Python IDE

Before you can begin programming Pico W with MicroPython, you must first install an integrated development environment (IDE). We will install Thonny, a popular IDE for MicroPython.

1. Launch your web browser and navigate to thonny.org

2. At the top of the page, you will see download links for Windows and macOS, and instructions for Linux. Download the installer for your operating system.

download thonny installer

3. Launch the installer and follow the installation wizard.

thonny installer

4. Once the installation is complete, launch the Thonny IDE.

A Quick Tour of the Thonny IDE

Thonny IDE’s UI is simple and can be divided into four areas.

thonny ide overview

1. Toolbar – The toolbar provides quick access to frequently used program functions such as saving, loading, and running programs.

2. Script Area – The script area is where you will write your MicroPython programs.

3. Python Shell – The Python Shell is where you can execute individual instructions. This is also referred to as REPL, which stands for ‘Read, Evaluate, Print, and Loop.’

4. Interpreter – This area displays and allows you to change the current Python interpreter – the version of Python used to run your programs.

Connecting Thonny to Pico W

To run your programs in MicroPython on your Pico W, you need to change the Python Interpreter in Thonny.

1. If you disconnected your Pico W, plug it back in.

2. Click on the word ‘Thonny’s Python’ at the bottom-right corner of the Thonny window. This shows your current interpreter.

current python interpreter in thonny

3. Look for ‘MicroPython (Raspberry Pi Pico)’ and select it from the list that appears. If you don’t see it in the list, make sure your Pico W is properly connected to the computer.

changing python interpreter in thonny

4. Look at the Python Shell area of the Thonny window. You’ll notice that it now says ‘MicroPython’ and that it’s running on a ‘Raspberry Pi Pico W’.

verify pi pico w interpreter

Congratulations, you are now ready to start programming!

Running First MicroPython Program

Now that you are all set up, let’s try to run your first line of code.

Click on the Python Shell area, then type the following instruction just to the right of the prompt >>> and hit Enter.

print("Hello, World!")

As soon as you hit ENTER, your instruction is executed instantly. That’s because the Shell is a direct link to the MicroPython interpreter running on your Pico W, whose job it is to interpret your instructions.

thonny shell hello world

This is known as interactive mode. In this mode, every expression and statement you type in is evaluated and executed immediately, similar to a face-to-face conversation in which the other person responds as soon as you finish speaking.

Writing and Saving Your Program to Pico W

Writing programs in interactive mode via the Shell is a great way to experiment with the language and test MicroPython code on the fly. However, it’s better to put longer programs in a file.

Click on the script area and type your program again.

print("Hello, World!")

When you press the ENTER key this time, nothing happens except for a new blank line in the script area. To run this version of your program, click the Run icon in the Thonny toolbar.

thonny run script icon

You’ll be prompted to save your program to either ‘This computer’ or ‘Raspberry Pi Pico’.

choose where to save a program in thonny

Choose ‘Raspberry Pi Pico’, then enter a suitable name, such as Hello_World.py, and click the OK button.

save program to pico

File Naming

  • You can give your programs almost any name you want, but try to make it descriptive of what the program does and avoid using boot.py and main.py, as these are special file names.
  • When saving programs to your Pico W, always use the .py file extension.

Once your program has been saved, it will run automatically on your Pico and the output will be displayed in the Shell area.

running a script in thonny

%Run -c $EDITOR_CONTENT is actually an instruction from Thonny telling the MicroPython interpreter on your Pico W to run the contents of the script area (i.e. EDITOR_CONTENT).

Loading a Saved Program

Click the Open icon from the Thonny toolbar.

thonny open program icon

Click ‘Raspberry Pi Pico’.

open saved program in thonny

You will see a list of all the programs you’ve saved to your Pico. Find Hello_World.py in the list and select it, then click OK.

select program to open

Your program will be loaded into Thonny, ready to be edited or run again.

program loaded into thonny

Blinking the On-board LED

Let’s create a MicroPython program to blink the on-board LED on and off in a loop.

Your Raspberry Pi Pico W has a small LED labeled ‘LED’ to the left of the micro-USB port. Unlike the Raspberry Pi Pico, the on-board LED on Pico W is connected to a GPIO pin (WL_GPIO0) on the Infineon CYW43439 wireless chip rather than a GP25 pin on the RP2040.

Now, enter the following program in Thonny’s script area.

import machine
import utime

led_onboard = machine.Pin("LED", machine.Pin.OUT)

while True:
    led_onboard.toggle()
    utime.sleep(1)

Click the Run button and save the program on your Pico W as Blink.py.

You’ll see the LED turn on for a second, turn off for a second, and turn on again – constantly repeating in an infinite loop. The LED will continue to blink until you click the Stop icon to cancel your program and reset your Pico W.

raspberry pi pico w onboard led blinking

Code Explanation:

The program starts with the following line.

import machine

This short line of code imports the ‘machine’ library. This library contains all the instructions MicroPython needs to communicate with the Pico W. Without this line, you won’t be able to control any of your Pico W’s GPIO pins.

Selective Imports

In both MicroPython and Python it’s possible to import part of a library, rather than the whole library. Doing so can reduce the amount of memory used by your program.

For instance, you could write from machine import Pin instead of import machine to instruct MicroPython to import only the “Pin” function from the “machine” library, rather than the whole library.

The next line imports the ‘utime’ library. This library handles everything related to time, from measuring it to inserting delays into your programs.

import utime

Next, an object called led_onboard is defined to refer to the on-board LED. For this, we use the Pin function from the machine library. This function, as its name suggests, is designed to handle your Pico W’s GPIO pins.

led_onboard = machine.Pin("LED", machine.Pin.OUT)

The Pin function accepts, at a minimum, two arguments: the pin number and the pin mode. To make things easier, the MicroPython firmware includes a constant called LED that allows you to easily control the built-in LED. Therefore, the first argument we’ll pass is LED. And the second will be machine.Pin.OUT, which tells Pico W that the pin should be used as an output rather than an input.

Next we use a while True loop to run our program endlessly.

while True:

Remember that the lines inside the loop must be indented by four spaces so that MicroPython knows they form a loop.

Now to instruct Pico W to turn on the LED, we use:

led_onboard.value(1)

And to turn it off, we use:

led_onboard.value(0)

However, instead of explicitly setting the LED’s output to 0 or 1, a toggle() function can be used to accomplish the same task.

led_onboard.toggle()

The toggle() function simply switches between on and off – if the pin is currently on, toggle() switches it off; if it’s off, toggle() switches it on.

To add a delay, we use the sleep function from the utime library, which causes the program to pause for whatever number of seconds you specify – in this case, one second.

utime.sleep(1)

No hay comentarios:

Publicar un comentario