Night Light

This example is to make a simple night light using an RBG LED and a photoresistor.

What you will need

  • TI LaunchPad
  • Breadboard
  • Photoresistor
  • RGB LED
  • 8x Jumper Wires
  • 1K or 10K ohm resistor

 

Wiring and Definitions

A breadboard is laid out in rows and columns that are electrically connected. Simply put a component or wire into one of the holes and it will be electrically connected with anything on the same row.

Wiring: LaunchPad jumpers should be connected to VCC and GND.

RGB LED stands for Red, Green, and Blue. You can make many different colors with combinations of red, green, and blue.

Wiring: The first three legs are connected to Red, Green, Blue, and the larger leg to VCC. View your schematic to make sure Red, Green, and Blue are connected to PWM pins.

MSP432 LaunchPad: the schematic shows PWM pins available: p2.5,p2.7,p2.6,p2.4,and p5.6.

A photoresistor is another variable resistor that changes resistance based on light exposure.

Wiring:Leg (1) connect to VCC, leg (2) connect to 10k ohm resistor and pin 2. The resistor should be connected to GND.

 

Schematic

 

 

Code

**Explanation: The setup() function initializes the LEDs and photoresistor. The loop() functions reads photoresistor value every second, checks to see if there is no light and turns on Red, Green and Blue colors of the LED in a sequence with a delay of 400ms.

 

*Note: AnalogWrite() function is used for PWM. It requires two arguments: One telling the function which pin to write to, and one indicating what PWM value to write.

 

*Note: Make sure your baud rate is set for 9600 and you have selected the correct serial port for UART.

 

#define RED 19 // pin 19 is always PWM capable according to LaunchPad standard
#define GREEN 40 // may need to change this for your LaunchPad PWM pin
#define BLUE 39 // may need to change this for your LaunchPad PWM pin
 
//Variables light sensor—constant
int value;         // Store value from photoresistor (0-1023)
int resistor = 2; //pin 2

void setup() {

// initialize serial communication over UART at 9600 baud
Serial.begin(9600);

pinMode (BLUE, OUTPUT); //Blue LED symbolising Cold and Not Fit for Consumption
pinMode (GREEN, OUTPUT); //Green LED symbolising Lukewarm and Read for Consumption
pinMode (RED, OUTPUT); //Red LED symbolising Hot and Not Fit for Consumption

//light sensor set up
pinMode(2, INPUT); // set resistor 2 pin as input 
} 

void loop() {

//reading the light sensor
int value = analogRead(2);//reads from pin 2
delay(1000); // delay
Serial.println(value); // Print the analog value to Serial

if (value > 800){
digitalWrite(RED, HIGH);//Turn leds off.
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
Serial.println("off");

}

else{

digitalWrite(RED, LOW); //Turn led on
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, HIGH);
delay(400);

digitalWrite(RED, HIGH); //Turn led on
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, HIGH);
delay(400);

digitalWrite(RED, HIGH); //Turn led on
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, LOW);
delay(400);
Serial.println("on");

}

}