IR receiver not working with OLED screen

Originally used a 20x4 led screen to display information received via an IR receiver with this code and it seemed to be working.

// Seems to work
#include <IRremote.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
 
void dump(decode_results *results)
{
  // Dumps out the decode_results structure.
  // Call this after IRrecv::decode()
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN) {
    Serial.print("Unknown encoding: ");
  }
  else if (results->decode_type == NEC) {
    Serial.print("Decoded NEC: ");
 
  }
  else if (results->decode_type == SONY) {
    Serial.print("Decoded SONY: ");
  }
  else if (results->decode_type == RC5) {
    Serial.print("Decoded RC5: ");
  }
  else if (results->decode_type == RC6) {
    Serial.print("Decoded RC6: ");
  }
  Serial.print(results->value, HEX);
  Serial.print(" (");
  Serial.print(results->bits, DEC);
  Serial.println(" bits)");
  Serial.print("Raw (");
  Serial.print(count, DEC);
  Serial.print("): ");
 
  for (int i = 1; i < count; i++) 
  { //The change is inside this loop. Changed to HEX for easy copy and paste.
    Serial.print("");
    Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
    Serial.print(", ");
  }
  Serial.println();
}
 
const int RECV_PIN=3;
IRrecv irrecv(RECV_PIN);
decode_results results;
 
//Set the pins on the I2C chip used for LCD connections
//ADDR,EN,R/W,RS,D4,D5,D6,D7
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the default I2C bus address of the backpack-see article
 
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  irrecv.blink13(true);
   // Set off LCD module
  lcd.begin (20,4); // 20 x 4 LCD module
  lcd.setBacklightPin(3,POSITIVE); // BL, BL_POL
  lcd.setBacklight(HIGH);
  lcd.print("Hello, I'm ready to");
  lcd.setCursor(0,1);
  lcd.print("receive IR codes");   
}
 
void loop()
{
  if (irrecv.decode(&results)) 
  {
    Serial.println("Hex:\t\tDecimal:");
    Serial.print(results.value, HEX);
    Serial.print("\t");
    Serial.println(results.value, DEC);
 
    lcd.clear();
    lcd.setCursor(0,0); 
    lcd.print("IR Signal Read:");
    lcd.setCursor(0,1); 
    lcd.print("DEC: ");
    lcd.print(results.value, DEC);
    lcd.setCursor(0,2); 
    lcd.print("HEX: ");
    lcd.print(results.value, HEX);
    dump(&results);
    irrecv.resume(); // Receive the next value
  }
}

Received some OLED screens and tested the screen functioning with the demo in Examples->Adafruit SSD1306->ssd1306_128x64_i2c

Trying to implement the OLED with the IR receiving code with this

// 0 - 15 yellow
// 16 - 64 blue
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <IRremote.h>
 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
const int RECV_PIN=3;
IRrecv irrecv(RECV_PIN);
decode_results results;
 
void setup()
{
  Serial.begin(9600);
 
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) 
  {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds
 
  // Clear the buffer
  display.clearDisplay();
 
  // Draw a single pixel in white
  display.drawPixel(10, 10, SSD1306_WHITE);
 
  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
 
  irrecv.enableIRIn(); // Start the receiver
  irrecv.blink13(true);
 
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 16);
  // Display static text
  display.println("Hello, I'm ready to\nreceive IR codes");
  display.display(); 
}
 
void dump(decode_results *results)
{
  // Dumps out the decode_results structure.
  // Call this after IRrecv::decode()
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN)
  {
    Serial.print("Unknown encoding: ");
  }
  else if (results->decode_type == NEC)
  {
    Serial.print("Decoded NEC: ");
  }
  else if (results->decode_type == SONY)
  {
    Serial.print("Decoded SONY: ");
  }
  else if (results->decode_type == RC5)
  {
    Serial.print("Decoded RC5: ");
  }
  else if (results->decode_type == RC6)
  {
    Serial.print("Decoded RC6: ");
  }
  Serial.print(results->value, HEX);
  Serial.print(" (");
  Serial.print(results->bits, DEC);
  Serial.println(" bits)");
  Serial.print("Raw (");
  Serial.print(count, DEC);
  Serial.print("): ");
 
  for (int i = 1; i < count; i++) 
  { //The change is inside this loop. Changed to HEX for easy copy and paste.
    Serial.print("");
    Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
    Serial.print(", ");
  }
  Serial.println();
 
  for (int i = 1; i < count; i++) 
  { //The change is inside this loop. Changed to HEX for easy copy and paste.
    Serial.print("0x");
    Serial.print(results->rawbuf[i]*USECPERTICK, HEX);
    Serial.print(", ");
  }
  Serial.println();
}
 
void loop()
{
  if (irrecv.decode(&results)) 
  {
    Serial.println("Hex:\t\tDecimal:");
    Serial.print(results.value, HEX);
    Serial.print("\t");
    Serial.println(results.value, DEC);
 
    display.clearDisplay();
 
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 16);
    // Display static text
    display.println("IR signal read:\n");
    display.print("Decimal: ");
    display.println(results.value, DEC);
    display.print("Hex:   ");
    display.println(results.value, HEX);
    display.display(); 
 
    dump(&results);
    irrecv.resume(); // Receive the next value
  }
}

Nothing happens on the screen and get "SSD1306 allocation failed" in the Serial. The problem is line 17
IRrecv irrecv(RECV_PIN)

What's the issue here?

One thing would be to post your code on this site in code tags.

// 0 - 15 yellow
// 16 - 64 blue
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <IRremote.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int RECV_PIN=3;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);

  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) 
  {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(10, 10, SSD1306_WHITE);

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);

  irrecv.enableIRIn(); // Start the receiver
  irrecv.blink13(true);

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 16);
  // Display static text
  display.println("Hello, I'm ready to\nreceive IR codes");
  display.display(); 
}

void dump(decode_results *results)
{
  // Dumps out the decode_results structure.
  // Call this after IRrecv::decode()
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN)
  {
    Serial.print("Unknown encoding: ");
  }
  else if (results->decode_type == NEC)
  {
    Serial.print("Decoded NEC: ");
  }
  else if (results->decode_type == SONY)
  {
    Serial.print("Decoded SONY: ");
  }
  else if (results->decode_type == RC5)
  {
    Serial.print("Decoded RC5: ");
  }
  else if (results->decode_type == RC6)
  {
    Serial.print("Decoded RC6: ");
  }
  Serial.print(results->value, HEX);
  Serial.print(" (");
  Serial.print(results->bits, DEC);
  Serial.println(" bits)");
  Serial.print("Raw (");
  Serial.print(count, DEC);
  Serial.print("): ");

  for (int i = 1; i < count; i++) 
  { //The change is inside this loop. Changed to HEX for easy copy and paste.
    Serial.print("");
    Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
    Serial.print(", ");
  }
  Serial.println();

  for (int i = 1; i < count; i++) 
  { //The change is inside this loop. Changed to HEX for easy copy and paste.
    Serial.print("0x");
    Serial.print(results->rawbuf[i]*USECPERTICK, HEX);
    Serial.print(", ");
  }
  Serial.println();
}

void loop()
{
  if (irrecv.decode(&results)) 
  {
    Serial.println("Hex:\t\tDecimal:");
    Serial.print(results.value, HEX);
    Serial.print("\t");
    Serial.println(results.value, DEC);

    display.clearDisplay();

    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 16);
    // Display static text
    display.println("IR signal read:\n");
    display.print("Decimal: ");
    display.println(results.value, DEC);
    display.print("Hex:   ");
    display.println(results.value, HEX);
    display.display(); 

    dump(&results);
    irrecv.resume(); // Receive the next value
  }
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Updated, thank you

Which MCU are you using?

Show a wiring diagram, post an image of the project all wired up. With both devices connected what does I2C scanner report?

You didn't say how you reached that conclusion.

I don't know if it will help but I found a project doing basically the same thing. The main difference is that they use Pin 12 instead of Pin 3 for IR input. It couldn't hurt to try that.

That means you are out of memory -- not enough room for the screen buffer.

Received some OLED screens and tested the screen functioning with the demo in Examples->Adafruit SSD1306->ssd1306_128x64_i2c

This works, without changing the setup, adding line by line for the IR and uploading after every added line, it fails to upload after adding
`IRrecv irrecv(RECV_PIN)

see post#7, the MCU is out of memory.

I've tried different pins. I fail to understand how a pin number will prevent the upload. I can see how it could prevent it from functioning properly, but to cause a freeze on uploading doesn't add up.

Received some OLED screens and tested the screen functioning with the demo in Examples->Adafruit SSD1306->ssd1306_128x64_i2c

This works, without changing the setup, adding line by line for the IR and uploading after every added line, it fails to upload after adding

IRrecv irrecv(RECV_PIN)

Received some OLED screens and tested the screen functioning with the demo in Examples->Adafruit SSD1306->ssd1306_128x64_i2c

ssd1306_128x64_i2c work properly. Without changing the setup, adding line by line for the IR, and uploading after every added line, it fails to upload after adding

IRrecv irrecv(RECV_PIN)

Please post the entire actual upload error message. Copy it from the IDE window. Text, not an image, please.

What do you mean by "fails to upload"?!? The original problem was that your sketch reached Serial.println(F("SSD1306 allocation failed")); but it must have uploaded to display that message.

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