Skip to content

Instantly share code, notes, and snippets.

@Conplug
Created June 4, 2019 02:49
Show Gist options
  • Save Conplug/77d4577ba746bb08fccdd62a4ae30e3c to your computer and use it in GitHub Desktop.
Save Conplug/77d4577ba746bb08fccdd62a4ae30e3c to your computer and use it in GitHub Desktop.
Scan LCD IIC address and init LCD module automatically.
//
// Copyright (c) 2019 Conplug (https://conplug.com.tw)
// Author: Hartman Hsieh
//
// Description :
// Scan LCD IIC address and init LCD module automatically.
//
// Connections :
// Connect LCD module to IIC bus.
//
// Required Library :
// https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
//
#include <Wire.h> // IIC bus
const int TryLcdAddress[] = {0x27, 0x3f}; // Add other addresses
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C* Lcd = 0;
void setup() {
Wire.begin(); // Join IIC bus
Serial.begin(9600);
//
// Scan Lcd IIC address in table - TryLcdAddress.
//
Serial.println("Try IIC address for LCD module.");
for(int i = 0; i < sizeof(TryLcdAddress); i++) {
Wire.beginTransmission(TryLcdAddress[i]);
uint8_t data8 = Wire.endTransmission();
if (data8 == 0) {
Serial.print("IIC address of LCD module is 0x");
Serial.println(TryLcdAddress[i], HEX);
Lcd = new LiquidCrystal_I2C(TryLcdAddress[i], 16, 2);
break;
}
}
//
// Check the scanning result.
//
if(Lcd == 0) {
Serial.println("ERROR : Address not found");
}
Lcd->begin();
Lcd->backlight();
Lcd->setCursor(0, 0);
Lcd->print(" conplug.com.tw ");
Lcd->setCursor(0, 1);
Lcd->print(" Hello World! ");
delay(5000);
}
void loop() {
Lcd->setCursor(0, 1);
Lcd->print(" ");
Lcd->setCursor(0, 1);
Lcd->print(millis());
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment