Fun with Arduino 39 OLED Display SSD1306

Distance_Measure_DeviceWe’re going to add a 128×32 I2C (4 pins) OLED to the distance measurement unit of the previous video to show the distance measured plus a bar graph. An Arduino Nano will be used this time, mounted on a breadboard. Together with the ultrasonic unit, the OLED and a 5V USB battery pack we have ourselves a fully portable distance measurement device.

The SSD1306 is an OLED display that can be had for less than $2,-. It comes in two sizes, 128×32 pixels, or 128×64 pixels. There also is a choice for SPI (7 pins) or I2C (4 pins) control. We’ll use the 4 pin I2C type.

OLED_128x64This video does not intend to be a complete overview of working with the SSD1306 OLED. We are just going to use the minimal necessary code to get our handheld distance measurement device working. More extensive explanations and examples can be found via Google. This User Manual explains the available display commands.

To get it working, a couple of libraries need to be included. The Wire.h library for the I2C communication is already available in the Arduino IDE. The SSD1306 and the GFX library we’ll have to install ourselves. Go to menu Sketch > Include Library > Manage Libraries. Search for SSD1306 and install the one that says ‘Adafruit SSD1306 … by Adafruit’. Do the same for GFX.

At the top of the code we include the library files and create the oled:

#include <Wire.h>             // the I2C library
#include <Adafruit_GFX.h>     // the graphics library
#include <Adafruit_SSD1306.h> // the SSD1306 library
Adafruit_SSD1306 oled(4);     // create a display with the name oled

Dependent on the size of our display, 64, 32 or 16 vertical pixels, we need to make a change in the library file Adafruit_SSD1306.h. Open that file (it resides in your …/libraries folder) look up line 73 and un-comment the line with the correct number of pixels. For a 32 lines display it should now look like this:

// #define SSD1306_128_64
#define SSD1306_128_32
// #define SSD1306_96_16

In setup() we start the oled:

oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);

0x3C is the I2C address of the display. Most times this will work out of the box. If it does not work, try address 0x3D. If that also does not work, see if you can find any address printed on the PCB, or if there is documentation with your display that states the address.

oled.setCursor(32,2);  // place the cursor in the 128x32 pixel field
oled.print("text");    // print text or variables
oled.drawLine(8,27,108,27,WHITE); // draw a rectangle from x1,y1 to x2,y2

Read on below the video …

The full code:

#define TRIG_PIN 10  // output: trigger to start measurement
#define ECHO_PIN 11  // input: measurement result = duration of HIGH level

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(4);

int distance_mm;
unsigned long measured_time;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN,  INPUT);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // address could also be 0x3D

void loop() {
// trigger the device and measure pulse length
  measured_time = pulseIn(ECHO_PIN, HIGH);
  distance_mm = (int)((float)measured_time / 5.83);

// print distance in mm
  if (value < 100) oled.print(" "); // leading spaces
  if (value <  10) oled.print(" ");

// display a bar graph, full length resembles 1000 mm

  oled.display(); // update the display

The video shows that our very own handheld distance measurement device works fine.

This little project was just for fun to introduce the OLED display as a preparation for some more ‘serious’ work … in the next video we are going to make station platform train departure display, including an analog clock that really works.


— 0 —

2 thoughts on “Fun with Arduino 39 OLED Display SSD1306

  1. Hallo Ruud,
    Ik zou dit graag ook eens proberen op mijn Raspberry Pi4, mijn vraag is welke software gebruik je om het programma te schrijven ?
    Ik ben een beginner zo hopelijk geen domme vraag.
    Met groet, Ad


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 )

Google photo

You are commenting using your Google 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