/*
* 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 // ESP32 pin GIOP36 (ADC0)
#define BUZZER 18
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(BUZZER, OUTPUT);
}
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(" => Dark");
tone(BUZZER, 1000); // Menghasilkan bunyi dengan frekuensi 1000 Hz
delay(200); // Durasi bunyi 200 ms
noTone(BUZZER); // Matikan bunyi
delay(100);
// Jeda 100 ms sebelum bunyi berikutnya
} else {
Serial.println(" => Very bright");
}
delay(500);
}