Introduction: Moonlamp Nightlight

This lovely night light uses the wonderful moonlamp that you can find here

It uses a low-cost ESP8266 board to create a fantastic nightlight that uses a 3W RGB LED from Future Eden and can display any of seven colours plus a beautiful 'shimmer' mode where the colour changes continuously.

The moon globe is rotatable - if you prefer viewing the 'dark side of the moon' then just turn the globe round.

As this will be used in a child's room, careful attention has been paid to safety considerations; see the section later on safety for more details

If you have a youngster interested in learning programming, the night light is controlled by MicroPython. So this is also a great way to get someone involved in computer programming!.


WeMos D1 Mini ESP8266 board.

There are lots of suppliers on ebay. I'd suggest purchasing 10 or so from a Chinese supplier as below. They are incredibly cheap and you will undoubtedly find lots of uses for them in IoT projects

BC337 transistor

Ferrite filters

2W resistors

Prototype board


2.5mm DC socket

40mm heatsink

Rotary Encoder

There are lots of ebay suppliers selling these. I used a 15mm D shaft encoder

Knob (to fit D shaft)

Step 1: Print the Moon Lamp

You want to print the 5 inch moonlamp from the instructables link I mentioned earlier. I printed this on an Ender 3 using white PLA at 100% infill and 0.15 inch layer height with supports. Then I shone a torch through the print and used a sharp knife to remove all the remaining support material. The result was absolutely perfect. Total print time was around 15 hours.

Step 2: Print the Moon Lamp Top and Base

Use the attached STLs to print the top and base. I printed these in black PETG to get a nice gloss finish but PLA would work fine too.

Step 3: Print the Moon Support

I printed this in translucent PLA to avoid any shadows being cast. I used PLA because the moon support plate will be glued to the moon print and I wanted to therefore be sure it would adhere well.

Step 4: Flash the ESP8266 With MicroPython

Download the latest version of Micro Python, connect the ESP8266 to a USB port on your PC and then use device manager to determine which COM port it is mapped to

Then flash the Micro Python subsystem using the flash tool they supply. The example commands below flash the latest version I found at the time of writing, assuming COM4 is the port that the device is mapped to and that Python 2.7 has been installed in c:\python27

c:\python27\scripts\ --port COM4 --baud 115200 erase_flash

c:\python27\scripts\ --port COM4 --baud 115200 write_flash --flash_size=detect 0 micropython\esp8266-20190529-v1.11.bin

You only have to flash Micro Python once.

Step 5: Install the WebRepl System

WebRepl is a browser-based system that lets you enter Micro Python commands and also transfer files to and from the ESP8266. It connects via WiFi directly to the ESP8266 so you don't need to have the ESP board plugged into your computer.

Follow the instructions here to get it all running.

Transfer the two Python files above to the ESP8266 using the WebRepl browser UI

Also transfer the files from this github project - there are two python files which together control the rotary encoder

Once you're sure that Micro Python is running OK on the ESP8266 you can carry on with the next step, where you will create the controller board.

Note - you can reprogram the ESP8266 at any time even after fitting it to the controller board. However I've had the odd unit not flash correctly so making sure it works properly is a good idea before soldering it onto the controller board

Step 6: Wire Up the Circuit Board

I used a prototype board as shown in the supplies link. The components are just wired point-to-point

The RGB led is mounted on the 40mm heatsink using Akasa thermal tape.

The WeMOS clones are supplied with header pins; I soldered these to the board and then to the prototyping board.

Note that the encoder pins are soldered to the bottom of the prototype board and that it is offset slightly to the right of the board looking from the top and with the encoder shaft facing you. This is because there are eight available board pads on the end of the board and so the three encoder pins are connected leaving two unoccupied pads to one side and three to the other.

Because the 40mm heatsink sits on top of the circuit board, ensure that the area covered by the heatsink doesn't have any components mounted too high, or they will interfere with the heatsink.

Step 7: Print the Shim and Assemble the Baseplate

The shim is just a small square of plastic that sits under the heatsink to ensure that it does not short out anything.

Fit the shim to the baseplate, then place the heatsink on top. You can just put some electrical tape on the heatsink if you prefer. It doesn't actually contact anything on the circuit board anyway except possibly the shield over the ESP8266 board and the LED is electrically isolated from the heatsink anyway

Now assemble the circuit board and baseplate.

Step 8: Attach the LED to the Heatsink and Then Wire It Up to the Circuit Board

I used Akasa thermal tape. Simply cut a 20mm x 20mm square and attach the LED. Note the instructions as to which coloured side goes to the heatsink and which side goes to the LED.

I used some standard computer ribbon cable to connect the six wires from the LED back to the circuit board.

Step 9: Make the Power Cable

The power cable is just made from a cheap USB cable. Chop off the USB connector leaving about 1-2 inches of cable so you can strip it and connect some twin core power cable (I used twin core cable having approximately 5 mm total width, so that a standard 5mm ferrite suppressor will clip onto it). Use heatshrink tubing to connect the red and black leads from the USB connector to power and ground then solder a 2.5mm power plug on the other end.

Note that the cable pictured is rather shorter than you'd want - it was for a different project but wired the same. You'd probably want around a 2m cable for convenience.

Why not just wire direct to the micro USB port?. Well, there's two problems. The voltage drop over standard USB cable is quite high because at high currents the tiny wires do drop quite a bit of voltage and this could cause problems with the ESP8266. Additionally, these boards aren't designed to supply significant current - the traces are quite thin on the board - so I would supply power separately.

Note: not shown on this cable is a clip-on ferrite filter. I recommend adding one of these in case any electrical noise is radiated via the power cable. Remember you are switching around 500mA of current through the three LEDs and this has the potential to create RFI.

Step 10: Check It Out

With power wired to the circuit board you should see the LEDs light up at around half brightness and then rotating the encoder should change the brightness.

If you keep rotating the encoder you will see the colour change. There are seven colours and the final mode is 'shimmer'. In shimmer mode the colour constantly changes. The effect is quite subtle and very pretty.

When you press the encoder switch the lamp should go out. Pressing it again brings the LEDs up white at half-brightness again.

Step 11: Glue the Moonlamp Plate to the Moon and Put It All Together

Check everything fits together properly. Then glue the moonlamp support plate to the moon, positioning the moon with one of the 'poles' downwards - normally the base of the 3D print. I used epoxy resin as shown in the picture above.

The moon should rotate freely afterwards but be held securely to the top assembly
Then just use four small self-tapping screws to screw the base to the top assembly and of course secure the encoder via its supplied nut.

Step 12: A Note on Safety

As this is an appliance intended for a child's room, safety is important. It runs from a safe 5V standard phone charger so as long as you use a reputable charger that will be quite safe. The power resistor values are chosen so that the internal heatsink temperature stays around 10-15 degrees above ambient. They are also chosen so that in the extremely unlikely event of a LED short-circuit the power dissipation in each resistor is still well within its 2W power rating.

Step 13: The Python Code

The main python control program is quite simple. It's not terribly elegant code - it could do with some refactoring into separate routines - but it does work.

The code has to deal with an unexpected issue I found - when testing, I was getting annoying random flickering. It turns out that when you change the PWM duty cycle of a channel you cannot change multiple channels at the same time. If you do then you get a flicker at times - so I set up a brief time delay and then the PWM changes are made on each channel in a 'round-robin' manner, so that flicker is avoided.