Projects

Kids' Basics: Light Up the Night

Arduino-Based Motion-Activated Night Light

Daniel Koch & Johann Wyss

Issue 28, November 2019

This article includes additional downloadable resources.
Please log in to access.

Log in

Even adults are scared of the dark (they just don’t admit it), but you don’t have to be with our motion-activated night light! Read on to find out why it is better than the ones you can buy off the shelf.

BUILD TIME: 3 HOURS
DIFFICULTY RATING: BEGINNER

Assumed knowledge:

Setting Up The Arduino IDE

Issue #17, http://diyode.io/017mhqr

This project involves setting up and programming an Arduino UNO, which is discussed in detail in this article.

Many kids need a night light. This is nothing to feel bad about, and sometimes even adults use one. They’re great for the elderly, too, as older people can get disoriented at night when they wake up (as do many of the rest of us).

Maybe you’ve moved house and don’t know the layout yet. Perhaps you live with anxiety and just want to see the familiar when you first wake up. If you’re like some of us here at DIYODE, you might just need a light to not clumsily hit your toes against the bed frame or doorway of your room.

If you are one of these many people, you may have noticed that commercial night lights come in one of two varieties. One stays on all night (with some including a light sensor to turn off in the day). Others are motion activated, but the commercial versions of this rarely stay on more than five minutes. This is a problem, because when you wake up and leave the room at night, you’re often gone longer than that. The last thing you want is for your night light to turn off just as you approach your doorway.

This is even more of a problem for people with anxiety, or younger kids who are just scared of the dark. For people in this situation, a night light needs to stay on until you fall asleep. That is hard to find in an off-the-shelf product, unless you go for the type that stay on until you remember to turn them off at the power point.

Our night light is different. It is USB-powered, so it can run from a mains power adaptor, or a battery bank. It is triggered by a Passive Infrared Detector (PIR) that you can mount away from the breadboard, enabling you to build it into a table decoration or something like that. We are using an Arduino for this project, something we have not done for a while. The complexity of the discrete component version was simply beyond Kids’ Basics.

The only required prior knowledge for this task is that you can upload sketches to an Arduino. If this is new to you, be sure to read our Setting Up the Arduino IDE article from Issue 17 or visit our website https://diyode.io/017mhqr.

LIGHTHOUSE DESIGN

We are using a self-adhesive LED light strip that is flexible. You could attach it to the underside of your bed, a shelf, a picture frame, anything you want. However, many people will want the light free-standing, and that means building it into something.

We decided to mount our light as the lantern of a lighthouse. You can design your own, or download the two-page PDF design we have made available on our website. This can be printed onto thin cardboard (210 GSM or similar) or standard A4 paper and glued to cardboard instead using a glue stick.

CRAFT MATERIALS & TOOLS REQUIRED:
Hot Melt Glue Gun and Glue Sticks
Scissors
Sharp Craft Knife
Pencil
Glue Stick
Clothes Pegs
Rubber Bands
Lighthouse Cut-out Printed on 210-300GSM Paper
Masking or Electrical Tape
Side Cutters
Wire Strippers

CRAFT MATERIALS & TOOLS REQUIRED:

ELECTRONICS Parts Required:JaycarAltronicsCore Electronics
1 x Solderless BreadboardPB8820P1002PCE05102
1 x Wire Link *PB8850P1014ACE05631
6 x Plug-to-Plug Jumper Wires *WC6024P1022PRT-12795
3 x Plug-to-Socket Jumper Wires *WC6028P1017PRT-12794
1 x Arduino UNO (or substitute)XC4410Z6240CE05629
1 x USB Cable (to cut up for power cable)WC7704P1901CFIT0265
1 x 5V LED Strip, 1m longZD0586--
1 x PIR ModuleXC4444Z6382 #CE05786
1 x 1kΩ Resistor *RR0572R7558COM-05093
1 x BC337 NPN TransistorZT2115Z1035-

ELECTRONICS Parts Required:

* Quantity shown, may be sold in packs.

# This unit lacks controls and has a different pin-out.

The Build:

We’re assuming very little (or zero) electronics knowledge, and so will try to walk through everything carefully, to ensure your success. There is no soldering required - we’ve kept it as simple as possible.

If this is your first project, you may also like to read “Breadboard Basics” in Issue 15, to familiarise yourself with the breadboard before getting started. The code for this project can be downloaded from our website.

Step 1:

Plug your Arduino UNO into a computer via its USB port, and upload the NightLight sketch.

Step 2:

Slide the socket ends of the plug-to-socket wires onto the pins of the PIR module. Note which colour you choose for VCC, OUT, and GND.

Step 3:

Hold the PIR with the lens facing you, and the board surface away from you, so that you are looking at the two orange dials. Turn the left-hand one (sensitivity) half way. Turn the right-hand one (duration) fully anti-clockwise.

Step 4:

Take the LED strip and cut it from its controller. Strip some of the insulation back, being careful not to cut through the inner cores. Strip the ends of those.

Step 5:

Bare the ends of the speaker wire (or any suitable wire you may have), twist the ends to the LED strip cores, and tape the joins. Take note of which wire is positive and which is negative.

Step 6:

Cut the device end off a cheap USB charger cable, leaving the end (USB A) that plugs into the USB power socket. Expose some of the inner cores, and strip the red and black ones. Cut any others back.

Step 7:

Take one light coloured and one dark coloured plug-to-plug wire, and cut both on half. Strip the ends.

Step 8:

Take one light and one dark half of the wires from step 6, and twist them to the red and black wires, respectively, of the USB cable. Tape the joins.

Step 9:

Take the other half of each wire from step 6, and twist them to the speaker wires from the LED strip, light for positive and dark for negative. Tape the joins.

Step 10:

Place the breadboard in front of you so that the upper rail, facing away from you, is red; while the lower rail, closest to you, is blue. Insert the wire link where shown.

Step 11:

Insert the 1kΩ ( brown-black-black-brown, or brown-black-red) resistor as shown. The right-hand end goes in the row just to the left of the wire link.

Step 12:

Insert the transistor with its flat side facing you, so that its middle leg (base) lines up with the resistor and the right-hand leg (emitter) lines up with the wire link.

Step 13:

Take a strip of four plug-to-plug jumper wires (or four loose ones) and plug one into the ‘5V’ socket of the Arduino, one into the ‘GND’ socket, one into the pin2 socket, and one into the pin3 socket.

Step 14:

Take the wire from ‘5V’ on the Arduino, and plug it into the upper red (+) rail of the breadboard. Take the wire from ‘GND’ and plug it into the upper blue (-) rail.

Step 15:

Take the wire from pin3 and plug it into the board to connect with the end of the resistor. Take the wire from pin2 and plug it into an unused part of the board.

Step 16:

Plug the light-coloured wire from the LED strip into the upper red (+) rail. Plug the dark-coloured wire into the same row as the left-hand leg (collector) of the transistor.

Step 17:

Plug the wire from the VCC of the PIR into the upper red (+) rail. Plug the wire for the GND into the upper blue (-) rail. Plug the wire for the OUT of the PIR into the same row as the pin2 wire from the Arduino.

Step 18:

Plug the light-coloured wire (+, from red wire on cable) from the USB cable into the upper red (+) rail of the breadboard. Plug the dark coloured wire (-), from the black wire on the USB cable) into the upper blue (-) rail. Plug the USB plug into a powered up USB socket to test.

How Does This Circuit Work?

The circuit for this project is quite simple. All it really consists of, is a transistor to allow the low-current digital output from the Arduino to switch the higher-current LED strip, and a resistor to limit the current to the base of the transistor, so that the base-emitter junction is not “cooked” i.e. overheats and fails. We have used an NPN transistor, a BC337 which has a rated continuous current of 800mA. Our LED strip draws around 200mA, while the BC547/8/9 we often use in Kids' Basics can only handle a maximum continuous current of 100mA.

All transistors are made to a tolerance, because manufacturing is not perfect. This range is an upper and lower value, abbreviated in datasheets and listings as Hfe. For any given individual in a batch of transistors, the current gain could be anywhere between the minimum and maximum. So, with a Hfe of 100/630 for the BC337, any unit you have could have a gain of 100, or 630, or anywhere in between.

You can measure the current gain of a transistor, but this would mean providing instructions on how to make a test circuit. While simple enough, it’s space we don’t have and it is not part of the main build. It is easier and safer to just calculate for a worst-case scenario. The 1kΩ resistor gives us a base current of 5mA from our Arduino 5V HIGH. At a current gain of the minimum 100, 5mA translates to a collector-emitter current of 500mA, more than enough for the 250mA max that the LED strip can draw.

None of this means anything, however, without the PIR and Arduino. The Passive InfraRed detector has a sensor that picks up infrared light. Infrared (IR) is a spectrum of light invisible to the human eye. Heat is part of the IR spectrum, and all living creatures (eg. Humans or animals) give off an IR signature. While the PIR does not sense heat as we understand it, the IR waves it receives are related to heat. This is the simplified version, but it means you shouldn't be able to just point a fan heater at the PIR and use it to hide all other movement.

The PIR monitors its field of view (the conical area that it can see) for changes in the IR signature it sees. The white Freznel lens focuses the incoming infrared waves and provides reflections from known angles, so that changes can be tracked. When changes are detected, the output from this device turns on, or goes HIGH, to 5V. The duration of this can be set with a dial on the sensor, as can the sensitivity. While it can achieve reasonable ‘on’ times on its own, we wanted both customisability and a longer duration.

For this reason, the shortest possible HIGH signal is fed to an Arduino. The code waits for this HIGH signal, and then switches on the LED output. This is where the transistor circuit above becomes useful. Now, the code waits for a user-set time in milliseconds, before returning the output LOW and switching off the light. It’s a fairly simple code, but we’ll break it down anyway.

int LED = 3;
int PIR = 2;

At the very top, these two lines name the pins that we are using, enabling us to use names throughout the code rather than try to remember pin numbers.

Naming pin allocations allows the programmer to move the Arduino code to another type of Arduino board, or another brand entirely, by simply changing the initial pin allocation on these two lines.

void setup() {
  // put your setup code here, to run once:
  pinMode(LED, OUTPUT);
  pinMode(PIR, INPUT);
  delay (10000);
}

The ‘void setup’ loop runs once at the beginning of a program. Normally, it is used to define starting states or settings, just as we have defined the LED pin as an output and the PIR pin as an input. However, we are also adding a delay here. This is a ten-second delay, the number ‘10000’ because there are one thousand milliseconds in every second, and we want ten lots of one thousand. The 'delay' command always works in milliseconds. We do this to allow the PIR to go through its start-up process, during which its output can be unstable. Not a real problem if you have your light running for a minute, but an issue if your light runs for an hour!!

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(PIR) == HIGH) {
    digitalWrite (LED, HIGH);
    delay(1800000);
    digitalWrite (LED, LOW);
  }
}

The ‘void loop’ section runs continuously. The first line watches the PIR pin, and asks if it is HIGH or LOW. If it is low, the loop ends and restarts, and it immediately asks again. If the PIR pin is HIGH, the code makes the LED pin HIGH (which turns on the transistor and the LED strip connected to it), and then moves to the next line. This line is the ‘delay’ command. It tells the program to wait for the number of milliseconds in the brackets. The output stays HIGH during this time. This is the heart of our Night Light, as this is what allows the light to be so customisable and to stay on longer than commercial ones.

After the ‘delay’ expires, the next line sets the LED pin LOW, or off, which switches the transistor and LED strip off. At this point, the code loops back to the start to wait for a new input. If you still want the light on when this happens, any movement should trigger the light again.

On this note, there was an interesting anecdote during development that illustrates the value of debugging. Initial testing was done with a delay of six seconds to allow all sorts of changes to be made and the effects seen quickly. Everything worked fine, but when the delay was set to above ten minutes or so, the light would not turn off! Thinking the resistor was allowing too much current to the base of the transistor and baking the base-emitter junction, we upped the value and swapped the transistor for a new one.

This did not solve the problem. We replaced the transistor circuit with a simple 5mm LED and resistor, for the same result. Frustrated, we opened serial monitor, which, for those of you unfamiliar with it (which included me before this point) is an Arduino function which prints various data to a text window on your PC. We asked a more knowledgeable colleague to help add the required code to show on the screen, what was happening at the inputs and outputs. Sure enough, the output was going low, then immediately high again.

Through trial and error, we eventually realised (the next morning) that the sensitivity of the PIR was wound up so far, that sunlight reflecting from cars outside in the afternoon sun was triggering the PIR continuously. Turning down the sensitivity and placing the PIR in a cardboard tube as a screen solved the issue. You won’t need to have a tube, as the sensitivity is ok at half the dial, and most situations that this light will be in lack the trigger which were giving us the continuous triggering. That said, it was an interesting lesson in problem solving “outside the box”!

Of course, if you want to have an on-time that is not in the table, you can still apply the maths above. As we glossed over in the coding section, the problem is that there are different bases involved. There are sixty seconds in a minute, and sixty minutes in an hour. However, the base unit, the second, gets divided by bases of ten when you cut it up. So, you can have tenths, hundredths, or thousandths of a second (or multiples of ten). We do not use sixtieths in Metric.

So, let's say you want one hour and twenty minutes. First of all, we need to work in minutes, so turn the one hour into 60 minutes, and add the 20 minutes. Now that we have 80 minutes, we can find the number of seconds by multiplying 80 by 60 to give us 4800. Now that we're at seconds, we need to remember that the 'delay' is working in milliseconds, and that there are 1000 milliseconds in every second. Multiplying 4800 by 1000 gives us 4 800 000 milliseconds.

Construction:

Making the Lighthouse

Our lighthouse has several details that are different from the real deal. Oceanic lighthouses are almost always a curved, tapered shape, the better to withstand the waves. Even a straight-edged taper in paper involves a lot of maths. Straight up-and-down cylinders are rare for lighthouse towers, as even ones which look straight-walled are usually slightly tapered. When a true cylinder is found, they are usually smaller lighthouses marking entrances or hazards close to harbours. Many coastal lighthouses are built into a building that houses machinery and stores, with separate keeper’s houses.

With this in mind, we have made our lighthouse a straight tower and mounted it to a box. You can decorate the box as ocean, or make your own cardboard decoration to turn the box into a machinery and stores building. We’ll provide some photos for inspiration, but an internet image search for lighthouses will help as well.

At any point in these instructions, items with capital letters are parts from the cut-out. You may also need glue drying time between steps. Grey sections are waste parts to be cut out, and blue is a glue tab.

Step 19:

Choose a box to mount your lighthouse. Shoe boxes work well, but we used a gift box. Mark a hole on the side for the PIR lens, using the template and a pencil. You may need help from your adult to cut this out, as a sharp craft knife is the best way to make the cut-out. On the opposite side, make a smaller hole for the USB power cable. While you’re here, make a hole in the lid in the middle of where you want to mount your lighthouse.

Step 20:

Download and print the Lighthouse cut-out on 210 GSM paper, or onto plain paper which can be glued to thin cardboard. Make sure your printer is set to print ‘actual size’ on A4 paper, with no scaling or ‘fit’ options.

Step 21:

Take sheet 1, the one with the red stripes, and use scissors to cut around the grey tabs. Also cut off and keep the red strip to the left, and dispose of the waste section.

Step 22:

For sheet 2, cut carefully around the Gallery, leaving the yellow spokes sticking out, and dispose of the grey sections. Cut the centre opening, which will not be seen.

Step 23:

Cut around the outside of the Base Retainer, then fold the middle in half (don’t press too hard), cut a nick with scissors, then open it out to cut the grey section from the centre.

Step 24:

Cut around the outside of the Gallery Support, and pinch and nick the middle to cut out the grey centre. Cut along the black lines so that there is a 1cm wide ring with tabs in the middle.

Step 25:

Cut out the Roof, being careful not to cut off the tab. Also, cut out the Railing from the edge of the page.

Step 26:

Apply glue to the tab at the side of the lighthouse Tower. Carefully wrap the paper around until the tab is inside the cylinder created. Make sure the edges are straight. Clamp with clothes pegs at the ends and rubber bands in the middle.

Step 27:

Glue the blue tab of the Railing to the back of the opposite end, to form a ring. While it dries, lay the Gallery coloured side up on the work surface, and glue the Gallery Support onto it.

Step 28:

Turn the Gallery over. Bend the thin yellow posts backwards, so the yellow faces out. Glue the Railing to the posts, using the dots on the Railing to align the posts.

Step 29:

Slide the Base Retainer over the bottom of the tower. Bend the tabs on the bottom of the Tower so that they radiate away from the cylinder.

Step 30:

Apply glue to the top and bottom of the tabs, and the bottom of the Base Retainer ring. Press the tabs onto the mounting surface, and the ring over the top. Make sure every bit is pressed down. This goes onto whatever box you are using as a display base, over the hole you made in the lid.

Step 31:

Roll the Roof into a cone, and glue it using its tab. Secure with a clothes peg.

Step 32:

Open the tabs out on the Gallery Support, and slide them over the top of the Tower. Glue them down, adding a rubber band until the glue is dry.

Step 33:

Glue the red Cover Band over the top of the tabs of the Gallery Support. This hides the tabs and adds strength.

Step 34:

Disconnect the LED strip from the breadboard. Take a cardboard tube, like that from a roll of aluminium foil or cling wrap, and use the adhesive backing to wrap it around the tube. Be aware that some tubes, like brand-name Glad Wrap®, are too thick to cut. Start wrapping from the free end of the strip, and work toward the powered end.

Step 35:

Where the strip ends, cut carefully around the circumference of the tube. Cut a notch as shown for the power cable.

Step 36:

Lower the connections for the LED strip through the hole in the Gallery floor, and through the hole in the lid at the base of the Tower.

Step 37:

Get your adult to help you glue the LED tube to the Gallery with hot melt glue. You can also attach the Roof now.

Step 38:

Place the circuit into the box. Glue the PIR with a little hot melt glue into the hole in the side. Be careful not to get any glue into the trimpots that control sensitivity and duration, as you may wish to adjust the sensitivity.

Additionally, use the glue as soon as it is able to flow well after heating up, or else it may get hot enough to melt the plastic.

Step 39:

Disconnect the power wires from the breadboard, pass them through the hole in the side of the box, and then reconnect them.

Step 40:

Connect the wires for the LED strip, referring to the circuit build instructions if need be, and place the lid on the box. Now, you can plug in the USB cable to a power source and test your night light!!

FINAL DECORATIONS

We don’t have space to provide step-by-step instructions on finishing your lighthouse, and we see it as more valuable to foster your creativity anyway. Creating is one of the highest levels of intelligence, and is at the top of educational philosophies like Bloom’s Taxonomy, something that your adults might like to look up. As such, we have finished our final model and our prototype model in two different ways.

41

Think about the materials you have access to, time available, safety, and use craft websites and blogs from the internet (PLEASE use your adult for guidance here) for ideas. You can come up with some amazing ideas and surprise yourself if you think the right way.