Raspberry Pi Pico W With BME280 and OLED in Thonny and MicroPython
by AZDelivery in Circuits > Arduino
22 Views, 0 Favorites, 0 Comments
Raspberry Pi Pico W With BME280 and OLED in Thonny and MicroPython
.png)
In this instructable we will show you how to connect the Raspberry Pi Pico W with a BME280 or DHT20 Sensor and an OLED using Micropython
Supplies
Pinout Diagram of the Raspberry Pi Pico W
In this article I want to take advantage of the WLAN interface and provide temperature and relative humidity with the web server in the home network. However, I will use the DHT20 sensor worth the price and the 1.3" OLED with SH1106 driver IC to minimize repetition.
But first the pinout diagram of the Raspberry Pi Pico W and the differences between the Raspberry Pi Pico and the Pico W:
Although the form factor and pinout have remained (largely) the same, the differences in layout are readily apparent. This is most obvious with the WLAN chip and the three mostly free pins for DEBUG, which are now located in the middle of the chip because of the WLAN antenna. The connector for the built-in LED has changed; more about that later. The WLAN interface with 2.4 GHz uses the chip Infineon CYW43439. Bluetooth is not yet implemented; this requires a firmware update, which is not planned in the near future. The latest version of the MicroPython firmware can be found under https://micropython.org/download/rp2-pico-w/.
After downloading the latest file with the extension .uf2 the Pico is plugged into the USB port of a computer with the BOOTSEL key pressed; then it is shown there as USB drive RPI-RP2; see the following picture. Simply copy the downloaded file into the directory. After the copying process is complete, the drive disappears and the Pico W is recognized under Thonny as a virtual COM port.
For those who have not yet installed or updated Thonny, here is the link once again:
Download from https://thonny.org/ or https://github.com/thonny/thonny/releases
Current as of October 2022: thonny-4.0.1.exe
Settings are made under the Run/Execute tab.
As usual, we first test "hello world" in the command line (also called shell or REPL).
The "hello world" in physical computing is the blinking LED, our second attempt:
In the program you can see the difference mentioned above for the built-in LED. With the simple Pico this is controlled via Pin 25 a pin that is not connected to the outside. With the Pico W, the LED is connected to the wireless chip and is addressed via the name "LED".
Rebuild Jörn Weise's Experimental Setup
After everything worked out, I would like to rebuild Jörn Weise's experimental setup first, to then use both another sensor, as well as another display.
Besides the MicroPython modules machine and utime the modules bme280 and ssd1306 for the 0.96" OLED. Both can be found at PyPI By clicking on "Manage packages ..." under the "Extras" tab and entering the terms in the search window. When selecting, please pay attention to the word "micropython" in the name.
Schematic:
Setup worked, see photo:
Replace the 0.96" Display With a 1.3" OLED
Now I first want to replace the 0.96" display with a 1.3" OLED. But this does not have the driver IC SSD1306, but the SH1106; and there is no MicroPython module for this on PyPI. The search on GitHub is quick: https://github.com/robert-hh/SH1106
The module is actually written for the ESP8266, but works fine. So download the ZIP file, unzip it, sh1106.py under Thonny and install it on the Pico W in the directory lib directory. The only deviation for the Pico W in the i2c sample program is an additional parameter during instantiation, the number for the i2c interface:
i2c=I2C(0, sda=sda, scl=scl, freq=400000)
When instantiating the display I had to insert rotate=180:
oled = sh1106.SH1106_I2C(128, 64, i2c, None, 0x3c,rotate=180)
With this it worked right away. So go to the DHT20, instead of the expensive BME280.
I had already used the DHT20 in a blog post in July 2022 introduced. Interesting is the i2c interface, where this combined temperature and humidity sensor differs from the DHT11 and DHT22. There is no MicroPython module on PyPI for this yet either. So again I search on GitHub. I find what I am looking for at https://github.com/flrrth/pico-dht20
Again download ZIP, unzip, dht20.py run it with Thonny and save it on the Pico W in the subdirectory lib subdirectory.
Here are the main new lines in the code:
And in the while loop:
Here the whole program for Download.
Connecting to the Internet With Pico-w
Now all that is missing is the addition that turns the Pico W into a web server for atmospheric data. Research on the Internet. So far missing. But there are two example programs from the Raspberry Pi Foundation, which have been copied By other bloggers: A simple web server with text Display and switching of the LED via the intranet, to be found in the brochure "Connecting-to-the-internet-with-pico-w“.
The part for logging into the home WLAN will always be needed:
In the lines
please enter the SSID and password for your home network.
As a basis for my project I use the sample program for the text output. Here I basically have to increase the font size of the second line to be able to read the text on the smartphone, as well as insert the measured values as a string. The measured values are updated every 10 seconds based on the HTML line
{output}
Finally I created the program under the name main.py on the Pico W. With this, the program starts By itself when the Pico W is powered. Thus, the monitoring of the garden shed, or storage rooms is easily possible.
Conclusion: For less than two euros extra compared to the Pico without WLAN, you get a versatile microcontroller with the Pico W, which can be easily registered in the home WLAN and is therefore suitable for many own smart home projects. I myself will soon use it for my favorite project Robot Car. I do not expect any difficulties; I will report back.