/*
This ESP32 code is created by esp32io.com
This ESP32 code is released in the public domain
For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-light-sensor
*/
#define LIGHT_SENSOR_PIN 34 // ESP32 pin GIOP36 (ADC0)
#define LEAD_PIN 32
#define POT_PIN 25
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(LEAD_PIN, OUTPUT);
pinMode(POT_PIN, INPUT);
}
void loop() {
// reads the input on analog pin (value between 0 and 4095)
// int analogValue = analogRead(LIGHT_SENSOR_PIN);
// Serial.print("Analog Value = ");
// Serial.print(analogValue); // the raw analog reading
// // We'll have a few threshholds, qualitatively determined
// if (analogValue < 40) {
// Serial.println(" => Dark");
// } else if (analogValue < 800) {
// Serial.println(" => Dim");
// } else if (analogValue < 2000) {
// Serial.println(" => Light");
// } else if (analogValue < 3200) {
// Serial.println(" => Bright");
// } else {
// Serial.println(" => Very bright");
// }
// delay(500);
int potValue = analogRead(POT_PIN);
// Map the potentiometer value to PWM range (0 to 255)
int ledBrightness = map(potValue, 0, 1023, 0, 255);
// Set the LED brightness
analogWrite(LEAD_PIN, ledBrightness);
// Read the LDR value (0 to 1023)
int ldrValue = analogRead(34);
// Print the LDR value to the serial monitor
Serial.print("LDR Value: ");
Serial.println(ldrValue);
Serial.print("\tLED Brightness: ");
Serial.print(ledBrightness);
Serial.print("\tLDR Value: ");
Serial.println(ldrValue);
delay(1000); // Small delay for stability
// int potread =analogRead(POT_PIN);
// int iwrite;
// iwrite=map(potread,0,4095,0,255);
// analogWrite(LEAD_PIN,iwrite);
// digitalWrite(LEAD_PIN, HIGH);
// delay(500);
// digitalWrite(LEAD_PIN, LOW);
// delay(500);
}