//Project:Photocell and 5V relay module - Photoswitch //Function: Hold the photocell with your fingers and check the value at A2 on Serial Monitor. //You can see when the resistance is up to 400 the normally open contact of the 5V relay module //is closed and the LED connected to pin 11 on the Arduino Uno board lights up; //or else, it keeps out. //************************************************ const int photocellPin = A2; //photocell attached to A2 const int ledPin = 11; //led attached to pin 11 const int relayPin=10; //5V relay module attached to digital pin 10 int photocellValue = 0;//define the value of the photoresistor /*************************************************/ void setup() { pinMode(relayPin,OUTPUT); //set relayPin as OUTPUT pinMode(ledPin,OUTPUT); //set ledPin as OUTPUT Serial.begin(9600); //initialize the serial communication as 9600bps } void loop() { photocellValue = analogRead(photocellPin);//read the value of photoresistor Serial.print("Photocell value: "); Serial.println(photocellValue); //print it in serial monitor if(photocellValue >= 400) //if the value of photocell is greater than 400 { digitalWrite(ledPin,HIGH); //turn on the led digitalWrite(relayPin,LOW); //relay connected } else //else { digitalWrite(ledPin,LOW); //turn off the led digitalWrite(relayPin,HIGH); //and relay disconnected } delay(1000); //delay 1 second }