SSD1306 Oled Raspberry Pi

Hey Guys, welcome back to my posts. Today I’m gonna discuss a very important tutorial with you. We are going to interface SSD1306 OLED with Raspberry Pi. We gonna use SPI communication between OLED and PI to send data. Also all code and libraries used included and steps to obtain it are given. So, without wasting time let’s start our tutorial.

SSD1306 OLED:

  • SSD1306 OLED is a monochrome display that uses a ssd1306 chip to control it. It comes in two sizes, 128×32 and 128×64.
  • Today I’m using 128×64 one which has 7 pins. There are two variants of it, one with I2C communication and the other with SPI communication.
  • The difference between these two communications are explained clearly in the detailed explanation of these communications in basic electronics section.
  • I2C communication uses two pins SCL and SDA which are clock and serial data pins.
  • They send Serial Clock and Serial Data to all sensor connected to it, but according to the working of the I2C communication, only the referred sensor responds to that data.
  • SPI communication uses 4 or 3 wires, depends on the sensor. SCK (Serial Clock), MISO (Master In Slave Out), MOSI (Master Out Slave In), and CS (Chip Select). These are four pins which are used generally. CS pin is optional, it is used mostly when we connect multiple devices to same SCK, MISO, and MOSI pin. So to select to from or to which sensor we have to receive or send data we use CS pin.
  • If you are a beginner then first have a look at Arduino Tutorial of SSD1306

Material Required:

Circuit Diagram:

SSD1306 Oled Raspberry pi

Raspberry PI

DHT11 Sensor

GPIO 23 Pin

S Signal Pin

+5V

+5 V

GND

GND

Raspberry PI

7 Pin Oled Display

+5 VOLT

VCC

GND

GND

GPIO 11 Pin

SCK

GPIO 9   Pin

SDA

GPIO 4   Pin

RES

GPIO 6   Pin

DC

GPIO 5   Pin

CS

Programming:

  • For programming, we’ll use Adafruit library of SSD1306. But before we have to install few repositories.
  • Open Terminal and type the following commands one by one:
 pip3 install adafruit-circuitpython-ssd1306  

Then

HTML Image as link
Qries
 sudo apt-get install python3-pil  
  • Now you have to enable SPI and I2C interface of Pi using
sudo raspi-config
  • In this under Interface options enable both
  • After this, edit your config file to set baudrate for I2C Communication. For both I2C and SPI MOSI, SCK are same only name gets change, so we have to set baudrate for I2C only.
  • To do this, open a terminal window and type the below line to open config.txt file.
sudo nano /boot/config.txt 
  • Find line dtparam=i2c_arm=on. Under this, add this line also.
dtparam=i2c_baudrate=1000000
  • Then press Ctrl+X, Y and Enter to save the changes now reboot your Pi (sudo reboot now in terminal).
  • After this you are good to go.

Code:

  • Now open Thonny IDE on your Pi and type some line of code.
  • First, we import Image, ImageDraw, and ImageFont from the PIL module. Also, we import some other modules which are required to run code.
from PIL import Image, ImageDraw, ImageFont  
 import adafruit_ssd1306  
 import board  
 import digitalio  
 from time import sleep
  • Then we create an object named ‘oled’ for oled display. Syntax of the line is mentioned below as comment of that line for pin identification.
oled=adafruit_ssd1306.SSD1306_SPI(128,64,board.SPI(),digitalio.DigitalInOut(board.D6),digitalio.DigitalInOut(board.D4),digitalio.DigitalInOut(board.D5)) 
 #oled=adafruit_ssd1306.SSD1306_SPI(width,height,spi_interface,dc,rst,cs)
  • After this, we import the font in which the text is to be displayed. Then we define some code to draw the code. Using oled.fill(0) we see all pixels of OLED to black, and via oled.show() we commit the previous statement.
oled.fill(0)  
 oled.show()  
 font=ImageFont.load_default()  
 image=Image.new('1',(128,64))  
 draw=ImageDraw.Draw(image)  
  • Then in the while loop, using the range function, we create scrolling text ‘HELLO WORLD’. Using draw.text function, we draw text on the screen which scrolls on the screen.

Code:

Here is the complete code:

from PIL import Image, ImageDraw, ImageFont  
 import adafruit_ssd1306  
 import board  
 import digitalio  
 from time import sleep  
 oled=adafruit_ssd1306.SSD1306_SPI(128,64,board.SPI(),digitalio.DigitalInOut(board.D6),digitalio.DigitalInOut(board.D4),digitalio.DigitalInOut(board.D5)) 
 #oled=adafruit_ssd1306.SSD1306_SPI(width,height,spi_interface,dc,rst,cs) 
 oled.fill(0)  
 oled.show()  
 font=ImageFont.load_default()  
 image=Image.new('1',(128,64))  
 draw=ImageDraw.Draw(image)  
 for i in range(40):  
   for j in range(56):  
     draw.text((i,j),"HELLO WORLD",font=font,fill=255)  
     oled.image(image)  
     oled.show()  
     sleep(0.01)  
     draw.text((i,j),"HELLO WORLD",font=font,fill=0)  
     oled.image(image)  
     oled.show()  
  • We have also shown tutorial of this on Raspberry Pi PICO. So, if you are interested, then visit that also from here. Also, many other articles on Raspberry Pi, visit them for more such project ts and tutorials.

Now we end up with this ssd1306 OLED Raspberry Pi tutorial here. I hope you enjoy it, and if you come across any issues then ask me below. I’ll be there to solve it.

Leave a Comment