5.2 Lichtregelsysteem
5.2.1 Fotocel-sensor
Open de 5.2.1Photocell-sensor code met Arduino IDE.
#define PhotocecllPin 34 //Define the photoresistor pin
void setup() {
//Initialize the serial port
Serial.begin(9600);
//Set the pin to input mode
pinMode(PhotocecllPin,INPUT);
}
void loop() {
//Read the value of photoresistor
int ReadValue = analogRead(PhotocecllPin);
//Print the value. NOTE: ESP32 board is 12-bit ADC, whose detection value range is within 0~4095.
Serial.print("Photocecll value: ");
Serial.println(ReadValue);
delay(500);
}
Kies het ESP32 Dev Module bord en de COM poort, en upload de code.

Testresultaat:
Open de seriële monitor.
Hoe helderder het licht dat door de fotoweerstand wordt gedetecteerd, hoe groter de waarde zal zijn.

Een fotoweerstandmodule zet een lichtsignaal om in een elektrisch signaal (spanning, stroom en weerstand). Wanneer licht de fotoweerstand raakt, geldt: hoe sterker het licht, hoe kleiner de weerstand, dus hoe groter de VCC-spanning die door de fotoweerstand zal gaan.

5.2.2 Lichtregelsysteem
Open de 5.2.2Light-Control-System code met Arduino IDE.
#define PhotocecllPin 34 //Define the photoresistor pin
#define LED 27 //Define LED pin
void setup() {
//Initialize serial port
Serial.begin(9600);
//Set the photoresistor pin to input mode
pinMode(PhotocecllPin,INPUT);
//Set the LED pin to output mode
pinMode(LED,OUTPUT);
}
void loop() {
//Read the value of the photoresistor
int ReadValue = analogRead(PhotocecllPin);
//Print the value. NOTE: ESP32 board is 12-bit ADC, whose detection value range is within 0~4095.
Serial.print("Photocecll value: ");
Serial.println(ReadValue);
//Determine:
//The value of the photoresistor >= 800, LED turns off
//The value of the photoresistor =< 800, LED turns on
if(ReadValue >= 800) {
digitalWrite(LED,LOW);
Serial.println("LED OFF");
}
else{
digitalWrite(LED,HIGH);
Serial.println("LED ON");
}
delay(100);
}
Kies het ESP32 Dev Module bord en de COM poort, en upload de code.

Testresultaat:
Wanneer de waarde van de fotoweerstand groter is dan 800 (overdag), gaat de LED uit. Als de waarde echter kleiner is dan 800, gaat de LED automatisch aan.

