3 easy steps to getting started with 0.96 OLED displays with Raspberry pi
Hey hello everyone hopes you guys are doing well. Today in this video we will learn how we can use the 0.96” OLED display with our Raspberry pi easily. We will also learn how we can show our custom images on the OLED display. We are using an easy to understandable python library for this purpose. And as an added bonus we will also make a live tracker project using this display in this lesson.
3 reason to use this display over others
- This display only use 4 wire to work with our Raspberry pi. Which makes the connection very easy.
- As this display uses the i2c protocol so we can hook multiple displays if needed without running out of GPIOs.
- This OLED display have lot more pixels then 16×2 lcds which makes the pictures more smooth.
I splits this tutorial in 3 easy steps so that it become very easy to understand. So without further a due let’s start learning. I assumed that you guys setup your Raspberry Pi with the raspbian OS properly if not then go through my previous post about How to setup your Raspberry pi .
Step: 1 Enabling the i2c protocol
As the display works on the i2c protocol so we need to enable it first in order to use it with our display. Follow there steps mention below. Open the terminal and run these commands.
~$sudo apt-get updateFor updating the system libraries
~$sudo raspi-configRaspberry pi system settings
Now after enabling the i2c interface simple click on the finish button to save the settings. Now to validate the i2c connection type this command on the terminal.
Step: 2 Making the connections
For making the connections you just need 4 male to female jumper wires and a small bread board. Follow the diagram below to hock up everything properly. Note: while connecting the components do switch off your Raspberry Pi which will save your pi from any accidental shock circuit.
Note: To know more about Raspberry pi GPIOs visit my previous post about Raspberry pis GPIO.
Step: 3 Installation and programming
After completing the connections properly it’s time to install the required libraries in our Raspberry pi. Here we are using Adafruit_Python_SSD1306 library for python 3.4 and above. And to install the library we need to run these 2 line of code in our terminal.
sudo python -m pip install --upgrade pip setuptools wheel
sudo pip install Adafruit-SSD1306
After successfully install those libraries now we are ready to write our first Python program to print a simple hello message on the OLED display. simply fire up any of your favorite IDE and copy the block of code given below and save it with .py extension.
import time import Adafruit_GPIO.SPI as SPI import Adafruit_SSD1306 from PIL import Image from PIL import ImageDraw from PIL import ImageFont RST = None disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) disp.begin() # Clear display. disp.clear() disp.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 = Image.new('1', (width, height)) # Get drawing object to draw on the 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 = 1 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() while True: draw.text((x+10, top),"Hello World", font=font, fill=255) # # Display image. disp.image(image) disp.display() time.sleep(1.5)
Now as soon as you run the program by the command python3 oled_example.py you can see the hello world message on the display.
How to show custom images on the 0.96” OLED display
To show any custom images or logos on the OLED display we first resize that using any online tools in the 128X64 pixels .ppm file. You can download the sample file from my GitHub repository logo.ppm.
And to display the image simply download the code given below and run the programme using python3 image.py
import time import Adafruit_GPIO.SPI as SPI import Adafruit_SSD1306 from PIL import Image # Raspberry Pi pin configuration: RST = 24 # Note the following are only used with SPI: DC = 23 SPI_PORT = 0 SPI_DEVICE = 0 # 128x32 display with hardware I2C: #disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) # 128x64 display with hardware I2C: disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) # 128x32 display with hardware SPI: # disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000)) # 128x64 display with hardware SPI: # disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000)) # Initialize library. disp.begin() # Clear display. disp.clear() disp.display() # Load image based on OLED display height. Note that the image is converted to 1-bit color. image = Image.open('dnylogo.ppm').convert('1') # Alternatively load a different format image, resize it, and convert to 1 bit color. #image = Image.open('happycat.png').resize((disp.width, disp.height), Image.ANTIALIAS).convert('1') # Display image. disp.image(image) disp.display()
So I hope everything works properly on your side but if you have any problem or question, simply put that in the comments section below. Also hope you learn something new today, if so don’t forget to share this in your community and leave a message below. Stay Home Stay Safe!