Vehicle accessories – SSD1306 status display

Having some kind of informative feedback from the autonomous vehicle can help a lot during debugging and fine-tuning. However, the serial monitor is not really an option once the vehicle is driving around. But a display turns out to be a very nice solution.

Autonomous vehicles, HC-SR04 sensors

Step 1: Detecting objects with HC-SR04
Step 2: Multiple HC-SR04 with Arduino using interrupts
Step 2b: Multiple HC-SR04 with Arduino using only 1 interrupt pin
Step 3: Choosing the right vehicle
Step 4: Automatic steer correction with HC-SR04
Step 5a: Vehicle accessories – SSD1306 status display
Step 5b: Vehicle accessories – Rotary encoder illuminated button
Step 6: Autonomous vehicles, putting it all together
Alternative 1: Steer correction with HC-SR04: Single sensor, stop and scan
Alternative 2: Steer correction with HC-SR04: Servo controlled car

SSD1306 128×32 pixels

SSD1306 128x32 OLED display

The SSD1306 128×32 pixels OLED display can be controlled over I2C bus. It has a very sharp image and great refresh rate (no flickering).

Hooking up the SSD1306

Since the display uses I2C, connecting it to an Arduino Uno (or any other I2C device) is quite simple.

  • VCC pin of the display to the 5V pin on the Arduino
  • GND to GND
  • SCL pin of the display
    to SCL or A5 pin on the Arduino (Uno)
  • SDA pin of the display
    to SDA or A4 pin on the Arduino (Uno)
SSD1306 connected to Arduino Uno
SSD1306 connected to Arduino Uno
SSD1306 connected to Arduino Uno schematic
SSD1306 connected to Arduino Uno schematic

Download the schematic for the SSD1306 OLED display to an Arduino Uno via I2C communication below

Coding the SSD1306

This specific display is a clone of the Adafruit SSD1306, but can work with the same libraries. You’ll need 2 libraries:

  1. Adafruit SSD1306 library can be found on github
  2. Adafruit GFX library can also be found on github

There is also a 128×64 pixel version available, which works with the same libraries. This gives the option to use bigger fonts and/or more lines of feedback.

As with most libraries, this one also comes with an extended example. However, since for this particular application, only text is interesting, the sample code below is a stripped down version for displaying text on this display.

/****************************************************************
      SSD1306 display (128x32 px)
****************************************************************/
// #include <SPI.h>
// #include <Wire.h>
// #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>   // Takes care of including SPI, Wire, Adafruit_GFX

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

/****************************************************************
      Configuration
****************************************************************/
#define displayRefresh 100   // Update display every x ms

bool displayPresent = true;      // SD1306 display present
/*****   END OF CONFIGUTATION    *****/

// Poll timers
unsigned long lastDisplayRefresh;

/****************************************************************
      SETUP
****************************************************************/
void setup()
{
  Serial.begin(115200);
  while (!Serial) {
    ; // Wait for Serial
  }
  Serial.println("--- Serial monitor started ---");

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  Serial.println("Connecting to the SSD1306 display ...");
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println("Cannot find the SSD1306 display");
    Serial.println("Swicthing to serial monitor instead");
    displayPresent = false;
  }
  else {
    Serial.println("SSD1306 display found");
    updateDisplay();
  }

  lastDisplayRefresh = millis();
}

/****************************************************************
      LOOP
****************************************************************/
void loop()
{
  // Refresh display ?
  if (millis() - lastDisplayRefresh >= displayRefresh)
  {
    updateDisplay();
    lastDisplayRefresh = millis();
  }
}

/****************************************************************
      Display update
****************************************************************/
void updateDisplay() {
  // Build display text
  String displayText;
  // Display distance measured by the front sensor
  displayText = "Controller is up for\n";
  displayText += String(millis());
  displayText += " ms\n";
  displayText += String(micros());
  displayText += " µs\n";

  // If display was not detected, send to serial monitor in stead
  if (displayPresent) {
    display.clearDisplay();             // Clear display
    display.setTextSize(1);             // Normal 1:1 pixel scale
    display.setTextColor(WHITE);        // Draw white text
    display.setCursor(0, 0);            // Start at top-left corner
    display.println(displayText);       // Write text to buffer
    display.display();                  // Refresh display
  }
  else {
    Serial.println(displayText);        // Show on serial monitor instead
  }
}
//END

Conclusion

Getting the SD1306 to display text is quite easy using the libraries. For a moving car, the default text size can be a bit small to read, but changing display.setTextSize(1) to display.setTextSize(2) the text size becomes much bigger. If you do this, consider upgrading to the 128×64 pixels version in stead. The Adafruit displays can also work with SPI if you need pins A4 and A5 for something else than I2C.


References
Arduino Boards: Arduino Uno R3 – Arduino Nano V3 – Arduino Micro
Arduino Software: Arduino IDE
Arduino Playground: Library NewPing
Arduino forum: Arduino Micro Interrupts 6
Arduino Reference: Port RegistersattachInterrupt()interrupt()noInterrrupt()pulseIn()pulseInLong()Servo library
Random Nerd Tutorials: Complete Guide for Ultrasonic Sensor HC-SR04 with Arduin
Circuit Digest: Arduino Interrupts Tutorial
Electro Noobs: Arduino register control, timers and interruptions
Wikipedia: Speed of sound – Echolocation