USB-Powered Nightlight W/ Battery Backup (Two Designs)

Introduction: USB-Powered Nightlight W/ Battery Backup (Two Designs)

About: Just your average idiot who likes electricity, programming, and doing things the hardest way possible.

A while back, I discovered a need for a battery-powered nightlight for my room. The idea was that I didn't want to get up out of bed every time I wanted to turn my light off to go to bed. I also needed a light that wasn't as bright as my bedroom light because going from really bright to really dark isn't very fun on the eyes. And on top of that, our power company had a period of time where our power would go out every couple of weeks for several minutes at a time...multiple times that particular week. My thought was if the power would just randomly blank out for no reason during the summer, of all times, what would it be like in the winter?

Here are a few of my needs:

  • First, low power. I'm still working on this part, but it's already fairly low as-is.
  • I feel as though I could do better while at the same time making it cheaper, which was the second goal.
  • Also, of course, battery-powered.
  • Brightness - about a mid-low level; bright enough to see what everything is. How bright this is for you, you will need to test for it. If it's too bright, it's going to be a little hard on your eyes--especially if you have to turn it back on after it's been off for a while!
  • Compact design - I wanted this to be sitting on the edge of a cluttered desk because that happens to be where my bed sits. Not on the desk--beside it.
  • Minimal parts because of the previous item on the list and it helps the third item.

I came up with an Extremely Basic design. I thought on it and I came up with scenarios that would make the design kind of annoying to use. For example, if the power went out while it was dark, I'd need a way to see the power switch to turn it on. I considered using a switch with an illuminated actuator and put only enough current through it to make it glow. It would use just a little more power, but maybe that could be offset later in the design. And, related, I wouldn't be waiting until it was completely dark before turning it on. I would at some point before it was actually needed. Also, what if I forgot to turn it off before going to bed? I needed a way to protect the battery.

After some more thinking, I came up with a Basic Design that would solve a lot of this. It was meant to be plugged into the wall to detect if there's a power outage via a transistor circuit. I decided to use this to power a relay instead and the relay would select between the batteries and another power source (specifically, the one powering the relay) for the LEDs.

When I mentioned this project to a couple of people, they admitted to being afraid of the dark and something like this would be useful. This has given me motivation to continue working on the project. I have since came up with the Advanced Version that is (mostly) breadboard friendly, but I jumped straight to putting it on a custom circuit board, so I don't have breadboard assembly instructions. If you know how to breadboard and can follow schematics, you shouldn't have an issue wiring it up.


If you want to use the Basic Version I began with, use this list:

  1. One Sacrificial™ wall wart (think old phone charger or one of the hundreds of power blocks you have in a drawer or box somewhere that you have no idea what they go to)
  2. One SPDT relay rated to the voltage of the previous item. I used an old HVAC relay that a technician replaced many years ago (I personally found nothing wrong with it). HVAC relays are a little awkward: they are DPST, but one set of contacts are normally open and the other set is normally closed. They are also rated for 24 VAC and my chosen wall plug put out 12 VDC.
  3. One 12 Volt white LED bar: I used one of these, but the website doesn't sell them anymore. You're always free to adapt to what you have or can get access to, or even design your own.
  4. One SPST switch
  5. One or more batteries. I used two 6 Volt lantern batteries in series, though I wanted to use a single 12 Volt lantern battery.
  6. Some way to connect it all together

If you want to make the Improved Version, use this list:

  1. One external 5 VDC power source. This design went straight to PCB (more on this later), so I slapped a USB type B female plug on there. You can chop the end off of a Sacrificial™ USB cable and strip the wires (you only need the 5 Volt wire and GND)
  2. One backup power source, i.e. the battery
  3. One 5 Volt rated DPDT relay. I used this. It's breadboard friendly!
  4. One DPST switch
  5. One LM7805 voltage regulator (here)
  6. One 0.22uF capacitor (optional). The datasheet implies it should be a ceramic type, but it doesn't explicitly say it should be like it does for the output capacitor (which I didn't add)
  7. Five current-limiting resistors for the LEDs or (preferably) a resistor bus. The LEDs on this list have a 3.3V voltage drop and I calculated 85 Ohms as the required resistance. I used a resistor bus with 150 Ohms resistance, found here.
  8. One 1N4004 diode
  9. Five white LEDs (3v3 @ 20mA)
  10. A way to connect it all together

If you want the Circuit Board Version, it's generally the same list above but with some differences:

  1. Obviously, the PCB. The design is currently only uploaded to OSH Park and they sell boards in batches of threes. You can get the board here.
  2. It has a spot for terminal blocks but wires can be soldered directly to the board. I used this style.
  3. One Battery clip (I used a 9V battery)
  4. The USB connector mentioned before. I chose type B and not mini or micro B because those two are a bit too fragile for my liking.

