7 years, 8 months ago.

i2C LCD 20x4 Display contrast

Using the previous help to Rich Knowles Question ""Trying to use I2C_LCD with K22F base board and 4X20 I2C adapted display"" I have successfully used Wim's answers / suggestions to get my Sunfounder 20x4 i2c (LM1602 i2c) interface working (found the display needs upto 5v on the +ve supply line to display characters). See code below.

#include "mbed.h"
#include "TextLCD.h"
#include "TMP102.h"

Serial pc(USBTX, USBRX);
I2C i2c(p9,p10);
I2C i2c_lcd(p9,p10); // sda, scl
TMP102 TMP102(p9,p10,0x90); //Define SDA, SCL pin and i2c address

TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD20x4); // I2C bus, 2004A Slaveaddress 0x4E, LCD Type

//TextLCD Textlcd(&i2c_lcd, 0x4E, TextLCD::LCD20x4);



float TempReading() {
    float value;
    value = TMP102.read();
    return value;
    }  

int main() {
   lcd.printf("I2CU! Searching for I2C devices...\n");
   lcd.setBacklight(TextLCD::LightOn);
    int count = 0;
    for (int address=0; address<256; address+=2) {
        if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
           lcd.printf(" - I2C device found at address 0x%02X\n", address);
           pc.printf(" - I2C device found at address 0x%02X\n", address);
            count++;
            wait(5);
            lcd.cls();
        }
    }
    lcd.printf("%d devices found\n", count); 

In version 41 of the TextLCD libraray.. TextLCD.h I added #define LCM1602 1 TextLCD_Config.h I added #define DEFAULT 1 and changed #define LCM1602 1 (was 0)

When I compile the program it shows warnings for the TextLCD_Config.h

Warning: Incompatible redefinition of macro "LCD_BUS_I2C_RS" (declared at line 98) in "I2C_TMP102_Temperature/TextLCD/TextLCD_Config.h", Line: 230, Col: 10 for each of the following code lines.

//I2C bus expander PCF8574 interface
#define LCD_BUS_I2C_RS (1 << 0)
#define LCD_BUS_I2C_RW (1 << 1)
#define LCD_BUS_I2C_E  (1 << 2)
#define LCD_BUS_I2C_BL (1 << 3)
#define LCD_BUS_I2C_D4 (1 << 4)
#define LCD_BUS_I2C_D5 (1 << 5)
#define LCD_BUS_I2C_D6 (1 << 6)
#define LCD_BUS_I2C_D7 (1 << 7)

#define LCD_BUS_I2C_E2 (1 << 1)

Is this correct or have I put the code in the wrong places even though the display is now working???

1 Answer

7 years, 8 months ago.

The configuration table in TextLCD_Config.h allows only one active #define to select the appropriate module version. Otherwise you will get the redefinition warnings. In your case for the LCM1602 this it what the table should look like:

//Pin Defines for I2C PCF8574/PCF8574A or MCP23008 and SPI 74595 bus expander interfaces
//Different commercially available LCD portexpanders use different wiring conventions.
//LCD and serial portexpanders should be wired accordingly.
//
//Select Serial Port Expander Hardware module (one option only)
#define DEFAULT   0
#define ADAFRUIT  0
#define DFROBOT   0
#define LCM1602   1
#define YWROBOT   0
#define GYLCD     0
#define MJKDZ     0
#define SYDZ      0
#define WIDEHK    0
#define LCDPLUG   0

Accepted Answer

Brilliant, thanks again Wim now compiles with no errors. See my next question i2c devices I must really be missing something?? :-)

posted by Derek Calland 06 Mar 2017

This is really strange....Now I have my combined I2C devices running ADL345 and ITG3200 and displaying the results on my I2C LCD screen the backlight has gone out. If I swap LCD screens over (I have another one running on one mbed reading temperature from a TMP102 device) it returns and the other LCD screen goes out. Is it possible the data being transferred (using all four lines) is causing this....using to partially remove lines makes no difference?

posted by Derek Calland 08 Mar 2017