Search code examples
arduinoarduino-unoi2cled

I2C OLED will not turn on or display


I have started using the Arduino language instead of the pyFirmata version. I am using an Arduino UNO. I have run into the same problem, and that is that the OLED won't work. I've tried 2 different OLEDs, one from UCTRONICS and one from HiLetGo. They are both I2C 128x64 OLEDs, and the UCTRONICS one is yellow and blue while the HiLetGo one is all white. I've tried 2 different codes, one that I made and one example from the ssd1306 library. There are no errors, the OLEDs just don't light up. The board is alco connected to 4 touch sensors I am using for the same project but they have nothing wrong with them (yet). I have troubleshooted for a while now, and I have been able to pinpoint where the error is (probably) located. This is my code: (even though the ssd1306 I2C 128x64 example also doesn't work.) I also do not want suggestions that require extra hardware that I don't have, like an RTC (even though it is not related to this that was the only example I could come up with) This is my code: (even though the ssd1306 I2C 128x64 example also doesn't work.)

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
#define sw 128
#define sh 64
Adafruit_SSD1306 display(sw, sh, &Wire, OLED_RESET);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x78);
  display.cp437(true);
  pinMode(A4, OUTPUT);


}

void loop() {
  digitalWrite(A4, HIGH);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("test");
  Serial.println("test");
  digitalWrite(A4, LOW);

}

Like always, I only have 1 week to fix this so help would be greatly appreciated.


Solution

  • Your I2C address setting appears wrong. Arduino's Wire library (doc) uses 7 bit address. The last bit is read/write bit and Wire automatically takes care of it. So, you want to chop off the least significant bit and set the address to 0x3C instead of 0x78.

    Adafruit_SSD1306 library actually uses 0x3C as a default address. See the declaration and notes for begin() in .h and .cpp files.

    For more info, I suggest looking at SSD1306 data sheet. Here is the I2C data format. See how slave address is formatted.

    enter image description here