I2C LCD1602: find its address, then say hello, on an Arduino Uno
A 16x2 LCD with an I2C backpack on A4 and A5. The sketch scans the bus, prints the address it finds, and writes a greeting and a seconds counter on the display. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// An I2C LCD1602: find it on the bus, then write to it.
//
// LCD backpack GND -> GND VCC -> 5V SDA -> A4 SCL -> A5
//
// I2C is a two-wire bus: every device on it answers to its own address. The
// scan below calls each address from 1 to 126 and lists the ones that answer.
// This backpack answers at 0x27 (some answer at 0x3F, and then the line
// creating the display needs that number instead).
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void scan() {
int found = 0;
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) {
Serial.print("I2C device found at 0x");
if (address < 16) Serial.print("0");
Serial.println(address, HEX);
found++;
}
}
if (found == 0) Serial.println("No I2C devices found");
}
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: scanning the I2C bus on A4 and A5");
Wire.begin();
scan();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Hello, inventor!");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print("Seconds: ");
lcd.print(millis() / 1000);
delay(250);
}
Parts list
3 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
How it is wired
4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 5V; Character LCD with I2C backpack pin VCC
- Ground: Arduino Uno R3 pin GND; Character LCD with I2C backpack pin GND
- Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
- Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.