Heltec ESP32 OLED display using Adafruit library (using Ultrasonic Sensor)

Hi, I’ve scoured the posts on this and am still having issues. I’m trying to connect a HC-SR04 Ultrasonic Sensor to a Heltec ESP32 OLED board. I updated my libraries, removed duplicates, and verified the OLED works with other libraries. I’d like to use Adafruit’s however.

Any suggestions on next steps?

I keep getting this error message:

"no matching function for call to ‘Adafruit_SSD1306::Adafruit_SSD1306(int, int, TwoWire*, int)’

CODE BELOW:

//CODE BY Javier Muñoz Sáez,05/11/2016 questions to javimusama@gmail.com
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define CommonSenseMetricSystem
//#define ImperialNonsenseSystem

#define trigPin 13
#define echoPin 12

// — OLED DISPLAY -----------------------------------------------------------------------
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 16 // Reset pin - changed to 16 for TTGO board
TwoWire twi = TwoWire(1); // create our own TwoWire instance

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &twi, OLED_RESET); // passing our TwoWire instance as param

//-------------

void setup() {

// ----- USE BELOW TO SWITCH TO NONSTD PINS -------

Serial.begin(115200);
twi.begin(4, 15); // Needs to come before display.begin() is used
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F(“SSD1306 allocation failed”));
for (;;); // Don’t proceed, loop forever

//------------------------
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

display.clearDisplay();

}

void loop() {
long duration, distance;

digitalWrite(trigPin, LOW); //PULSE |—|
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

#ifdef CommonSenseMetricSystem
distance = (duration / 2) / 29.1;
#endif
#ifdef ImperialNonsenseSystem
distance = (duration / 2) / 73.914;
#endif

display.setCursor(22, 20); //oled display
display.setTextSize(3);
display.setTextColor(WHITE);
display.println(distance);
display.setCursor(85, 20);
display.setTextSize(3);

#ifdef CommonSenseMetricSystem
display.println(“cm”);
#endif
#ifdef ImperialNonsenseSystem
display.println(“in”);
#endif

display.display();

delay(500);
display.clearDisplay();

Serial.println(distance);//debug

}
}

distance_ultrasound_measure_and_display.ino (2.08 KB)

Read up on the use of code tags in the part of the forum info that you missed.

I have had numerous issues with using Adafruit libraries and the ESP32. I found that the U8g2lib, which is ESP32 tested, works great and does not produce Guru Meditation errors.

I know, not want you wanted to read.

Even though you say you updated your libraries, I suspect you're still using an outdated version of the Adafruit SSD1306 library. I just compiled your sketch for ESP32 using the latest release of Adafruit SSD1306 (1.3.0). You have a misplaced closing brace. After I fixed that, it compiled no problem.

If you compile your sketch with verbose output during compilation enabled in File > Preferences, you will see the library versions and locations that were used in the black console window at the bottom of the Arduino IDE window. Make sure you're using Adafruit SSD1306 1.3.0.