Raspberry Pi Monitoring System Via OLED Display Module

Introduction: Raspberry Pi Monitoring System Via OLED Display Module

About: Control systems and robotics engineer, nurgaliyev@shakhizat.info

In this tutorial I’ll explain how to setup 0.96 inch OLED display module for showing system information of Raspberry Pi 4 Model B using its I2C interface.

Supplies

Hardware Required:

  • Raspberry Pi 4 Model B
  • 128×64 OLED display module (SSD1306)
  • Connecting Wires

Step 1: Hardware Connection

Below are the connections of OLED module with Raspberry Pi 4 Model B:

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

Step 2: Enable I2C Interface

The I2C interface is disabled by default so you need to enable it. You can do this within the raspi-config tool on the command line by running:

sudo raspi-config
  1. A blue screen will appear. Now select Interfacing option.
  2. After this, we need to select I2C option.
  3. After this, we need to select Yes and press enter and then ok.
  4. After this, we need to reboot Raspberry Pi by typing below command:
sudo reboot

The following libraries may already be installed but run these commands anyway to make sure :

sudo apt-get install python-smbus
sudo apt-get install i2c-tools

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

On the older Raspberry Pi type the following command:

sudo i2cdetect -y 0

Here is the output I see on my Raspberry Pi 4 Model B:

0 1 2 3 4 5 6 7 8 9 a b c d e f

00: -- -- -- -- -- -- -- -- -- -- -- -- --

10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --

40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

70: -- -- -- -- -- -- -- --

It showed the device had been detected with an address of 0x3c. This is the default hex address for this type of device.

Step 3: Install Adafruit Python Library for OLED Display Module

To install the library we will clone the Adafruit git repository.

git clone  https://github.com/adafruit/Adafruit_Python_SSD13...

Once completes navigate to the library’s directory:

cd Adafruit_Python_SSD1306

and install the library for Python 2:

sudo python setup.py install

or for Python 3:

sudo python3 setup.py install

Step 4: System Monitor Python Script

Navigate into the examples directory:

cd examples

In this folder you should find example script:

  • stats.py
python3 stats.py

By default it shows memory usage, disk usage, CPU load and ip address. Also, b-prefix in front of each strings can be seen.

It will be slightly modified in order to get rid of the b-prefix and add CPU temperature of Raspberry Pi 4 Model B as well.

cmd = "hostname -I | cut -d\' \' -f1"

will be replaced by the following line:

cmd = "hostname -I |cut -f 2 -d ' '"

This code is perfect on boot when you want to find your Raspberry Pi's IP address for SSH or VNC.

The following lines will be added to show CPU temperature on OLED display module:

cmd = "vcgencmd measure_temp |cut -f 2 -d '='"
temp = subprocess.check_output(cmd, shell = True )

Below code was modified accordingly to remove 'b' character from the OLED display.

draw.text((x, top),  "IP: " + str(IP,'utf-8'), font=font, fill=255)<br>
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)

Finally, you should see something similar to the following output on OLED display:

Step 5: Running Stats.py 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:

sudo python /home/pi/stats.py &

  • Save and exit.
  • Reboot to verify that the screen comes up on boot!

Be the First to Share

    Recommendations

    • Maps Challenge

      Maps Challenge
    • First Time Author Contest

      First Time Author Contest
    • DIY Summer Camp Contest

      DIY Summer Camp Contest

    5 Comments

    0
    jaineelp
    jaineelp

    Question 8 days ago

    I m having this problem: please someone help me:
    OSError: [Errno 121] Remote I/O error

    My i2cdetect -y 0 output:
    0 1 2 3 4 5 6 7 8 9 a b c d e f
    00: -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --

    0
    petermcshee
    petermcshee

    5 months ago on Step 5

    Hi
    Thanks for posting this tutorial, i am getting errors and wondered if you could help:
    File "stats.py", line 47, in <module>
    disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
    File "/usr/local/lib/python3.7/dist-packages/Adafruit_SSD1306-1.6.2-py3.7.egg/Adafruit_SSD1306/SSD1306.py", line 288, in __init__
    File "/usr/local/lib/python3.7/dist-packages/Adafruit_SSD1306-1.6.2-py3.7.egg/Adafruit_SSD1306/SSD1306.py", line 85, in __init__
    File "/usr/local/lib/python3.7/dist-packages/Adafruit_GPIO-1.0.4-py3.7.egg/Adafruit_GPIO/GPIO.py", line 420, in get_platform_gpio
    ModuleNotFoundError: No module named 'Adafruit_BBIO'

    The module is being detected on the i2cdetect at 3c
    Regards
    Peter

    0
    nashdracones
    nashdracones

    Reply 5 months ago

    install the module:
    sudo pip3 install Adafruit_BBIO

    0
    winterMOO
    winterMOO

    11 months ago

    Hi there, thank you for this.

    I wanted to add another line showing disk space on my mounted USB. I tried copying the line with a new string and using 'df -h /dev/sda1' which works in the terminal but here no new line shows up. I'm sure it has something to do with me not understanding the '$NF==' stuff, any tips would be appreciated! Thanks again :)

    1597114738282980710134333854026.jpg