ENV IV in Micropython with an OLED Part 2
2024-05-22 | By M5Stack
License: General Public License Bluetooth / BLE Displays Wifi Arduino ESP32 M5Stack Raspberry Pi
* Thanks for the source code and project information provided by @Adam Bryant
Things used in this project
Raspberry Pi Pico
M5Stack ENV IV
M5Stack OLED Unit 1.3" 128 × 64 Display
M5Stack Grove T
Software apps and online services
M5Stack UIflow2
How to program the M5Stack ENV IV unit in Micropython on the M5Stack Core and the Raspberry Pi Pico W And Display the values on the M5Stack OLED Unit.
In the previous guide found here: https://www.digikey.com/en/maker/projects/env-iv-in-micropython-part-1/3d2741f3e2b64536b8067206f4225e74 I showed you how to use the M5Stack ENV IV unit on the RPI Pico W. In this guide, I will expand that project by adding an M5Stack I2C OLED Unit to display the values.
In the previous step, I left the RPI Pico displaying the values to Thonny's shell.
In order to display the values I need to add the OLED Unit and to connect the display to the ENV IV, I also need to add a Grove T.
Once connected I then need to find a library to control the screen. The library come from: https://github.com/peter-l5/SH1107
Copy the sh1107.py file to the root of the pico as shown:
Then I change the previous code to the following:
from machine import I2C, Pin
import bmp280
from micropython_sht4x import sht4x
import sh1107
import time
i2c = I2C(1, sda=Pin(2), scl=Pin(3), freq=400000)
display = sh1107.SH1107_I2C(128, 64, i2c, address=0x3c, rotate=180)
sht = sht4x.SHT4X(i2c)
bmp = bmp280.BMP280(i2c)
bmp.oversample(bmp280.BMP280_OS_HIGH)
bmp.use_case(bmp280.BMP280_CASE_WEATHER)
temperature, relative_humidity = sht.measurements
while True:
display.sleep(False)
display.fill(0)
display.text(f"Temp: {temperature:.2f}C", 0, 10, 1)
display.text(f"Hum: {relative_humidity:.2%}", 0, 20, 1)
display.text(f"Press: {bmp.pressure}Pa", 0, 30, 1)
print(f"SHT40 Temperature: {temperature:.2f}°C")
print(f"SHT40 Relative Humidity: {relative_humidity:.2%}%")
print("")
print("BMP280 tempC: {}".format(bmp.temperature))
print("BMP280 pressure: {}Pa".format(bmp.pressure))
print("")
display.show()
time.sleep(0.5)
And then save the file and hit run to display the values on the OLED Units screen.
Contact Me if more support is needed.
https://twitter.com/Cpt_J_Purcell
https://bsky.app/profile/jamespurcell.bsky.social
On Discord (if I ever work out how to share the profile!)https://mastodonapp.uk/@AdamBryant
And on the M5Stack Facebook group and community forum.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum