Nightlight

Start 

This project uses the light sensor to create a nightlight that only turns on when it's dark.

Modules 

Gather the following parts to complete this project. If your LED module doesn't have an LED on it, then one must be inserted. The Creator Set comes with 3 Light Emitting Diodes, or LEDs. Each LED has a positive side and a negative side. The positive side must be put in the positive hole in the LED Module. The positive LED "leg" is the longer one. The positive hole on the LED Module has a small ‘+’ next to it. Putting the LED in backwards won’t permanently damage it, but it won’t work until correctly inserted.

LED ModuleLED Module

Parts

All Parts
All Parts
LED
Light
Button
Cable
PartQuantity
LED 1
Light 1
Button 1
Cable 3

Starting With the Light Part of Nightlight 

Take a cable and unwrap it. Plug one side into the light sensor and the other into any Analog socket.

All the parts needed for this project
All the parts needed for this project
Take a cable...
... and unwrap it
Plug one side into the light sensor...
... and the other into Analog socket A0

Upload

Upload the code below. This tutorial uses Analog socket A0. If you are using a different socket, update the code after copying it.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
//Change here if you're using a different socket
#define sensorSocket A0

int val;

void setup() {
  Serial.begin(9600);
}

void loop() {
  val = analogRead(sensorSocket);
  Serial.println(val);
}

Observe

Open up the Serial Plotter and move your hand over the light sensor. Check out what values you can get. Look at the light value you get when the sensor is uncovered and covered. We'll need those two later.

LED Modules 

Take a cable and unwrap it. Plug one side into the led socket and the other into D6.

Take a cable...
Take a cable...
... and unwrap it
Plug one side into the LED socket
The other into Digital socket D6

Upload

Upload the code below. This tutorial uses Digital socket 6. If you are using a different socket, update the code after copying it.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define ledSocket 6

int i;
int fadeTime = 50;

void setup()
{

}

void loop()
{
  for (i = 0; i <= 255; i++) {
    analogWrite(ledSocket, i);
    delay(fadeTime);
  }
  for (i = 255; i >= 0; i--) {
    analogWrite(ledSocket, i);
    delay(fadeTime);
  }
}

Observe

The LED should be fading in and out.

Modify

Change the fadeTime delay value to change the speed of the fading.

Button 

Take a cable and unwrap it. Plug one side into the button socket and the other to Digital socket D5.

Take a cable...
Take a cable...
... and unwrap it
Plug one side into the button socket
The other into Digital socket D5

Upload

Upload the code shown below. This tutorial uses Digital socket 5. If you are using a different socket, update the code after copying it.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//If you use different socket numbers update them below
#define buttonSocket 5
#define ledSocket 6

void setup()
{
  pinMode(buttonSocket, INPUT);
  pinMode(ledSocket, OUTPUT);
}

void loop()
{
  if (digitalRead(buttonSocket)) {
    digitalWrite(ledSocket,HIGH);
  } else {
    digitalWrite(ledSocket,LOW);
  }
}

Observe

Pressing the button makes the LED turn on. This uses two states. An On state and an Off state. The button switches between those two. Now we'll take those two states and make the light sensor switch between them.

Nightlight 

At this point, the button can be removed.

Remove the buttonRemove the button

Upload

Upload the code shown below. Add in the covered light value from earlier in the lowTrigger variable. Add in the uncovered light value from earlier in the highTrigger variable.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//If you use different socket numbers update them below
#define ledSocket 6
#define lightSensorSocket A0

int lowTrigger = 250;  //<- Change to YOUR measured value
int highTrigger = 300; //<- Change to YOUR measured value
int lightAmount;
int state;

void setup()
{
  pinMode(ledSocket, OUTPUT);
}

void loop()
{
  lightAmount = analogRead(lightSensorSocket);
  if (lightAmount < lowTrigger) {
    state = 1;
  } if (lightAmount > highTrigger) {
    state = 0;
  }

  if (state) {
    digitalWrite(ledSocket, HIGH);
  } else {
    digitalWrite(ledSocket, LOW);
  }
}

Observe

The led should now be light sensitive. When exposed to high levels of light it will turn off. Once it gets dark, it will turn on.

The finished nightlight project
The finished nightlight project
Uncovered and Off
Covered to simulate the light level in the dark

Modify

Change the trigger number to more accurately have the nightlight turn on at the right darkness.