Introduction: Hand-held Night Light
My 5-year-old kept waking us up in the night, and we kept lecturing him abount letting mom and dad sleep, until I realized he was actually unable to determine by himself whether it was sleep time or play time.
Plus, he'd ask us to turn on the light. He always makes a fuss about turning out the light, but I'm a little worried about leaving a light on all night, with the effects on melatonin and sleep quality.
I really wasn't satisfied with any night light I could find that money can buy: some could be lit on with a touch, but not turn off by themselves; some were all cute and soft, but none could give the time to a 5-year-old.
So I've put together a quick hack of a night ligth that:
- gives color codes to the time of night:
- yellow (evening): story time
- red: sleep time
- yellow (morning): you're allowed to get up and play without waking up mom or dad
- bright white: time to get up
- off during the day.
- When you press it at night, it gives off a reassuring yellowish glow. Then it fades out and turns back to red after half an hour. (Press it in the morning to turn it off)
- glows gently with a frequency close to my kid's resting breathing speed (around 2.5~3s per breath). That's a known sleep aid trick.
- does away with the blue light known to disturb melatonin during the night. Produces a bluish-white melatonin-shedding light in the morning.
- Takes the time from the Internet, via NTP.
- An ESP32 break-out board, without headers. I bought this one on the cheap (25 RMB).
- A 3D-printed case.
- I've had it professionally printed by this shop, made of white 未来8000 resin, SLA process, for around 30 RMB. The top cover came out nicely (it is 1mm thick); and the transparency is good.
- a push button. Mine is a 12*12mm button, 7.5mm high, through-hole. I bent the pins up so it would lay flat.
- two neopixel lights (cut off from a strip bought here for 70 RMB)
- if you want a battery:
- a battery charger module. I use a TP4056, like this one, for less than 1 RMB.
- a Li-Po battery. I bought these 1000 mA batteries, which were slightly smaller than my board at 45*26*8.5mm, for 14 RMB.
- a Schottky diode to keep the USB 5V from charging and destroying your battery. I had some laying around.
- obviously, make the case large enough to accomodate the battery, the charger, and the cables. Doh!
- some double-sided tape, electrical tape, wire, soldering wire.
- soldering tools and skills.
Disclaimer: I have no affiliation with the listed vendors, and list them only for the convenience of the reader.
Step 1: Prepare the Board
Remove the headers if needed.
Glue the push button and LEDs on the back of the ESP32 with hot glue or double-sided tape.
I soldered the neopixels to GND and 3V3, and to pin 12 for data, then I chained them together with wire.
I soldererd the button between pin 25 (set as output, value=0, so it acts as a drain) and pin 26 (set as input with pull-up). This was easier to solder than fetching a ground line.
I put a bit of black electrical tape over the power LED of the board, to keep it from showing through the case. I left the Signal LED uncovered, as I use it to indicate whether initialization (and setting the clock through NTP) has succeeded, then turn it off.
Step 2: Order the Enclosure
First, measure your ESP32 board dimensions. I've prepared enclosures for 29x52 or 26x49mm boards.
Next, measure the height of your button relative to the board surface (when pressed down). My designs assume 7mm.
If your board matches, you can directly download the corresponding .STEP file from the githup repository. If not, you'll need to modify the design in Fusion360. I'm using the "personal non-commercial" free license. Thanks Autodesk!
I won't teach you how to use Fusion360 here, but here are a few steps to follow once you know your way around:
- The design is parametric. Changing one dimension in the right place should make everything else fall into place naturally.
- To modify the board dimensions:
- edit the "PCB" sketch,
- double-click on the outside dimensions to modify them.
- Validate, then click "finish sketch".
- To modify the button height:
- set your view to only display the PCB
- navigate to find the top of the button (materialized as a cylinder)
- use the "press pull" tool,
- select that surface, and enter the correct new height (negative).
- the height of the pin inside the top cover should be adjusted automatically
- To order 3D printed parts:
- Make sure only the top and bottom shells are visible; hide the PCB body
- Go to File->Export, then choose the STEP format.
- Fusion 360 will export a .STEP 3D file that contains both bodies, and should be readable by your 3D print shop.
Step 3: Program
Install Micropython on your board, following these instructions.
Download the three .py files from this Github repository, and copy them to the board using MU or Thonny:
- main.py: startup file. It will call the other two files in turn, unless the button is pressed down. This is useful if you're using MU, which will otherwise timeout and fail to recognize the board.
- you need to edit this file if you used different pins from the example for connecting the button or LEDs
- do_connect.py: a script for connecting to your Wi-Fi network, and fetching the time.
- you need to edit it, and fill in your Wi-Fi SSID and password.
- handheld_night_light.py: the script that pulses the night light according to the time of day.
- you need to edit it if you used different pins from the example for connecting the button or LEDs
- you also need to edit it to configure the colors and sleep times to your liking
- finally, you need to set your timezone in this file
Once the 3 files are uploaded to your board, reboot it, and you should observe the signal LED blinking twice: once while connecting to Wi-Fi, and a second time while fetching the time from NTP. Both might take multiple seconds.
Pressing the button will switch the light on or off, depending to the time of day.
Step 4: Powering From Battery
Now is the time to solder everything together. I've powered my board from a battery, and also added a small battery charger module. This way, plugging the ESP32 to a power source will charge the battery, and unplugging it will power the board from the battery. There's no ON/OFF switch, for simplicity.
Powering the board from the battery happens through a small diode. Without this diode, your battery will be powered directly form the USB source's 5V, and will probably overheat and be destroyed. I've been careful to choose a Schottky diode, that lowers the voltage drop, thereby increasing the effective battery lifetime.
A night light is all about light control. More is not better, especially if it's blue (as blue light tends to inhibit melatonin secretion and hinder sleep). So I've covered the unwanted signalling LEDs on both the ESP32 and battery charger module with black electrical tape.
Step 5: Final Assembly
The enclosure is quite straightforward to assemble. There are 2 small hooks for holding the top cover inside the bottom cover, and an opening for the USB port.
Before closing, be sure to check for any possible wire pinching. Out of the print shop, the top cover will lay flat along the top surface of the ESP32 board. I've made a dent in the top cover to leave room for the wires.
Step 6: Parenting Tips
And you're done!
Here are some tips when talking to your kid:
- I've engaged my kid early in the process, showing him the enclosure design on my computer, and asking him if he wanted it or not. That's been driving interest and adoption.
- I've rehearsed the color codes a few times with him before giving him the night light. He's been very collaborative.
- I would advise testing and charging before giving him the final product. For one, it's difficult to take it back. Also, he made his mind about "leaving it plugged in during the night, so it wouldn't turn off suddenly", and wouldn't budge...
- Lastly, my kid has been very receptive to the light codes. He watches the light intently during story time, waiting for it to turn red. One evening, we've started the bedtime ritual a little late, and the light turned red minutes into the story. Rather than negociating like he normally does, he became sincerely distressed, and cried immediately, as if there was no way around sleep time... (I gave him 5 minutes of grace, and pressed the button to turn the light back to yellow during that time, otherwise he wouldn't even listen).
Step 7: Next Steps
The night light as it is works, and is giving satisfaction.
There are a few things you might want to push forward if you roll your own:
- the Neopixel lights give off a brief flash every so often, probably from timing glitches. Some other types of LEDs might be less demanding. This happens despite powering them from the 3.3V (same voltage on the power and signal pins).
- It would be interesting to record the time of button presses, to see whether the kid actually used the light during the night.
- The battery life would be greatly extended by using a DC-DC voltage pump to power the board.
- I've tested the battery life, and it lasts one whole night, but not all the way to the next evening.
- I was initially thinking about devising an separate light source outside the bed, with more Neopixel LEDs, that would be remote controlled by bluetooth. It hasn't proven to be necessary yet.