SSD1306 Oled Raspberry Pi

Raspberry Pi Raspberry Pi Tutorial

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 which uses 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 pin. There are two variants of it one with I2C communication and other with SPI communication. Difference between these two communication can be easily understand.

I2C communication uses two pins SCL and SDA which are clock and serial data pins. It sends a clock signal to the scl pin of any other sensor. Then according to the signal data gets transmitted. Of course there are few rules which determines sending and receiving 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 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:

  • Raspberry PI with screen, keyboard and mouse
  • SSD1306 Oled (7 pin version, SPI communication)
  • Jumper wires
  • BreadBoard

Circuit Diagram:

 

SSD1306 Oled Raspberry pi

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

 sudo apt-get install python3-pil  

Now you have 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 naming gets change so we have to set baudrate for I2C only)

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 sae 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, ImageFont from PIL module. Alo we import some other modules whic 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 line is commented below 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 font in which text to be diaplayed. 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 while loop, using range function we create scrolling text ‘HELLO WORLD’. Using draw.text function we draw text on screen which scrolls on 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 od this on Raspberry Pi Pico. So, if you are interested then visit that also from here.

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

Spread the love, Share this post with friends