Connect to Your Wireless Network With a Raspberry Pi Pico W

Smartphones, tablets, and computers make connecting to your Wi-Fi at home, a coffee shop, or a local library painless and straightforward. That’s because they have an operating system that comes with built-in tools to handle wireless connectivity so we can take it for granted.

To get the most out of your Raspberry Pi Pico W, you’ll want to link up this device to your home’s wireless router serving up your home’s internet connection. Due to Pico’s lack on an OS, this process requires some manual coding to connect. With a little help, however, connecting your device is easier than you think.


Getting Started

Let’s review a few of the required items to ensure you have what you need to connect your Raspberry Pi Pico W to the internet:

  • IDE (programming application such as Thonny)
    • If you haven’t had your Raspberry Pi Pico W (Pico) in your possession for very long, be sure to review how to set up a MicroPython IDE with your Raspberry Pi Pico W.
  • 2.4GHz wireless connection
    • Confirm that you’re able to connect using a wireless 2.4GHz signal. For the time being, 5GHz internet wireless connectivity is not compatible with the Raspberry Pi Pico W.
  • Python code
    • You can download a copy of the code from the MakeOfUse GitHub repo where the required file is located.
  • Soldered header pins
    • You know you’re going to want to jump onto another creative project when you’re connected. It’s recommended to solder the header pins to your Raspberry Pi Pico W before pursuing this project.

Code to Connect

As the Raspberry Pi Pico W doesn’t have an operating system, you’ll need to manually instruct the microcontroller to connect to the internet using Python code. With the Pico connected via USB to your computer, locate the downloaded Python file and load it into your IDE in order to make some changes.

The SSID and password values are set as a placeholder values in the interest of privacy. You’ll need to change these values to the SSID (network name) of your wireless router and its password:

ssid = 'Enter Your SSID'
password = 'Enter your LAN password'

You’ll notice that the file includes importing network, time, and machine modules to include the necessary functions for this task. Otherwise, the instructions set out in this Python file are not complicated at all.

The system will check to see if the wlan.status is active, validate the SSID and password (before connecting), as well as run through some straightforward while loops to help your Raspberry Pi Pico W cycle through some basic connectivity troubleshooting.

Under 50 lines of code are required to instruct your Raspberry Pi Pico W how to connect to your home Wi-Fi connection. Not bad!

Under the #handle connection error comment in the file, you may opt to keep responses from your Raspberry Pi Pico W simple using the following conditional statement:

if wlan.status() != 3:
raise RuntimeError('network connection failed')

When you’re connected to your home Wi-Fi, you’ll see the word “connected” in the IDE’s Python Shell to confirm you’re online. If you prefer to make your Raspberry Pi Pico W’s on-board LED blink when connected, retain the following code:

if wlan.status() != 3:
raise RuntimeError('network connection failed')
s = 3
while s > 0:
s -= 1

This code, using a simple while loop, blinks the Raspberry Pi Pico W’s LED light three times if the network connection is successful. This is an excellent way to create a visual indicator when your next IoT project requires that your Raspberry Pi Pico W runs independently of your computer (or a display).

The final two lines combine text prompts and programmatic values to create an output such as: “Connected to FBI Van. IP: 192.168.X.XXX.” Does your neighbor have a crazy Wi-Fi SSID too?

status = wlan.ifconfig()
print( 'Connected to ' + ssid + '. ' + 'Device IP: ' + status[0] )

When you are all set, press the play button at the top of Thonny (or the equivalent with your favorite IDE) to run the code. With your Python code running, you should soon be connected to the internet. Having issues? The Raspberry Pi Foundation has a great troubleshooting guide that will help you get back on track.

Making Use of This New Connection

Consider opening a small web server, retrieving information from a popular website, or serving up a web page. The Raspberry Pi Foundation has fun projects that will introduce you to the idea of serving up simple web pages to client web browsers!

Now that you are able to connect your Raspberry Pi Pico W to the internet, the projects you can complete with this microcontroller are nearly endless!

Leave a Reply

Your email address will not be published. Required fields are marked *