OLED doesn't display anything with lightIRRecv and IRremote libraries (using U8g2 on Arduino UNO)

Hello. I have a problem, with displaying something on my 128x64 OLED display, when <lightIRRecv.h> or <IRremote.h> are added. I'm using Arduino UNO. Here is the code (I commented what everything do):

// This program will read IR remote code and measure the distance by ultrasonic signals.

#include <SPI.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <NewPing.h>
// when I remove the library below all works fine
#include <lightIRRecv.h>

NewPing sonar(7, 6);

//Do you want to OLED be inverted?
#define inverted false
//Minimum distance to buzz
#define minDistTB 1;
//Maximum distance to buzz
#define maxDistTB 20;


#define i2c_Address 0x3c 

//#define SCREEN_WIDTH 128 
//#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//#define OLED_RESET -1   //   QT-PY / XIAO
//Adafruit_SH1106 d(OLED_RESET);
U8G2_SH1106_128X64_NONAME_1_HW_I2C d(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

#define trigPin 6
#define echoPin 7
#define buzzer 5
#define ir 3
//IRrecv irrecv(ir);
//decode_results results;



void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  delay(250);
  //d.begin(i2c_Address, true);
  d.begin();
  if(inverted) {
    d.setColorIndex(1);
  }
  d.setCursor(1, 55);
  //"Welcome to Distance Meter" ( it doesn't work because IR helper libraries :( )
  d.print(F("Welcome to distance m"));
  delay(1500);
  d.setCursor(1, 55);
  d.print(F("elcome to distance me"));
  delay(100);
  d.setCursor(1, 55);
  d.print(F("lcome to distance met"));
  delay(100);
  d.setCursor(1, 55);
  d.print(F("come to distance mete"));
  delay(100);
  d.setCursor(1, 55);
  d.print(F("ome to distance meter"));
  delay(100);
  d.setCursor(1, 55);
  d.print(F("me to distance meter!"));
  delay(1500);
  d.setFont(u8g2_font_6x12_tf);
  d.clearDisplay();
  
  lirrBegin(ir, PROTOCOL_NEC);
}

void loop() {
  
  //buzzing when cm < maxDistTB boolean (20)
  check();

  // printing distance
  d.setFont(u8g2_font_6x12_tf);
  d.setCursor(1, 0);
  d.println(F("Distance:"));
  d.setFont(u8g2_font_9x18B_tf);
  d.setCursor(24, 24);
  d.println(meter());


  delay(200);

  //reading the code from the IR reciever (that works good)
  remoteEvents_t remoteEvents = lirrGetEvents();
  switch(remoteEvents.buttonState)
  {
    case (BUTTON_PRESSED):
      d.setFont(u8g2_font_6x12_tf);
      d.setCursor(1, 55);
      d.print(F("Code: "));
      d.println(remoteEvents.buttonCode);
  }
}

>//these voids are working:
int meter() {
  long duration;
  long distance;
  //sending ultrasonic signals
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  //recieving signals
  
  duration = pulseIn(echoPin, HIGH);
  distance = duration / 58;

  // returning received signals
  
  return distance;
}

>
void  check() {
  int a = minDistTB;
  int b = maxDistTB;
  int checkDist = meter();
  // if lower than 20 cm and higher than 2 cm then buzz, else stop buzzing
  if ((checkDist > a) && (checkDist < b)) {
    digitalWrite(buzzer, HIGH); 
    delay(100);
    digitalWrite(buzzer, LOW);
    delay(100);
  }
  else {
    digitalWrite(buzzer, LOW);
  }
}

Oh, and I forgot to add memory usage:
Sketch uses 14682 bytes (45%) of program storage space. Maximum is 32256 bytes.

Global variables use 705 bytes (34%) of dynamic memory, leaving 1343 bytes for local variables. Maximum is 2048 bytes.

The code you posted does not compile. You have both your display declarations of d commented out and you have a couple of errant > in the code.

1 Like

Oh, I'm sorry, I did not seen that, here is the correct code:

// This program will read IR remote code and measure the distance by ultrasonic signals.

#include <SPI.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <NewPing.h>
// when I remove the library below all works fine
#include <lightIRRecv.h>

NewPing sonar(7, 6);

//Do you want to OLED be inverted?
#define inverted false
//Minimum distance to buzz
#define minDistTB 1;
//Maximum distance to buzz
#define maxDistTB 20;


#define i2c_Address 0x3c 

//#define SCREEN_WIDTH 128 
//#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//#define OLED_RESET -1   //   QT-PY / XIAO
//Adafruit_SH1106 d(OLED_RESET);
U8G2_SH1106_128X64_NONAME_1_HW_I2C d(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

#define trigPin 6
#define echoPin 7
#define buzzer 5
#define ir 3
//IRrecv irrecv(ir);
//decode_results results;



void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  delay(250);
  //d.begin(i2c_Address, true);
  d.begin();
  if(inverted) {
    d.setColorIndex(1);
  }
  d.setCursor(1, 55);
  //"Welcome to Distance Meter" ( it doesn't work because IR helper libraries :( )
  d.print(F("Welcome to distance m"));
  delay(1500);
  d.setCursor(1, 55);
  d.print(F("elcome to distance me"));
  delay(100);
  d.setCursor(1, 55);
  d.print(F("lcome to distance met"));
  delay(100);
  d.setCursor(1, 55);
  d.print(F("come to distance mete"));
  delay(100);
  d.setCursor(1, 55);
  d.print(F("ome to distance meter"));
  delay(100);
  d.setCursor(1, 55);
  d.print(F("me to distance meter!"));
  delay(1500);
  d.setFont(u8g2_font_6x12_tf);
  d.clearDisplay();
  
  lirrBegin(ir, PROTOCOL_NEC);
}

void loop() {
  
  //buzzing when cm < maxDistTB boolean (20)
  check();

  // printing distance
  d.setFont(u8g2_font_6x12_tf);
  d.setCursor(1, 0);
  d.println(F("Distance:"));
  d.setFont(u8g2_font_9x18B_tf);
  d.setCursor(24, 24);
  d.println(meter());


  delay(200);

  //reading the code from the IR reciever (that works good)
  remoteEvents_t remoteEvents = lirrGetEvents();
  switch(remoteEvents.buttonState)
  {
    case (BUTTON_PRESSED):
      d.setFont(u8g2_font_6x12_tf);
      d.setCursor(1, 55);
      d.print(F("Code: "));
      d.println(remoteEvents.buttonCode);
  }
}

int meter() {
  long duration;
  long distance;
  //sending ultrasonic signals
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  //recieving signals
  
  duration = pulseIn(echoPin, HIGH);
  distance = duration / 58;

  // returning received signals
  
  return distance;
}

void  check() {
  int a = minDistTB;
  int b = maxDistTB;
  int checkDist = meter();
  // if lower than 20 cm and higher than 2 cm then buzz, else stop buzzing
  if ((checkDist > a) && (checkDist < b)) {
    digitalWrite(buzzer, HIGH); 
    delay(100);
    digitalWrite(buzzer, LOW);
    delay(100);
  }
  else {
    digitalWrite(buzzer, LOW);
  }
}

That code compiles just fine for me - on a UNO board?

1 Like

The failure often takes place in the display .begin() call, if it can't allocate memory for the display buffer.

You need more than 1K bytes available in a block.

1 Like

So I need to purchase Arduino Mega?

Yes on Arduino Uno board.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.