Read and Display Environment Sensor Data Using Raspberry Pi Pico and CircuitPython

Read environment sensor data and display it to OLED using Maker Pi Pico and CircuitPython.

Introduction

In this tutorial, we will try to read sensor data from M5Stack Environment Sensor Unit II and display it to the OLED Grove module using I2C communication. The advantage of using I2C is we can use a single bus (SDA, SCL) to communicate with multiple devices with different addresses. However, for this tutorial we would like to test on different I2C ports for sensors and display.

Video

This video shows how to read environment sensor data and display it to OLED using Maker Pi Pico and CircuitPython.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is CircuitPython sample program to read environment sensor data and display it to OLED through I2C communication using Maker Pi Pico and CircuitPython. Please include those CircuitPython libraries in the Raspberry Pi Pico CIRCUITPY drive.

  • adafruit_bus_device
  • adafruit_display_text
  • adafruit_bmp280.mpy
  • adafruit_displayio_ssd1306.mpy
  • adafruit_sht31d.mpy
#
# Read and display evironment sensor data using Raspberry Pi Pico and CircuitPython
#
# References and credit to
# – https://learn.adafruit.com/monochrome-oled-breakouts/circuitpython-usage
# – https://learn.adafruit.com/adafruit-bmp280-barometric-pressure-plus-temperature-sensor-breakout/circuitpython-test
# – https://learn.adafruit.com/adafruit-sht31-d-temperature-and-humidity-sensor-breakout/python-circuitpython
#
# Raspberry Pi Pico
# – [Maker Pi Pico] https://my.cytron.io/p-maker-pi-pico?tracking=idris
# Grove – OLED Display 0.96 inch
# – https://my.cytron.io/p-grove-oled-display-0p96-inch-ssd1315
# M5Stack Environment Sensor Unit II
# – https://my.cytron.io/p-m5stack-environment-sensor-unit-ii
#
# Additional Libraries
# – adafruit_bus_device
# – adafruit_display_text
# – adafruit_bmp280.mpy
# – adafruit_displayio_ssd1306.mpy
# – adafruit_sht31d.mpy
#
# Update:
# 21 Feb 2021 – Tested with CircuitPython Pico 6.2.0-beta.2
#
import time
import board
import busio
import displayio
import terminalio
from adafruit_display_text import label
import adafruit_displayio_ssd1306
import adafruit_bmp280
import adafruit_sht31d
displayio.release_displays()
i2c_oled = busio.I2C(scl=board.GP5, sda=board.GP4)
display_bus = displayio.I2CDisplay(i2c_oled, device_address=0x3C)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64)
i2c_sensor = busio.I2C(scl=board.GP3, sda=board.GP2)
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c_sensor, address=0x76)
sht30 = adafruit_sht31d.SHT31D(i2c_sensor, address=0x44)
while True:
print("\nTemperature: {:.2f} C".format(sht30.temperature))
print("Humidity: {:.2f} %".format(sht30.relative_humidity))
print("Pressure: {:.2f} hPa".format(bmp280.pressure))
print("Altitude = {:.2f} meters".format(bmp280.altitude))
# Make the display context
text_group = displayio.Group(max_size=10)
# Draw a label
text = "ENVIRONMENT SENSOR"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF, x=0, y=4)
text_group.append(text_area)
text = "Temp (C): {:.2f}".format(sht30.temperature)
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF, x=0, y=17)
text_group.append(text_area)
text = "Humi (%): {:.2f}".format(sht30.relative_humidity)
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF, x=0, y=30)
text_group.append(text_area)
text = "Pres (hPa): {:.2f}".format(bmp280.pressure)
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF, x=0, y=43)
text_group.append(text_area)
text = "Alti (m): {:.2f}".format(bmp280.altitude)
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF, x=0, y=56)
text_group.append(text_area)
display.show(text_group)
time.sleep(2)

view raw
code.py
hosted with ❤ by GitHub

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.

1 thought on “Read and Display Environment Sensor Data Using Raspberry Pi Pico and CircuitPython”

  1. I’m trying to convert the temperature from Celsius to Fahrenheit. I was able to change the display to say F instead of C.
    I can’t figure out where to put the conversion equation in the code.
    I just keep getting an error.
    I’m sure I can figure it out but as of now I haven’t been able to.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

DIY Digital Alarm Clock Using REKA:BIT With Micro:bit
Display Internet Time (NTP) on micro:bit
DIY Interactive Robot Using REKA:BIT With Micro:bit
BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
Tutorials of Cytron Technologies Scroll to Top