### 5.2 Lichtregelsysteem #### 5.2.1 Fotocel-sensor Open de **5.2.1Photocell-sensor** code met Arduino IDE. ```c #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. ![5458448](../media/5458448.png) **Testresultaat:** Open de seriƫle monitor. Hoe helderder het licht dat door de fotoweerstand wordt gedetecteerd, hoe groter de waarde zal zijn. ![image-20250417112320453](../media/image-20250417112320453.png) 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. ![image](../media/cou2.png) #### 5.2.2 Lichtregelsysteem Open de **5.2.2Light-Control-System** code met Arduino IDE. ```c #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. ![5458448](../media/5458448.png) **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. ![flo2](../media/flo2.png) ![image-20250417113142484](../media/image-20250417113142484.png)