Sub topic 2
Finding your display's I2C address
This chapter assumes you can upload a sketch with the Arduino IDE, that you know the setup() and loop() structure, that you can wire a breadboard, and that you can install libraries with the Library Manager.
Scanning the I2C bus
I2C is a two-wire bus. Each device on the bus answers to a 7-bit address. LCD backpacks often use 0x27 or 0x3F, and SSD1306 OLEDs often use 0x3C or 0x3D, but module makers vary, so scan the bus instead of guessing. On an Uno, connect SDA to A4 and SCL to A5, then upload this scanner:
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) {
Serial.print("Device found at 0x");
Serial.println(address, HEX);
}
}
}
void loop() {}Open the Serial Monitor at 9600 baud. A line such as "Device found at 0x3C" gives you the address to pass to your display library.
Saving pins with an I2C LCD backpack
A parallel HD44780 LCD needs six or more signal pins. An I2C backpack is a small board soldered to the back of the LCD that reduces the connection to SDA and SCL, plus power and ground. Install the LiquidCrystal_I2C library and use the address from the scanner:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Use the address found by the scanner; 0x27 and 0x3F are common.
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("I2C LCD ready");
}
void loop() {}Getting started with an SSD1306 OLED
The SSD1306 is a 128x64 monochrome OLED that also uses I2C. Install the Adafruit SSD1306 and Adafruit GFX libraries. The begin() call takes the address and returns false when no device answers at that address.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;) {} // display not found: stop here
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Hello, OLED"));
display.display();
}
void loop() {}Drawing functions change only the RAM buffer. Nothing appears on the panel until you call display().
Exercise
Exercise
The scanner prints nothing. Name two causes you would check first.
Show answer
First, check the wiring: SDA and SCL may be swapped or loose, or the module may have no power or ground. Second, confirm the module's logic voltage matches the board, because a 3.3 V module on a 5 V bus can fail to respond.