LCD display I2C errors

Hi,

I already had a thread running for a push button to toggle the backlight, well I seem to be having persistent problems so I thought it would be easier to start another thread to explain the issues.

I've got a temp/humidity project with a 16x2 display. Was all working perfectly. Installed code so that the backlight could be toggled via a button. Decided to get another screen which has a I2C adapter soldered on, which I got working with my program but without the button to toggle the light. The light remains on.

working code I2C (with no toggle working). There is still code in there for the button when the I2C display was not being used, but just showing the code to show what was working

#include <Wire.h> 

#include <LiquidCrystal_I2C.h>

#include <DHT.h>
#include "DHT.h"


#define DHTPIN A0     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22

// Set pins for lcd display backlight
#define BUTTON_PIN A5    // The number of the push-button pin.
#define LCD_LIGHT_PIN A4 // The number of the pin where anode of the display backlight is.
#define LCD_LIGHT_ON_TIME 10000 // How long (in milliseconds) should lcd light stay on
 
unsigned int currentLcdLightOnTime = 0;
// For calculating the lcd light on time.
unsigned long lcdLightOn_StartMillis;
 
boolean isLcdLightOn;
 
// For checking push-button state.
int buttonState = 0;

#define NUM_LEDS        3
const byte pinLED_LOW = 6;        // Blue LED
const byte pinLED_MED = 9;        // Green LED
const byte pinLED_HIGH = 10;      // Red LED

const byte pinArray[NUM_LEDS] = 
{
    pinLED_LOW,
    pinLED_MED,
    pinLED_HIGH
};

//LiquidCrystal_I2C lcd(0x27, 16, 2); //define pins used for LCD display
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  Serial.begin(9600);
for ( int i=0; i<NUM_LEDS; i++ )
        pinMode( pinArray[i], OUTPUT );
 
  lcd.begin(16, 2); //16 by 2 character display


  // Set the push-button pin as an input for LCD backlight
  pinMode(BUTTON_PIN, INPUT);
  // Set the lcd display backlight anode pin as an output.
  pinMode(LCD_LIGHT_PIN, OUTPUT);
  // Set the lcd display backlight anode pin to low - lcd light off.
  digitalWrite(LCD_LIGHT_PIN, LOW);
  isLcdLightOn = false;

// maybe need } 

dht.begin();
}
 
void loop()
{
  delay(2000);
  // Set as 1000 for DHT11 and 2000 for DHT22 as slow sensors and only read at these intervals
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();




  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");   
  //lcd.print(t);
  lcd.print(t,1);
  lcd.print("\337C");
  
  lcd.setCursor(0,1);
  lcd.print("Humid: ");
  //lcd.print(h);
  lcd.print(h,1);
  lcd.print("%");
  
  //the 3-led setup process
  if (t<=20.9)
  {
        digitalWrite(pinLED_LOW, HIGH);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, LOW);
  }
  else if (t>21 && t<22.9)
  {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, HIGH);
        digitalWrite(pinLED_HIGH, LOW);
  }
  else if (t>=23)
  {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, HIGH);
    }// 


  // Check the state of the push-button.
  buttonState = digitalRead(BUTTON_PIN);
 
  if (buttonState == HIGH){
    // Button pressed.

 
    lcdLightOn_StartMillis = millis();
    currentLcdLightOnTime = 0;
    isLcdLightOn = true;
    digitalWrite(LCD_LIGHT_PIN, HIGH);
  }
  else{
    // Button not pressed.
    //Serial.println("Button not pressed - LOW");
 
    if(isLcdLightOn){
      currentLcdLightOnTime = millis() - lcdLightOn_StartMillis;
      if(currentLcdLightOnTime > LCD_LIGHT_ON_TIME){
        isLcdLightOn = false;
        digitalWrite(LCD_LIGHT_PIN, LOW);



      }
    }
  }



}

I've been looking online, reading loads and trying different codes, to try and get my head around it all. Well I copied a code which apparently worked, so that I could understand how it worked.

I started having issues, not uploading to the Uno, or problems with the code itself. After reading a bunch of threads, I uninstalled all of the lcd libraries which I'd been using and installed the fmalpartida library.

Also I came across the guesser descriptor script which worked perfectly. Using the data found in the script, I copied that over to the original code which again, worked but without the backlight toggle.

Persisting with the sample code, I wrote in the LCD details found in the guesser script, but I only get errors. Firstly it was to do with 'POSITIVE' although that's gone away for now.

Continued...……..

The sample code which I'm testing to try and get the button working for the back light is

// USING THIS CODE FOR TEST
// https://www.avrfreaks.net/comment/2170126#comment-2170126



//#include <LCD.h>
//#include <LiquidCrystal_I2C.h>
//#include <LiquidCrystal_SI2C.h>


#include <Wire.h>  // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
//LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()  
{
  Serial.begin(9600);  // Used to type in characters

  lcd.begin(16,2);   // initialize the lcd for 20 chars 4 lines, turn on backlight

  for(int i = 0; i< 3; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  

  lcd.backlight(); // finish with backlight on  

  lcd.setCursor(0,0); //Start at character 4 on line 0
  lcd.print("Hello, world!");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("HI!YourDuino.com");
  delay(8000);  

// Wait and then tell user they can start the Serial Monitor and type in characters to
// Display. (Set Serial Monitor option to "No Line Ending")
  lcd.clear();
  lcd.setCursor(0,0); //Start at character 0 on line 0
  lcd.print("Use Serial Mon");
  lcd.setCursor(0,1);
  lcd.print("Type to display");  

};

Source; https://www.avrfreaks.net/comment/2170126#comment-2170126

The error I get is

Arduino: 1.8.8 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\MAGUIRES\AppData\Local\Temp\ccscyhGa.ltrans0.ltrans.o: In function `main':

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/main.cpp:46: undefined reference to `loop'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I know in my original post the correct definitions are not used but I can not get any code to work so far. Massively confused.

I've used the sample sketches and they work, with both libraries.
Currently using this library
https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/

This is the display and I2C pack I bought
https://www.ebay.co.uk/itm/1602-2004-LCD-Board-Module-16x2-LCD-Display-Module-Controller-I2C-HD44780-AI/292702196476?ssPageName=STRK%3AMEBIDX%3AIT&var=591497196766&_trksid=p2057872.m2749.l2649

You still need a loop() function even if you are not using it:

void loop() {
// empty
}

6v6gt:
You still need a loop() function even if you are not using it:

void loop() {
// empty
}

Ah that compiled. I tried hours with that. This Arduino stuff catches me out with the simplest things :slight_smile:

Now to try and translate this into the original code.