Pico 2 W: join Wi-Fi, or start an access point of its own
A Raspberry Pi Pico 2 W joins the home network when it can and, when it can't, starts its own Wi-Fi access point, while it filters a sensor on its FPU and…
Communication · Intermediate
In a real product: Smart thermostat setup mode (Smart home): A new device that cannot find its network opens a setup access point, so a phone can still reach it and give it the password.
The Pico 2 W is a Pico 2 (an RP2350 with two Cortex-M33 cores) with the Pico W's Infineon CYW43439 Wi-Fi and Bluetooth chip beside it. Connected products often start this way: they try the network they know, and if it is not there they become an access point themselves, so the owner's phone can join them and set them up. This sketch does that, and keeps working on its sensor (a knob on GP26) the whole time.
What you'll learn
WiFi.begin()with a timeout, andWiFi.softAP()as the fallback when the network does not let the board in- That the board's LED is on the radio chip (
LED_BUILTIN, pin 64), so each change of it is a message to that chip - A float low-pass filter on the RP2350's FPU, and
analogWriteon GP15 showing the result
How it works
At start-up the arduino-pico core powers the CYW43439 through GP23 and loads its firmware over a three-wire SPI bus that a PIO state machine drives (GP24 data, GP25 chip select, GP29 clock). None of those pins are on the header, and GP29 also reads the supply (VSYS / 3) on the board.
WiFi.setTimeout(3000) makes WiFi.begin() wait at most twice that for the network and an address from its DHCP server, instead of fifteen seconds. When it gets one the sketch prints it and the board's LED stays on. When it does not, the sketch calls WiFi.softAP("pico2w-setup", "configure-me") and answers at 192.168.4.1 itself, the LED blinking while the access point waits.
loop() reads the knob with the ADC's 12 bits and filters it: filtered += 0.2f * (raw - filtered) is one multiply-add on the Cortex-M33's FPU. The LED on GP15 follows the filtered value.
The home network is the diagram's "network": TryEmbedded, WPA2 with the password lab-pass-123. As written the sketch joins it and gets 192.168.4.2. In the simulator nothing joins the board's own access point, so the fallback shows in the Serial Monitor only.
Try this
- Change the password in the sketch: the join fails (the Network panel says "wrong password") and the access point starts instead.
- Change the filter's 0.2 to 0.02 and turn the knob: the LED follows more slowly and more smoothly.
- Print
WiFi.RSSI()once joined: the signal strength the radio measures, in dBm.
sketch.ino
// Raspberry Pi Pico 2 W (Arduino core): join the home network, else start an access point of its own. A knob on
// GP26 is the sensor, filtered in float on the Cortex-M33's FPU; the LED on GP15 shows it by its brightness. The
// board's LED (on the radio, LED_BUILTIN) is steady in station mode and blinks as an access point.
#include <WiFi.h>
const int KNOB = 26;
const int LEVEL_LED = 15;
bool accessPoint = false;
float filtered = 0;
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
analogReadResolution(12);
WiFi.setTimeout(3000);
WiFi.begin("TryEmbedded", "lab-pass-123");
if (WiFi.status() == WL_CONNECTED) {
Serial.print("joined the network, address ");
Serial.println(WiFi.localIP());
} else {
accessPoint = WiFi.softAP("pico2w-setup", "configure-me");
Serial.print("could not join: access point pico2w-setup at ");
Serial.println(WiFi.softAPIP());
}
}
void loop() {
filtered += 0.2f * (analogRead(KNOB) - filtered); // a low-pass filter, one FPU multiply-add
analogWrite(LEVEL_LED, (int)(filtered / 16)); // 0 to 4095 as 0 to 255
static unsigned long last = 0;
if (millis() - last >= 250) {
last = millis();
digitalWrite(LED_BUILTIN, accessPoint ? !digitalRead(LED_BUILTIN) : HIGH);
Serial.printf("sensor %.2f V\n", filtered * 3.3f / 4095);
}
delay(10);
}