Introduction: Color Changing Nightlight - Arduino Microcontroller

So, instead of buying a nightlight for my kid's room, I decided I could do a much better job of it. This nightlight cycles through 1500 colors over 7 mins and turns on when it is dark and turns off when it is light. It is also small enough into small objects to add effect. Items needed are:

Arduino Microcontroller or equivalent                          (used a Mapleleaf for this)
RBG HB LED common cathode                                   (this one is COM-09264 from sparkfun.com)
Photoresistor                                                                    (this one is from Radioshack, any will do)
10k ohm Resistor
 

Step 1: Setup Arduino

First create a voltage divider with the photoresistor and the 10k resistor. You can choose any ADC pin, I used 20. I also connected the photoresistor to VCC and 10k to ground. If you choose to swap those, it will change your threshold values and inequalities in your if statements. You can see the configuration in the picture. I didn't install current restricting resistors on the RGB LED because this microcontroller doesn't source much current, but if you may not be that lucky, read the spec sheet for the LED to see current values. The reason I'm crossing the board with the resistor and LED is because many of the grounds on this board were not working.

Step 2: Programming

Here is the program if used, you may have to changes sometimes to adapt it to your microcontroller.

/*
NightLight
*/

int sensorValue = 0;  // Variable to store the value coming from the photoresistor
int ledPin1 = 0; // Connect an LED to PWM pin 0
int ledPin2 = 1;        // '' pin 1
int ledPin3 = 2; // '' pin 2
int fadeValue2 = 0; // Variable for fading 2 colors at once
int threshold = 850; // Threshold value for the photoresistor
void lightup(void);


void setup() {
    pinMode(20, INPUT_ANALOG); // Setup pin 20 to read a voltage
    pinMode(ledPin1, PWM);   // setup the pin as PWM
    pinMode(ledPin2, PWM);
    pinMode(ledPin3, PWM);
}

void loop() {
    sensorValue = analogRead(20); // Read the analog value
    if(sensorValue < threshold) {
      lightup();   // If less than threshold call lightup
    }
    if(sensorValue > threshold) {
      pwmWrite(ledPin1, 0);  // If not less than threshold turn off LED
      pwmWrite(ledPin2, 0);
      pwmWrite(ledPin3, 0);
    }

    delay(10);
}

void lightup(void) {
    pwmWrite(ledPin1, 65535);
    for (int fadeValue = 0; fadeValue <= 65535; fadeValue += 1280) {
        // Sets the value (range from 0 to 65535):                              yellow
        pwmWrite(ledPin2, fadeValue);
        // Wait for 30 milliseconds to see the dimming effect:
        delay(1000);
    }
    for (int fadeValue = 0; fadeValue <= 65535; fadeValue += 1280) {
        // Sets the value (range from 0 to 65535):                              white
        pwmWrite(ledPin3, fadeValue);
        // Wait for 30 milliseconds to see the dimming effect:
        delay(1000);
    }
    for (int fadeValue = 65535 ; fadeValue >= 0; fadeValue -= 1280) {
        // Sets the value (range from 0 to 1280):                               violet
        pwmWrite(ledPin2, fadeValue);
        // Wait for 30 milliseconds to see the dimming effect:
        delay(1000);
    }
    for (int fadeValue = 65535 ; fadeValue >= 0; fadeValue -= 1280) {
        // Sets the value (range from 0 to 1280):                               blue
        pwmWrite(ledPin1, fadeValue);
        // Wait for 30 milliseconds to see the dimming effect:
        delay(1000);
    }
    for (int fadeValue = 65535 ; fadeValue >= 0; fadeValue -= 1280) {
        // Sets the value (range from 0 to 1280):                               green
        pwmWrite(ledPin3, fadeValue);
        pwmWrite(ledPin2, fadeValue2);
        fadeValue2 += 1280;
        // Wait for 30 milliseconds to see the dimming effect:
        delay(1000);
        sensorValue = analogRead(20);       
    }
    for (int fadeValue = 0; fadeValue <= 65535; fadeValue += 1280) {
        // Sets the value (range from 0 to 65535):                              lt blue
        pwmWrite(ledPin3, fadeValue);
        // Wait for 30 milliseconds to see the dimming effect:
        delay(1000);
        sensorValue = analogRead(20);     
    }
    fadeValue2 = 65535;
   
    for (int fadeValue = 0; fadeValue <= 65535; fadeValue += 1280) {
        // Sets the value (range from 0 to 65535):                              red
        pwmWrite(ledPin1, fadeValue);
        pwmWrite(ledPin2, fadeValue2);
        pwmWrite(ledPin3, fadeValue2);
        fadeValue2 -= 1280;
        // Wait for 30 milliseconds to see the dimming effect:
        delay(1000);     
    }
}

Adjusting the value for threshold will change the sensitivity to ambient light.

Comments

author
iwoodinspire (author)2012-09-19

I'm kind of confused, the title says night light and arduino, but you're using a maple leaf to drive a single LED and photoresistor. Is the title being used to try to get views? Is the plan going to be to place it all in an enclosure for the kid's room, because my kids wouldn't leave it alone like that. Seems like a pretty expensive night light.

It's not bad for a first instructable, though (not that I'm any kind of expert). I am interested in this sort of thing, but unless I already know what you're talking about here, I would have no idea how to duplicate it, and if I did know, I would not need this help to do the same thing. Don't know if that makes sense :) Good luck on future projects, you obviously have the knowledge to make even better ones, I know I could learn a thing or two from ya.

author

Yeah, sorry about the confusion. I guess I wrote it considering that everyone would know what I'm talking about. I'm using a maple leaf which is another brand of microcontroller that uses an ARM processor just like the Arduino. The arduino is much more common and the Arduino IDE software can also be used to program the maple leaf. You should be able to copy my code and program your Arduino, no problem. The maple leaf is overkill for this project, especially this one. I only used it because I received it free and it was just laying around. A picaxe would be more suited because of the the size, price, and speed. I really just wrote the instructable to showcase an idea I had. If you are interested in learning anything about microcontrollers, there are tons of tutorial site out there. The Arduino is completely open source, I believe.

As for the enclosure, I used a frosted glass votive candle holder flipped upside down and attached it to a wooden base I made. I will post pictures of it.

author
nicholasgalassi (author)2012-09-04

It's a maple leaf. Uses same arm as arduino, but much more powerful. This one is 72MHz and 32bit. Way overkill for this project but someone gave me this. I wouldn't recommend this one to anyone. The pins are shotty and the ide sucks. I had so many problems with it. I say just stick to arduino or picaxe.

author
amandaghassaei (author)2012-09-04

nice, what kind of microcontroller is that?

About This Instructable

4,062views

16favorites

License:

More by nicholasgalassi:Color Changing Nightlight - Arduino Microcontroller
Add instructable to: