/*
* 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 36 // day night detection ESP32 pin GIOP36 vp pin (ADC0)
#define LIGHT_SENSOR_PIN_A 39 // ESP32 pin vn fault detction
#define LIGHT_SENSOR_PIN_B 34 // ESP32 pin 34 fault detction
//#define LIGHT_SENSOR_PIN_C 35 // ESP32 pin 35 fault detction
//#define LIGHT_SENSOR_PIN_D 32 // ESP32 pin 32 fault detction
const int ledPin4 = 18; // 16 corresponds to GPIO16
const int ledPin5 = 19; // 16 corresponds to GPIO16
//const int ledPin6 = 21; // 16 corresponds to GPIO16
//const int ledPin7 = 3; // rx pin
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin4, ledChannel);
ledcAttachPin(ledPin5, ledChannel);
//ledcAttachPin(ledPin6, ledChannel);
//ledcAttachPin(ledPin7, ledChannel);
}
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 < 500) {
Serial.println(" =>Night time");
for(int dutyCycle =240 ; dutyCycle <= 255; dutyCycle++)
{
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(5);
}
//fault detection logic
// reads the input on analog pin (value between 0 and 4095)
int analogValue_A = analogRead(LIGHT_SENSOR_PIN_A);
int analogValue_B = analogRead(LIGHT_SENSOR_PIN_B);
//int analogValue_C = analogRead(LIGHT_SENSOR_PIN_C);
//int analogValue_D = analogRead(LIGHT_SENSOR_PIN_D);
/*Serial.print("\nAnalog Value A = ");
Serial.print(analogValue_A); // the raw analog reading
Serial.print("\nAnalog Value B = ");
Serial.print(analogValue_B); // the raw analog reading
Serial.print("\nAnalog Value C = ");
Serial.print(analogValue_C); // the raw analog reading
Serial.print("\nAnalog Value D = ");
Serial.print(analogValue_D); // the raw analog reading*/
//particular lamp fault detection
if (analogValue_A < 2400){
Serial.println(" \n=> fault on lamp A !!!");
} else if (analogValue_B < 2400) {
Serial.println(" \n=> fault on lamp B !!!");
}/* else if (analogValue_C < 2100) {
Serial.println(" \n=> fault on lamp C !!!");
} else if (analogValue_D < 2400) {
Serial.println(" \n=> fault on lamp D !!!");
} */
else {
Serial.println(" => All lamps are good");
}
delay(310);
}
else {
Serial.println(" =>Day time");
// decrease the LED brightness
for(float dutyCycle = 0.3; dutyCycle >= 0; dutyCycle--)
{
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(1);
}
}
delay(10);
}