Skip to content

Displaying Sensor Data on OLED with Raspberry Pi

This tutorial serves as an example for how to acquire data from a sensor Wireling and output that data for display on an OLED Wireling using a Raspberry Pi. This same concept can be applied for use with any sensor Wireling and any OLED display Wireling. For this example, a BME680 and 0.96" OLED are used due to the variety of sensor values available to us.


Materials

Hardware

Required Software


Hardware Assembly

If you haven't already, attach the Wireling Adapter Raspberry Pi Hat to your Raspberry Pi. Then, using Wireling cables, plug the 0.96" OLED Wireling into Port 0 and the BME680 Wireling into Port 1 of the Pi Hat.


Software Setup

Before running this program, be sure you have installed the BME680, SSD1306 Python packages. This can be accomplished by running the following commands in the Pi's terminal, respectively:

pip3 install adafruit-circuitpython-bme680
pip3 install adafruit-circuitpython-ssd1306

Upload Program

Create a new python file in whichever directory you choose by typing the following into the terminal:

nano BME680_Display.py

This will open up the text editor nano where you can simply paste the following code:

BME680_Display.py
from busio import I2C
import adafruit_bme680
import time
import board
import tinycircuits_wireling

# For OLED
import busio
from digitalio import DigitalInOut
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306

# Initialize and enable power to Wireling Pi Hat
wireling = tinycircuits_wireling.Wireling()
OLED96_port = 0
BME680_port = 1
wireling.selectPort(BME680_port)

# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c, 0x76)

# change this to match the location's pressure (hPa) at sea level
bme680.sea_level_pressure = 1013.25

# A reset line may be required if there is no auto-reset circuitry
reset_pin = DigitalInOut(board.D10)

# use the 0.96" OLED Screen
wireling.selectPort(OLED96_port)
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c, reset=reset_pin) # 0.96" Screen

# Load a font
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 14)

while True:
    # Select the sensor port to get data
    wireling.selectPort(BME680_port)

    tmpC = bme680.temperature
    tmpF = (tmpC * 1.8) + 32

    tmp = "Temp: %0.1f F" % tmpF
    voc = "Gas: %d ohm" % bme680.gas
    hum = "Hum: %0.1f %%" % bme680.humidity
    prs = "Pres: %0.3f hPa" % bme680.pressure
    alt = "Alt: %0.2f m" % bme680.altitude

    # Select the OLED port for displaying data
    wireling.selectPort(OLED96_port)

    # Create blank image for drawing.
    display.fill(0)
    image = Image.new('1', (display.width, display.height))
    draw = ImageDraw.Draw(image)

    # Draw the text
    draw.text((0, 0), tmp, font=font, fill=255)
    draw.text((0, 12), voc, font=font, fill=255)
    draw.text((0, 24), hum, font=font, fill=255)
    draw.text((0, 36), prs, font=font, fill=255)
    draw.text((0, 48), alt, font=font, fill=255)

    # Display image
    display.image(image)
    display.show()
    time.sleep(.25)

After pasting the code, press Ctrl+O to write out the buffer, press Enter to save the buffer to BME680_Display.py, and then press Ctrl+X to exit.

Before running this program, be sure that the pigpio service is running on your Pi. This service can be started by typing:

sudo pigpiod

After this, you will be able to run the program by running the following command in the terminal:

sudo python3 BME680_Display.py

The program should now run and the sensor values will be printed to the OLED display. Terminate execution at any time by pressing Ctrl+C.

BME680 readings displayed on a 0.96" OLED


Contact Us

If you have any questions or feedback, feel free to email us or make a post on our forum. Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it.

Thanks for making with us!