Detecting a light or making a switch using Photoresistor interface with Arduino. Electronic Components Required Arduino UNO/MEGA/PRO 1x LED 1x 220k Ohm Resistor 1x 10k Resistor 1x LDR(Photo resistor)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* . . ........ .... .. ...... ........ MMMM MMMMM MMMMMMMMMMMMMMMMMMM MMMMMMMMMM. MMMMMMMMM MMM.MMMMMMM MMM MMM .MM.MMM MMM ......... .... ... . . . MMM MMMMMMM . .MM. MM. MMM MM. . MMM MMMMMMMMM MMMM MMMMM.MM MM MMMMMMMMMMMM. .MMM MMM. .MMM.MMMMMMMMM ........ MMM MM . M .MMMMMMMM. MMM .MMM MMMMMMMM.MMMMMMM. MM .MMM. MMMMMMMM. MMM .MMMM MMMM MMMMMM. . ... . . ...... */ int photocellPin = 2;//define the LDR to D2; int ledPin =12; int val =0; //val to store the data; void setup(){ pinMode(ledPin,OUTPUT);//set the ledPin to output; Serial.begin(9600); } void loop(){ val = analogRead(photocellPin); Serial.println("current light is"); Serial.println(val); if (val<350){ //512 =2.5V, you can modify this to adjust the sensitivity; digitalWrite(ledPin,HIGH); } else{ digitalWrite(ledPin,LOW); } delay(1000); } |
Download the source code here | 14Core Light Detection