Light-sensitive Night Light and Arduino
by Lisleapex Blog in Circuits > Arduino
135 Views, 0 Favorites, 0 Comments
Light-sensitive Night Light and Arduino
This time it is a simple Arduno practical small project, "Photosensitive Night Light". This "night light" is simulated using a photoresistor sensor and an RGB LED light. It automatically turns on the light when it reaches a certain level of darkness, and turns off the light when it is very bright, which is both convenient and energy-saving.
Supplies
Arduino uno x1
Photoresistor x1
RGB LED light x1
Resistor x3
Jumper
Wiring Method
Check the wiring method in the pictures
Program Source Code
// #include <Wire.h>
int redPin= 7;
int greenPin = 6;
int bluePin = 5;
int Intensity =0;//Define Intensity variable
#define AD5 A5//Define analog port A5
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
}
void loop() {
//The darker the light, the larger the value; vice versa, the smaller it is.
Intensity = analogRead(AD5); //Read the value of analog port AD5 and store it in the Intensity variable
Serial.print("Intensity = "); //Serial port output "Intensity = "
Serial.println(Intensity); //The serial port outputs the value of the Intensity variable and breaks the line
delay(500); //Delay 500ms
if(Intensity>600){
setColor(0, 0, 255); // Blue Color
delay(5);
//When it is greater than 600, it reaches a certain degree of darkness and the light is turned on
}
else {
setColor(0, 0, 0); // Black Color
delay(1000);
//Otherwise, turn off the light
}
}
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
Test Data
Explain the changing pattern of Intensity. The darker the light, the larger the value; the brighter the light, the smaller the value.
So here I set the light to be on when the light is greater than 600; otherwise, the light is off.