Ceci est une ancienne révision du document !
It’s been a while since I’ve been able to write a ‘Micro This Micro That’ article. I apologize that it’s been so long. Too many things have been in my way, but at least for this month, I can provide you with something.
I also know that I had promised a long time ago that we would complete the compass app and I still intend to do that, but so much has changed in the Microcontroller world, I felt that I needed to discuss some of the new things. With any luck, the next ‘Micro This Micro That’ article will finish that.
For this month, we’ll be discussing the new Raspberry Pi Pico board, the Pico-W. You can find out a good bit of information from https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html. This webpage covers the entire RPi Pico family, the Original board, the Pico-H, and the Pico-W. We’ll ignore the Pico-H for now, other than to say that the Pico-H is pretty much the same board as the original Pico, but it has a three-pin keyed socket for the Serial wire debug connections. Here is the pinout for the Pico-W
You can see that the pinout is pretty much the same as the original Pico, so, for the most part, you can use the software you’ve already developed, but you need to get the latest MicroPython version that is designed to support the Pico-W. You can get that from the website above or from the MicroPython.org site. The process of flashing the board is the same as the original board, so I won’t bore you with that.
One of the new things that the Pico-W brings to the table is that the onboard LED pin is not connected to a GPIO pin on the board, but is connected to a GPIO pin on the wireless module (and the MicroPython version has been modified to handle it). So it makes it much easier to access it in your code. Here is a quick demo program that shows how to do it.
import machine
import time
led=machine.Pin(“LED”,machine.Pin.OUT)
led.on()
time.sleep(3)
led.off()
As you can see, it is very simple. We define the led object, then we simply call the led.on() and led.off methods to turn on and off the led. You can use this to provide a visual indication that you have a wireless connection.
Since the pinout of the Pico-W is pretty much the same as the original, we can use our original i2cscan program to probe the i2c bus and provide the address of all the i2c devices attached to our Pico-W (shown previous page, top right).
As I usually do, I connected the sda (data) line to physical pin 8 and the scl (clock) line to physical pin 9. When I ran the program I got the following output.
0x19 25 0x1e 30 0x40 64
Address 0x19 is the lsm303 accelerator and 0x1e is the lsm303 magnetometer. Address 0x40 is the SI7021 temperature/humidity sensor. (See, I AM going to finish the compass project!).
Since there are now three RPi Pico boards, how can you tell which device you are working with (other than looking at it)?
The micropython versions for the Pico boards now support a sys method named implementation (shown top right). Of course you have to import the sys module first. I threw together a simple program that shows how to access the information.
When you run the program, here’s what it returns.
(name='micropython', version=(1, 19, 1), _machine='Raspberry Pi Pico W with RP2040', _mpy=4102) Raspberry Pi Pico W with RP2040 Pico with Wireless
So, which1 returns a tuple that holds the fact that we are using Micropython, the version of Micropython installed, the type of machine, and a value for _mpy. The important part of this is the value located at position 2 of the tuple (remember this is zero based). Which1[2] returns a string which we can use the in operator of Python. So by checking for “Pico W” within the string, if we are running on a Pico-W, we will get back a True.
This whole thing can be distilled down to a simple function (shown bottom right).
And we get back True. I’ve saved this as WhichPico2.py.
Now, let’s look at how to work with the wireless portion of the Pico-W. It is very similar to what we’ve done with the ESP-32 and the ESP-8266 microcontrollers.
As a minimum, you need to import the network, time and a special file named secret.py.
Secret.py contains
# secret.py #
SSID = “” PASSWORD = “”
This way, you can keep the wireless access point and your password secret and have to provide only this sample file to anyone you share your code with. Be sure to modify the two values on your Pico-W before trying to run the program.
Once you have the imports section complete, you need to add only the following code (previous page, middle right).
We create the wlan object and set the active method to True in order to connect. Then we read the SSID and PASSWORD values from the secrets.py file and pass these to the connect method. We then need to wait until we get a True value from the isconnected method and a status greater than 0. This can sometimes take a few seconds, so be patient. Once we are connected, we print the status and the ifconfig information, and we can carry on with what we need to do.
Here is the output from the program
3
('192.168.1.195', '255.255.255.0', '192.168.1.1', '192.168.1.1')
The status value of 3 means that we are properly connected to the wireless network. The ifconfig information is (from left to right) IP address, netmask, gateway, DNS. Here are all the possible values for the status method.
Return value of cyw43_wifi_link_status #define CYW43_LINK_DOWN (0) #define CYW43_LINK_JOIN (1) #define CYW43_LINK_NOIP (2) #define CYW43_LINK_UP (3) #define CYW43_LINK_FAIL (-1) #define CYW43_LINK_NONET (-2) #define CYW43_LINK_BADAUTH (-3) I’ve created a simple program that rolls all of the test programs we’ve used into one. I’ve named it net1a.py. I’ve changed most of the comments into explanation lines here, to make it easier for the editors of FCM to include the full code. import machine import network import time import secret import sys Check that the board is a Pico-W with wireless support (previous page, top right). First, set up the led object and turn off the led (just in case). Next, set up all the things we need to do for the wireless support, including getting the SSID and PASSWORD from the secret.py file, and call the wlan.connect with these values (previous page, bottom right). In the event the board is NOT a Pico-W, print that we can’t deal with wireless on this board and end the program (top right). So that’s using the RPi Pico-W networking in a nutshell. The site I mentioned earlier has a very nice PDF on using the wireless portion of the Pico-W at https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf, as well as other important information about the board. I’ve set up a github repository for the code that we’ve used this month. You can find it at https://github.com/gregwa1953/FCM-185_MicroThisMicroThat. Until next time, as always; stay safe, healthy, positive and creative!