We’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.
This 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); Wire.begin(); oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // address could also be 0x3D oled.clearDisplay(); oled.setTextColor(WHITE); oled.setCursor(32,12); oled.print("STARTING"); oled.display(); delay(2000); } void loop() { // trigger the device and measure pulse length digitalWrite(TRIG_PIN,HIGH); delayMicroseconds(20); digitalWrite(TRIG_PIN,LOW); measured_time = pulseIn(ECHO_PIN, HIGH); distance_mm = (int)((float)measured_time / 5.83); // print distance in mm oled.clearDisplay(); oled.setTextColor(WHITE); oled.setCursor(8,0); oled.setTextSize(2); if (value < 100) oled.print(" "); // leading spaces if (value < 10) oled.print(" "); oled.print(distance_mm); oled.setTextSize(1); oled.setCursor(97,8); oled.print("mm"); // display a bar graph, full length resembles 1000 mm oled.drawLine(8,27,108,27,WHITE); oled.fillRect(8,28,8+int(distance_mm+0.5)/10,30,WHITE); oled.drawLine(8,31,108,31,WHITE); oled.display(); // update the display delay(200); }
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 —
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
LikeLike
Ik gebruik Notepad++ of Visual Studio Code. Notepad is gemakkelijks on Me te beginnen, er is een video van in deze serie.
LikeLike
how would you get it to use mqtt readings instead?
LikeLike
If you can find an Arduino mqtt library there are opportunities.
LikeLike