In this example, you'll test your Pico W WiFi connection by connecting to your SSID, printing your MAC address and IP address to the REPL and then pinging Google.

Add Your settings.toml File

Remember to add your settings.toml file as described in the Create Your settings.toml File page earlier in the guide. You'll need to include your CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD in the file. Note that Create Your settings.toml File uses different names for these values.

CIRCUITPY_WIFI_SSID = "your-ssid-here"
CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here"

Code the Basic WiFi Test

Once you've finished setting up your Pico W with CircuitPython, you can access the code by downloading the Project Bundle.

To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.

# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import os
import ipaddress
import wifi
import socketpool

print()
print("Connecting to WiFi")

#  connect to your SSID
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))

print("Connected to WiFi")

pool = socketpool.SocketPool(wifi.radio)

#  prints MAC address to REPL
print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])

#  prints IP address to REPL
print("My IP address is", wifi.radio.ipv4_address)

#  pings Google
ipv4 = ipaddress.ip_address("8.8.4.4")
print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))

Upload the Code and Libraries to the Pico W

After downloading the Project Bundle, plug your Pico W into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Pico W's CIRCUITPY drive. 

  • code.py

Your Pico W CIRCUITPY drive should look like this after copying the code.py file.

No libraries from the library bundle are used in this example, so the lib folder is empty. All of the libraries are a part of the core.
CIRCUITPY

Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!

Having Problems?

If you get the message "TypeError: object with buffer protocol required", likely CircuitPython cannot find the file settings.toml on your CIRCUITPY drive. Check to be sure the file name is correct and that it contains the CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD values as noted in the "Create Your settings.toml File" section.

If you get "Connecting to WiFi" but don't get to "Connected to WiFi", check to make sure the CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD values in settings.toml are valid for your 2.4GHz network. There is no retry in this code, you might have to run the program a second time to get a response.

How the Pico W Basic WiFi Test Works

In the basic WiFi test, the Pico W connects to your SSID by importing your SSID and SSID password from the settings.toml file.

wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))

Then, your MAC address and IP address are printed to the REPL.

#  prints MAC address to REPL
print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])

#  prints IP address to REPL
print("My IP address is", wifi.radio.ipv4_address)

Finally, google.com is pinged. The amount of time it takes to ping is printed to the REPL and the code stops running.

#  pings Google
ipv4 = ipaddress.ip_address("8.8.4.4")
print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))

By successfully running this WiFi test code, you can confirm that your Pico W is connecting to WiFi with CircuitPython successfully and you can move on to more advanced projects.

This guide was first published on Oct 14, 2022. It was last updated on Mar 29, 2024.

This page (Pico W Basic WiFi Test) was last updated on Mar 29, 2024.

Text editor powered by tinymce.