Pico W joins WiFi in MicroPython
MicroPython on a Pico W: network.WLAN joins the simulated access point with a ten-second limit and prints the IP address it was given. The whole build, a Raspberry Pi Pico W, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6. On Mokxi they're free.
What you'll learn
- Joining WiFi with network.WLAN in station mode
- Waiting with a timeout instead of forever
- Raising an error when something fails
- Putting a job in a function you can call again
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Raspberry Pi Pico W
- Parts: 1
How it works
Nothing is wired. The board joins a WiFi network as a client. Here that is Mokxi's simulated access point, named Mokxi-Lab with the password breadboard. At home, you would put in your own network's name and password.
The script calls connect, which waits up to ten seconds for the link. When it succeeds, the script prints the IP address the router handed out. If it times out, the script stops with an error saying it could not join.
Parts list
1 part, plus the jumper wires. They retail for about $6; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
Kits that can build it
These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Access to the network: join WiFi and print the address the router
# gives the board. Here the board joins Mokxi's simulated access point,
# whose name and password are the board's WiFi properties; at home, put
# in your own network's.
import network
import time
SSID = "Mokxi-Lab"
PASSWORD = "breadboard"
def connect(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
for _ in range(20): # give it ten seconds
if wlan.isconnected():
return wlan.ifconfig()[0]
time.sleep_ms(500)
raise RuntimeError("could not join " + ssid)
print("Connecting to", SSID)
ip = connect(SSID, PASSWORD)
print("Connected, IP address", ip)
while True:
time.sleep(1)
Code walkthrough
The key functions of the sketch, and what each one does.
connect main.py
Turns on the station interface and starts joining with wlan.connect. It then checks up to 20 times, half a second apart, which is ten seconds. If isconnected turns true it returns the IP address. If not, it raises a RuntimeError naming the network.
def connect(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
for _ in range(20): # give it ten seconds
if wlan.isconnected():
return wlan.ifconfig()[0]
time.sleep_ms(500)
raise RuntimeError("could not join " + ssid)Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- It stops with could not join This project
Check that SSID and PASSWORD match the network exactly. In Mokxi they are the board's WiFi properties. - Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts. - WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.