#define ldr 4 //setting a variable for the Photoresistor pin.
#define led 13 //setting a variable for the led pin.
float ldrRead; //reserving a variable to save the analog reading from the sensor later.
float voltage; //reserving a variable to save the voltage from the sensor later.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Setting the baud rate.
pinMode(ldr, INPUT); //configuring the pin as an input.
pinMode(led, OUTPUT); //configuring the pin as an output.
}
void loop() {
// put your main code here, to run repeatedly:
delay(500); //A delay between readings.
ldrRead = analogRead(ldr); //saving the read into the "ldrRead" variable.
voltage = (ldrRead / 4095) * 3.3; //calculating the voltage.
Serial.print("Analog Read: " + String(ldrRead));
Serial.println(" Voltage: "+ String(voltage) + "V"); //output both the analog reading and the voltage.
if (ldrRead > 1700){
digitalWrite(led, HIGH); //turning on the light if it is dark.
}
else{
digitalWrite(led, LOW); //turning off the light if it is light.
}
}