LDR for LCD

to start want to say I am new to the forums and arduino I have been trying to figure out how to use a LDR to control the backlight of my LCD but when i thought i had it i get nothing can someone look at this code and tell me what i am messing up please ?

// include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
// #define LDR A1

RTC_DS1307 rtc;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int LDR_pin = A1;
int back_lighth = A2;
int back_lightl = A0;
int LDR_out;
int pwm_back_light;

void setup() {

lcd.begin(16, 2);
if (!rtc.begin()) {
lcd.begin(16, 2);
lcd.print(“Couldn’t find RTC”);
delay(1000);
while (true);
}
rtc.adjust(DateTime(F(DATE), F(TIME)));
}

void loop() {

int LDR_ou = analogRead(LDR_pin);

if (LDR_ou <= 800) {
Serial.print(" hi ");
analogWrite(back_lightl, HIGH);
analogWrite(back_lighth, LOW);
}

else {
Serial.print(" bye" );
analogWrite(back_lightl, LOW);
analogWrite(back_lighth, HIGH);
}

DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print(now.day(), DEC);
lcd.print(":");
lcd.print(now.month(), DEC);
lcd.print(":");
lcd.print(now.year(), DEC);
lcd.print("");
delay(1000);

}

 analogWrite(back_lightl, HIGH);
    analogWrite(back_lighth, LOW);

You should take some time to poke around the reference section.

Pay particular attention to the entries for digitalWrite and analogWrite. Read them both. Only one takes HIGH and LOW as arguments. The other one works a completely different way.

Next please read the "How to use this forum" post at the top of any board and pay particular attention to the part about how to post code on the forum.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Be aware of the recommended maximum current for an Arduino pin. This depends on the type, but for a Uno for example, it is 20mA.
A 1602 type LCD backlight can exceed this. For example, Sparkfun use this https://www.sparkfun.com/datasheets/LCD/GDM1602K.pdf which quotes a maximum of 160mA. In this case, you need a suitable transistor to switch the backlight.

6v6gt:
Be aware of the recommended maximum current for an Arduino pin. This depends on the type, but for a UNO for example, it is 20mA.
A 1602 type LCD backlight can exceed this.

That said, the vast majority of the 1602 LCD modules available have only a single LED and draw 25 mA so can be switched directly by an Arduino (328) pin.

Look for resistor R8 on the module. If it is “101” or 100 Ohm, then the current draw will be 25 mA. If you cannot find R8 or R9 of that value, it is a completely different module and 2004 modules may also be different.