LED turning on from a light sensor

Hello,

I'm trying the get the LED in digital pin 13 to turn on when the light is below 1000 lux. I've included a LCD screen that displays it clearly but can't get the code to work.

#include<LiquidCrystal.h>
#include<Wire.h>

LiquidCrystal lcd (2, 3, 4, 5, 6, 7);
int BH1750address = 0x23;
byte buff[2];

const int LED_PIN = 13;

void setup(){
Wire.begin();
lcd.begin(16,2);
lcd.print(" Hello ");
lcd.setCursor(0,1);
lcd.print("");
delay(2000);
lcd.clear();
}

void loop(){
int i;
uint16_t value=0;
BH1750_Init(BH1750address);
delay(200);

if(2==BH1750_Read(BH1750address)){
value=((buff[0]<<8)|buff[1])/1.2;
lcd.setCursor(0,0);
lcd.print(" Light in LUX");
lcd.setCursor(6,1);
lcd.print(value);
lcd.print(" ");
}
delay(150);
}

int BH1750_Read(int address) {
int i=0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while(Wire.available())
{
buff[i] = Wire.read();
i++;
}
Wire.endTransmission();
return i;
}

void BH1750_Init(int address) {
Wire.beginTransmission(address);
Wire.write(0x10);
Wire.endTransmission();
}

You have no pull-up resistors on the I2C lines. You should use 4K7 on each line.

You are not even making any attempt to write to pin 13 in your code.

Please post your code using a code block.

I can't see in your code where you are even attempting to turn on the internal LED.

1 Like

So when you posted that you were given a warning that it was not posted correctly, but you ignored that and clicked on "Post anyway". Is this correct?

Please post it correctly so I can try to compile it myself and see what mistakes you have made.
Learn how to post code here and in general how to ask and respond to questions here
how to get the best out of this forum

Thanks for posting that.
However that compiles for the Uno when I try it. When I have removed that spurious < at the beginning.
What error messages are you getting when you try and compile it. Please click on the copy tab of the error message and post it as if it were code.

#include<LiquidCrystal.h>
#include<Wire.h>

const int LED_PIN          = 13;  
const int ANALOG_THRESHOLD = 1000;

LiquidCrystal lcd (2, 3, 4, 5, 6, 7); 
int BH1750address = 0x23;
int analogValue;
byte buff[2];

void setup(){
  Wire.begin();
  lcd.begin(16,2);
  lcd.print("  Hello  ");
  lcd.setCursor(0,1);
  lcd.print("Test");
  delay(2000);
  lcd.clear();
  pinMode(LED_PIN, OUTPUT);
}
 
void loop(){
  int i;
  uint16_t value=0;
  BH1750_Init(BH1750address);
  delay(200);
 
  if(2==BH1750_Read(BH1750address)){
    value=((buff[0]<<8)|buff[1])/1.2;
    lcd.setCursor(0,0);
    lcd.print("  Light in LUX");
    lcd.setCursor(6,1);
    lcd.print(value);
    lcd.print("   ");

   if(2==BH1750_Read(BH1750address)){
    value < ANALOG_THRESHOLD
    digitalWrite(LED_PIN, HIGH)
    else
    digitalWrite(LED_PIN, LOW);
   }
  }
  delay(150);
}
 
int BH1750_Read(int address) {
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) 
  {
    buff[i] = Wire.read();
    i++;
  }
  Wire.endTransmission();  
  return i;
}
 
void BH1750_Init(int address) {
  Wire.beginTransmission(address);
  Wire.write(0x10);
  Wire.endTransmission();
  }

OK but not addressing what I asked about in post #7

 'void loop()':

sketch_apr04a:41:5: error: expected ';' before 'digitalWrite'

     digitalWrite(LED_PIN, HIGH)

     ^~~~~~~~~~~~

exit status 1

expected ';' before 'digitalWrite'



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

This bit of code is wrong. the digital write should have a ; at the end of it
and
value < ANALOG_THRESHOLD
should be in an if statement

if(value < ANALOG_THRESHOLD)

then the else will reference this if and not the first one.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.