Step 1: The Extrmely Basic Version

Because of what I was working with at the time, I had a lot of challenges to face. The relay I had is drawn out in the first pic. While what I came up with wasn't ideal by any means, it worked (second pic). I converted my awkward DPST relay to SPDT by shorting two terminals together to make a common pin. This connection went to the switch, then to the LED bar, then ground. The battery bank I made--the positive side went to the NC connection on the relay. The relay's coil was connected to the external supply, which would keep the relay in its "on" position. The power supply also connects to the NO connection on the relay. All grounds are connected together.

The theory is that while it's getting external power, the relay stays in the "on" state, thus the NO connection is closed and the NC connection is open. This means the common connection is getting power from the external source. If power is lost, the relay falls back into its "off" state and the common connection starts getting power from the batteries. Whatever the source, the switch controls power to the LED bar. The common ground between everything allows a complete circuit in either situation.

I used actual wire connectors on most of the circuit so I wouldn't have to solder anything. To connect the batteries (they have spring terminals), I used Sacrificial™ test leads (alligator clips with wires attached) by chopping one in half and stripping the wires (and added wire connectors). The one used to connect both batteries together is still in one piece. Everything is mounted to a red SparkFun shipping box.

I've no pictures of the final product, but I will take some if requested.

Step 2: The Advanced Version

I was finally able to order the parts needed to make an actually-good version of the circuit (which I had already drawn out). The important changes made in this design were designing it to work off of a regulated 5V supply and also the relay not being constantly powered. I had to swap the switch for a DPST type too. The theory of operation is just a touch more complex.

Looking at the battery half of the circuit, let's say the switch is turned off. The relay is still wired so that its "off" state causes the battery to be connected to the 5 Volt regulator, then the output of the regulator is fed back into the relay (one of the NC connections), then LEDs. The switch is wired between the battery and the relay to break the circuit and keep current from flowing. If the switch is turned on, then current will be able to flow through the circuit.

Looking at the other half of the circuit, we see that the power from the USB is immediately ran into the switch, then the coil of the relay and one of the NO connections on the relay. This connection's common pin is shared with the previously mentioned NC connection, thus this is the set of contacts actually switching between power supplies. The other set of contacts are for protecting the voltage regulator. If the switch is turned on, the relay turns on and sends the external power to the LEDs.

The diode is connected parallel (but in reverse) to the relay to reduce fly back voltage when the relay switches from its "on" to "off" position on power loss. This is to protect the power source: i.e., a USB phone charger or a PC USB port.

The capacitor might can be excluded. The datasheet for the regulator doesn't specify how far "far from the power supply filter" is, so I figured I'd might as well include it anyway.

Step 3: The PCB Version

The PCB Version is the exact same as the Advanced Version, but on a circuit board. All parts are mounted to the top of the board and are labelled with the part numbers (or some other important information) so that you can find replacement or substitute parts as required or desired. The battery input (isolated from the switch input) has a (+) input and a (-) input. The (+) side is labelled with a (+).

The switch input had an A section and a B section, which are labelled with A and B. The two terminals surrounding the "A" is the A input. Likewise, the B terminals are surrounding the "B."

Also featured are two mounting holes close to the USB plug. They have no electrical connections to anything, not even each other.

The three pics were taken at different times. The first is the blank board. The second is a partially-populated board with mixed parts. The third is the completed board using all parts called for.

Pre-Post Edit:

I tried to attach the KiCAD files (as a zip) but I got an error. Will find another way to attach later.

Battery Powered Contest

Participated in the
Battery Powered Contest

Be the First to Share


    • Jewelry Challenge

      Jewelry Challenge
    • One Board Contest

      One Board Contest
    • Anything Goes Contest 2021

      Anything Goes Contest 2021



    1 year ago

    Hi there, very nice work on the PCB and implementation!
    I made a few projects along the same premise, I used a USB plug adapter as a charger, and the light turns on when the power fails. It is here in the Battery Operated contest as well.
    I also made a version that added a PIR motion, and would power on when the AC failed.
    Thank you for sharing,
    Bob D


    Reply 1 year ago

    Thank you. I like your implementation and I considered similar ideas. I very originally wanted to mount mine to a wall and have it to turn on on power failure like emergency backup lights. In terms of a nightlight, I considered using a motion sensor, then I thought about a light to frequency converter (and an arduino). I ended up combining the two (emergency lights and nightlight) when the (constantly changing) design goals overlapped.

    I also considered a rechargeable battery, but didn’t feel confident enough with my skills to build a circuit. Also, I later switched to smd components for the board and made it considerably smaller (it’s under half the size used here! But my LEDs are small too and have four pins).