Getting Started With Raspberry Pi Pico W Using MicroPython
Getting Started With Raspberry Pi Pico W Using MicroPython
Overview
This is a getting started tutorial with Raspberry Pi Pico W, a brand new exciting
Microcontroller board based on RP2040 Microcontroller & CYW43439 WiFi+BLE Chip. The
Raspberry Pi Pico W is a low-cost Arm-based microcontroller that we can program
using C/C++ and MicroPython.
Raspberry Pi Pico W also adds on-board single-band 2.4GHz wireless interfaces (802.11n)
using the Infineon CYW43439 while retaining the Pico form factor, so in addition to the basic
GPIO function, it can also connect to the network so we can use it for some IoT projects. For
example, using IFTTT for a security system, building a cloud player and a cloud service bell
system using MQTT, and so on.
The tutorial covers the overview of Raspberry Pi Pico W, its features & specifications. The
detailed guide of Raspberry Pi Pico W Pins like ADC pins, I2C Pins, SPI Pins, UART, etc can
help you to interface any sensors or module with this powerful board.
Later we will we through the MicroPython Setup & programming like Blinking of LED,
Scanning WiFi Networks & Connecting to the WiFI Network.
RP2040 Microcontroller
Earlier all the Raspberry Pi boards like Raspberry Pi 3 or 4 or Raspberry Pi Zero featured
Broadcom Processors like BCM2835, BCM2836, BCM2711 etc. The RP2040 chip was
announced on 21st January 2021 and is the first processor designed by the Raspberry Pi
Foundation.
The RP2040 is a 32-bit dual ARM Cortex-M0+ microcontroller integrated circuit released at
the same time as part of the Raspberry Pi Pico board. The processor is a low-cost microcontroller
and costs around US$4. The chip is 40nm silicon in a 7×7 mm QFN-56 package.
The RP2040 contains two ARM Cortex-M0+ cores clocked at 133 MHz together with 264
KB of RAM. The Program memory is external and supports up to 16 MB. The device has
everything you expect from a modern microcontroller like UARTS, SPI, and I2C ports, and
there are timers, PWM, DMA, and a 12-bit analog-to-digital converter (ADC).
WiFi Features
Wi-Fi 4 (802.11n), Single-band (2.4 GHz)
1×1 SISO
20 MHz channels, up to 96 Mbps PHY data rate
Integrated internal PA, LNA, and T/R switch supports a single antenna shared between
Wi-Fi and Bluetooth
Raspberry Pi Pico W pairs RP2040 with 2MB of flash memory, and a power supply chip
supporting input voltages from 1.8–5.5V. It provides 26 GPIO pins, three of which can function
as analog inputs, on 0.1”-pitch through-hole pads with castellated edges.
The CYW43439 wireless chip is connected via SPI to the RP2040. While the CYW43439
supports both 802.11 wireless and Bluetooth, initially Pico W does not have Bluetooth support.
Support may be added later, and will use the same SPI interface. If support is added existing
hardware may require a firmware update to support Bluetooth, but there will be no hardware
modifications needed.
21 mm × 51 mm form factor
RP2040 microcontroller chip designed by Raspberry Pi in the UK
Dual-core Arm Cortex-M0+ processor, flexible clock running up to 133 MHz
264kB on-chip SRAM
2MB on-board QSPI flash
2.4GHz 802.11n wireless LAN
26 multifunction GPIO pins, including 3 analog inputs
2 × UART, 2 × SPI controllers, 2 × I2C controllers, 16 × PWM channels
1 × USB 1.1 controller and PHY, with host and device support
8 × Programmable I/O (PIO) state machines for custom peripheral support
Supported input power 1.8–5.5V DC
Operating temperature -20°C to +70°C
Castellated module allows soldering directly to carrier boards
Drag-and-drop programming using mass storage over USB
Low-power sleep and dormant modes
Accurate on-chip clock
Temperature sensor
Accelerated integer and floating-point libraries on-chip
There are 40-pin on the Raspberry Pi Pico. The functionality of all the pins is explained below.
GP0-GP28: These are General-purpose input/output pins. It act as either input or output
and have no fixed purpose of its own
GND: This is a 0 volts ground Several GND pins around Pico W to make wiring easier.
RUN: It enables or disables Pico. You can start and stop Pico W from another
microcontroller.
GPxx_ADCx: General-purpose input/output or analog input. It can be used as an analog
input as well as a digital input or output – but not both at the same time.
ADC_VREF: Analog-to-digital converter (ADC) voltage reference. A special input pin
that sets a reference voltage for any analog inputs.
AGND: Analog-to-digital converter (ADC) 0 volts ground. A special ground connection
for use with the ADC_VREF pin.
3V3(O): 3.3 volts power. A source of 3.3V power, the same voltage your Pico W runs at
internally, generated from the VSYS input.
3v3(E): Enables or disables the power. Switch on or off the 3V3(O) power, can also
switches your Pico W off.
VSYS: 2-5 volts power. A pin directly connected to your Pico’s internal power supply,
which cannot be switched off without also switching Pico W off.
VBUS: 5 volts power. A source of 5 V power taken from your Pico’s micro USB port
and used to power hardware which needs more than 3.3 V.
Before you start using Raspberry Pi Pico W, you have to solder 40-pin male headers, 20 on
each side of the board.
But I will recommend using MicroPython to program the Raspberry Pi Pico W Board.
MicroPython is a Python Language Interpreter that is developed for Microcontrollers
and embedded systems. The Syntax for MicroPython is very similar to Python. So, if you
worked with Python, then working with MicroPython will be very easy.
To program the Raspberry Pi Pico using Micropython, we will use Thonny IDE:
But before getting started with Raspberry Pi Pico W, you have to install MicroPython on
Raspberry Pi Pico W Board.
Push and hold the BOOTSEL button on the Pico W, & then immediately connect the Pico W
Board to your computer using a micro USB cable. Release BOOTSEL once the drive RPI-RP2
appears on your computer.
Visit the Raspberry Pi Pico W Official Documentation page from here: Raspberry Pi
Datasheets.
Download the MicroPython UF2 file from the MicroPython tab.
Drag and drop the UF2 file onto the RPI-RP2 drive. The Raspberry Pi Pico W will reboot and
will now run MicroPython.
Now you can start using the Raspberry Pi Pico W with MicroPython on Thonny IDE & make
unlimited IoT projects.
1 import sys
2 sys.implementation
The directory can also be viewed using following command.
1 import uso
2 uso.listdir()
The Python shell will following message which means the program will run without any error.
The program will run and you can see the LED toggling in the board
Scanning WiFi Access Points
The Raspberry Pi Pico W has CYW43439 2.4-GHz Wi-Fi chip from Infineon. So the Board can
connect to the WiFi network and all the features related to WiFi can be accessed as well.
Lets scan all the available Network Access Points using the Raspberry Pi Pico W working in a
station Mode.
In the Thonny editor, paste the following code and save it with any name like main.py.
1 import network
2 wlan=network.WLAN(network.STA_IF)
3 wlan.active(True)
4 accesspoints = wlan.scan()
5 for ap in accesspoints:
6 print(ap)
Now hit the run button to run the code.
The WiFi chip will scan all the available network and shows the list of all the network in Shell
Window.
1 import network
2 wlan = network.WLAN(network.STA_IF)
3 wlan.active(True)
4 wlan.connect("SSID","PASSWORD")
5 print(wlan.isconnected())
Now Run current script. If the connection is successful, true will be printed.
Create a new script by clicking the New button on Thonny IDE. Copy and paste the following
code. Replace the WiFi credentials.
1 import network
2 import time
3
4 wlan = network.WLAN(network.STA_IF)
5 wlan.active(True)
6 wlan.connect("SSID","PASSWORD")
7
8 # Wait for connect or fail
9 wait = 10
10 while wait > 0:
11 if wlan.status() < 0 or wlan.status() >= 3:
12 break
13 wait -= 1
14 print('waiting for connection...')
15 time.sleep(1)
16
17 # Handle connection error
18 if wlan.status() != 3:
19 raise RuntimeError('wifi connection failed')
20 else:
21 print('connected')
22 print('IP: ', wlan.ifconfig()[0])
Now run the script.
For more information about the WiFi commands, refer to the class WLAN MicroPython
documentation.
secrets.py
Copy the following code into a new script file on Thonny. Note to
change SSID and PASSWORD to yours. Set the file name to secrets.py
1 secrets = {
2 'ssid': 'SSID',
3 'password': 'PASSWORD',
4}
do_connect.py
Copy the following code into a new script file, and save it to Raspberry Pi Pico
as do_connect.py.
1 import network
2 import time
3 from secrets import *
4
5 def do_connect(ssid=secrets['ssid'],psk=secrets['password']):
6 wlan = network.WLAN(network.STA_IF)
7 wlan.active(True)
8 wlan.connect(ssid, psk)
9
10 # Wait for connect or fail
11 wait = 10
12 while wait > 0:
13 if wlan.status() < 0 or wlan.status() >= 3:
14 break
15 wait -= 1
16 print('waiting for connection...')
17 time.sleep(1)
18
19 # Handle connection error
20 if wlan.status() != 3:
21 raise RuntimeError('wifi connection failed')
22 else:
23 print('connected')
24 ip=wlan.ifconfig()[0]
25 print('network config: ', ip)
26 return ip
main.py
Calling the above script in other scripts by the following will allow the Raspberry Pi Pico W to
connect to the network.
The Kit contains 450+ components including all the active-passive components, sensors,
modules, ICs, etc. Using the kit you can make a total of 117 projects using MicroPython &
C/C++ Code.
Reference Link
https://how2electronics.com/getting-started-with-raspberry-pi-pico-w-using-micropython/