Introduction
The sensor is in fact a photoresistor which changes its resistance with varying light intensity. It can be used to make a photoswitch.

Components
– 1 * SunFounder Uno board
– 1 * USB data cable
– 1 * Relay module
– 1 * Photoresistor sensor module
– 2 * 3-Pin anti-reverse cable
– 1 * Dupont wire (F to F)
Experimental Principle
A photoresistor or light-dependent resistor (LDR) or photocell is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity. A photoresistor can be applied in light-sensitive detector circuits, and light- and dark-activated switching circuits. In this experiment, hook up the photoresistor to A0 of the SunFounder board, and relay to pin 8. When the value reaches or even exceeds 400, the normally open contact of the relay is closed and the LED at pin 13 on the SunFounder Uno board will light up; otherwise, it will be off. In this way you can make a photoswitch.

Experimental Procedures
Step 1: Build the circuit
The wiring between the relay and SunFounder Uno board:
Relay | SunFounder Uno |
GND | GND |
VCC | 5V |
SIG | 8 |
The wiring between the photoresistor and SunFounder Uno board:
Photoresistor | SunFounder Uno |
SIG | A0 |
VCC | 5V |
SIG | A0 |

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)
Step 3: Compile
Step 4: Upload the sketch to SunFounder Uno board
Now hold the photoresistor with your fingers and check the value at A0 on Serial Monitor. You can see when the resistance is up to 400ohm, the normally open contact of the relay is closed and the LED connected to pin 13 on the SunFounder Uno board lights up; or else, it keeps out.

Summary
In this experiment we’ve use the sensor making a photoswitch. You may try other applications. For instance, connect a bulb to the relay module based on the circuit. Then when it gets dimmer, the normally open contact of the relay is closed and the bulb will light up. The same way applies to the street light.
Code
/***************************************************** * name:Photoswitch * function: hold the photoresistor with your fingers and check the value at A0 on Serial Monitor. * You can see when the resistance is up to 400ohm, * the normally open contact of the relay is closed and the LED connected to pin 13 on the SunFounder Uno board lights up; * or else, it keeps out. ************************************************/ //Email:support@sunfounder.com //website:www.sunfounder.comconst int photocellPin = A0; //photoresistor module attach to A0 const int ledPin = 13; //pin 13 built-in led const int relayPin=8; //relay module attach to digital 8 int outputValue = 0; /*************************************************/ 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() { outputValue = analogRead(photocellPin);//read the value of photoresistor Serial.println(outputValue); //print it in serial monitor if(outputValue >= 400) //if the value of photoreisitor 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 1s } /*************************************************/ |