Deep Dive into Reading

Raspberry Pi Z W Monitoring Humidity and Temperature Via OLED Display Module


Hardware Required:

  • Raspberry Pi zero w
  • Sensors
  • 128×64 OLED display module (SSD1306)
  • Connecting Wires

If you already know how to setup the sensor, you only need the Python code below, if not, go to step by step instructions.

Python code

# -*- coding:utf-8 -*-
import ctypes
import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

import subprocess
RST = None     # on the PiOLED this pin isnt used
# Note the following are only used with SPI:
DC = 23
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)

# Clear display.

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image ='1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0

# Load default font.
font = ImageFont.load_default()

class SHTC3:
    def __init__(self):
        self.dll = ctypes.CDLL("./")
        init = self.dll.init
        init.restype = ctypes.c_int
        init.argtypes = [ctypes.c_void_p]

    def SHTC3_Read_Temperature(self):
        temperature = self.dll.SHTC3_Read_TH
        temperature.restype = ctypes.c_float
        temperature.argtypes = [ctypes.c_void_p]
        return temperature(None)

    def SHTC3_Read_Humidity(self):
        humidity = self.dll.SHTC3_Read_RH
        humidity.restype = ctypes.c_float
        humidity.argtypes = [ctypes.c_void_p]
        return humidity(None)

if __name__ == "__main__":
    shtc3 = SHTC3()
    while True:
        print('Temperature = %6.2f°C , Humidity = %6.2f%%' % (shtc3.SHTC3_Read_Temperature(), shtc3.SHTC3_Read_Humidity()))
        draw.rectangle((0,0,width,height), outline=0, fill=0)

        # Shell scripts for system monitoring from here :
        cmd = "hostname -I | cut -d\ '\' -f1"
        IP = subprocess.check_output(cmd, shell = True )
        cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
        CPU = subprocess.check_output(cmd, shell = True )
        cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
        MemUsage = subprocess.check_output(cmd, shell = True )
        cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
        cmd = "vcgencmd measure_temp | cut -f 2 -d '='"
        temp = subprocess.check_output(cmd, shell= True )
        Disk = subprocess.check_output(cmd, shell = True )
        tempe =  str('%6.2f°C'% shtc3.SHTC3_Read_Temperature())
        hum = str('%6.2f%%' % shtc3.SHTC3_Read_Humidity())  
        # Write two lines of text. Choose whatever you want to view on the screen!

        draw.text((x, top),       "IP: " + str(IP, 'utf-8'),  font=font, fill=255)
        #draw.text((x, top+8),     str(CPU, 'utf-8') + " " + str(temp,'utf-8'), font=font, fill=255)
        #draw.text((x, top+16),    str(MemUsage, 'utf-8'),  font=font, fill=255)
        #draw.text((x, top+25),    str(Disk, 'utf-8'),  font=font, fill=255)
        #draw.text((x, top+8),     "Temp:" + str(temp, 'utf-8'), font=font, fill=255)
        draw.text((x, top+16),  "Temperature:" + tempe, font=font, fill=255)
        draw.text((x, top+25), "Humidity: " + hum, font=font, fill=255)
        time. Sleep(.1)

Below are the connections of OLED module with Raspberry zero w:

  • SDA ==> GPIO 2(pin 3)
  • SCL ==> GPIO 3(pin 5)
  • VCC ==> 3.3V(pin 1)
  • GND ==> GND(pin 14)

Step by step Instructions

Step 1- Install OS

Install Raspberry Pi OS lite or Desktop environment, instruction here

Step 2- Enable SHH in raspberry Pi

To enable SSH access on your headless Raspberry Pi, simply create an empty file called  ssh at the root of your SD card.

Then, create a file at the root of the SD card called wpa_supplicant.conf. Replace WIFI_NAME and WIFI_PASSWORD with the actual name and password for your WiFi network.


ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

Find the IP Address of Raspberry Pi, how? In windows terminal type:

ping raspberrypi.local

Connecting to pi via terminal

In windows terminal type: ssh pi@”your_ip_address_here”. The default Raspberry Pi username is pi and the default password is raspberry.


Step 3 setting up the sensor

Open the Raspberry Pi terminal and enter the following command to enter the configuration interface.

sudo raspi-config 

In the interface enable iC2: Choose Interfacing Options -> I2C ->yes start i2C kernel driver and, reboot Raspberry Pi with the command:

sudo reboot

If you want to use WiringPi library instead of Python read this first and then install the libraries following these instructions.

Installing Python and libraries. 
sudo apt-get update
sudo apt-get install python-pip 
sudo pip install RPi.GPIO
sudo pip install spidev
sudo apt-get install python-smbus
sudo apt-get install python-pip3 
sudo pip3 install RPi.GPIO
sudo pip3 install spidev
sudo apt-get install python3-smbus

Open Python and write the program describe above save it with the name you want, in my case was

To run the program, type on terminal:

sudo python3

Step 4 working with the display

To find a list of the devices connected to the I2C bus on the Raspberry Pi you can use the following command:

sudo i2cdetect -y 1

 Install Adafruit Python Library for OLED Display Module

git clone

Once completes navigate to the library’s directory:

cd Adafruit_Python_SSD1306
sudo python3 install

Step 5: Running “” on Startup

You can easily make it, so this program runs every time you boot your Raspberry Pi.

The fastest and easiest way is to put it in /etc/rc.local. Run the bellow command on terminal:

sudo nano /etc/rc.local

Scroll down, and just before the exit 0 line, enter the following (replace xxx with the name of your file):

sudo python /home/pi/ &
  • Save and exit.
  • Reboot to verify that the screen comes up on boot!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at

%d bloggers like this: