Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Lisleapex Blog
Published © CERN-OHL

Light-sensitive Night Light and Arduino

This time it is a simple Arduno practical small project, "Photosensitive Night Light".

BeginnerWork in progress2 hours118
Light-sensitive Night Light and Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Photoresistor
×1
RGB LED Lighting Shield with XMC1202
Infineon RGB LED Lighting Shield with XMC1202
×1
Photo resistor
Photo resistor
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Premium Female/Male Extension Jumper Wires, 40 x 6" (150mm)
Premium Female/Male Extension Jumper Wires, 40 x 6" (150mm)

Story

Read more

Schematics

Light-sensitive Night Light and Arduino

Code

code

Arduino
// #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);

}

Credits

Lisleapex Blog
26 projects • 0 followers
Fast Delivery of High-Quality Electronic Components
Contact

Comments

Please log in or sign up to comment.