LDRsensor & LCD,LED
LDR sensor with LCD
THINK CODE
#include <Adafruit_LiquidCrystal.h>
int light = 0;
Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
lcd_1.begin(16, 2);
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
light = analogRead(A0);
Serial.println(light);
lcd_1.setCursor(0, 0);
lcd_1.print("light ray");
lcd_1.setCursor(0, 1);
lcd_1.print(light);
delay(1000); // Wait for 1000 millisecond(s)
lcd_1.clear();
if (light > 30) {
digitalWrite(5, HIGH);
digitalWrite(10, HIGH);
} else {
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
}
CPP CODE
#include <Adafruit_LiquidCrystal.h>
int light = 0;
Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
lcd_1.begin(16, 2);
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
light = analogRead(A0);
Serial.println(light);
// Update LCD display
lcd_1.setCursor(0, 0);
lcd_1.print("light ray ");
lcd_1.setCursor(0, 1);
lcd_1.print(light);
lcd_1.print(" "); // Clear extra characters
delay(1000); // Wait for 1000 milliseconds
// Control LEDs based on light level
if (light > 30) {
digitalWrite(5, HIGH);
digitalWrite(10, HIGH);
digitalWrite(6, HIGH); // Turn on pin 6
} else {
digitalWrite(5, LOW);
digitalWrite(10, LOW); // Turn off pin 10
digitalWrite(6, LOW); // Turn off pin 6
}
}
ARDUINO CODE
It looks like your code is almost correct, but there are a couple of small issues that need to be addressed:
1. Missing Semicolon: In the else block, the line digitalWrite(10,LOW) is missing a semicolon at the end.
2. PinMode for Pin 5 and Pin 10: You have set pins 5 and 10 as output, which seems fine for driving external components (like LEDs), but make sure the external wiring is set up properly.
Here's the corrected version of the code:
#include <Adafruit_LiquidCrystal.h>
int light = 0;
// Initialize the LCD object (assuming you're using an I2C-based backpack with a specific address)
Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
pinMode(A0, INPUT); // Set A0 as input to read analog light sensor
Serial.begin(9600); // Start serial communication at 9600 baud
lcd_1.begin(16, 2); // Initialize the LCD (16x2 display)
pinMode(5, OUTPUT); // Set pin 5 as output (for an LED or other output device)
pinMode(10, OUTPUT); // Set pin 10 as output (for another device)
}
void loop()
{
light = analogRead(A0); // Read the value from the light sensor connected to A0
Serial.println(light); // Print the light sensor value to the serial monitor
lcd_1.setCursor(0, 0); // Set the cursor to the first row, first column
lcd_1.print("light ray"); // Print "light ray" on the first row of the LCD
lcd_1.setCursor(0, 1); // Set the cursor to the second row, first column
lcd_1.print(light); // Print the light sensor value on the second row
delay(1000); // Wait for 1000 milliseconds (1 second)
lcd_1.clear(); // Clear the LCD display
if (light > 30) {
digitalWrite(5, HIGH); // Turn on the output connected to pin 5 if light > 30
digitalWrite(10, HIGH); // Turn on the output connected to pin 10 if light > 30
} else {
digitalWrite(5, LOW); // Turn off the output connected to pin 5 if light <= 30
digitalWrite(10, LOW); // Turn off the output connected to pin 10 if light <= 30
}
}
Key Fixes:
1. Semicolon added after digitalWrite(10,LOW); in the else block.
2. Ensure that the LCD works with the correct I2C address (if you're using an I2C backpack). You may need to change the address passed to the Adafruit_LiquidCrystal lcd_1(0); line if your device uses a different address. You could find the correct address using an I2C scanner if needed.
This code should work as intended, reading the analog value from a light sensor on pin A0, displaying it on an LCD, and controlling two output devices (like LEDs) based on the sensor reading.
Comments
Post a Comment