<>Arduino Photosensitive sensor control LED Lamp brightness

Goal one :
Through photosensitive sensor , bring LED The brightness is automatically adjusted according to the ambient brightness , Low ambient brightness LED The light is brighter

Photosensitive sensor :
The photosensitive sensor has four ports :
AO: Analog signal output
DO:TTL Switch signal output ( When the brightness is greater than the set value DO Output low level , Conversely, output high level )

Circuit diagram :
—— There is no connection here DO port ——

Related code :
int value; // Store the brightness value of the electric lamp after passing through the photosensitive sensor void setup(){ Serial.begin(9600);
pinMode(4,OUTPUT); //4 The pin is the output pin } void loop(){ value=analogRead(A0)/4;
analogWrite(4,value); }
design sketch :

Goal two :

Using serial port monitor control LED Lamp mode :

* A. Night light mode (LED Low lamp brightness )
* B. Reading mode (LED High lamp brightness )
* C. Adaptive mode (LED Lamp brightness adaptation )
code :
int mode; // define mode ( pattern 0: Reading mode pattern 1: Night light mode pattern 2: Adaptive mode ) int value; // Store the brightness value of the electric lamp after passing through the photosensitive sensor
void setup(){ Serial.begin(9600); pinMode(4,OUTPUT); } void loop(){ char
LED=Serial.read(); //LED Read input if(LED=='A') // Judgment mode mode=0; else if(LED=='B')
mode=1; else if(LED=='C') mode=2; value=analogRead(A0)/4; control(); // call contol
} void control(){ // Three kinds LED Lamp mode if(mode==0){ analogWrite(4,255); } else
if(mode==1){ analogWrite(4,90); } else if(mode==2){ analogWrite(4,value); } }

Technology