Enlighten your garden with MicroPython

pascal boudalier
6 min readApr 15, 2021

and do it from your coach

Deep blue in the mountains

It all started with an engineer guilty thought. Let’s fix something which is working fine.

A while ago, I had developed a string lights which automatically blinks at night. The system was rather primitive, based on the Arduino nano board, and built with a few lines of C code.

It was powered from a wall plug, and hung from my garage.

I thought: for Christmas, let’s make it better, solar powered, connected to my home network, and controlled from a smartphone. And I rewrote it in MicroPython (a version of Python for microcontroller).

Actually, to be honest, I was just looking for an excuse to port this old C code to Python.

I went for an ESP8266 microcontroller as the brain for this project. It is very inexpensive (a few $) and has WiFi built-in. A version of MicroPython for ESP8266 is available.

I use two type of lights:

  • a 3 meter RGB (red, blue, green) led strip (150 led total). The strip is powered with 12 volts, and has 3 control pins(r, g, b). When one of this pins is connected to the ground, all leds of the corresponding color simultaneously lit.
  • a set of electroluminescent wires (EL wire) such as this one. Those wires glow nicely in the dark, and come in different colors. An EL inverter must be used to drive the EL wires. Mine is powered with 2 AA batteries (so 3V).

The entire system is powered from a 12V battery, recharged by a 100W solar panel I have in my garden. As the ESP8266 microcontroller requires 3.3V, I use an MP1584 buck converter to convert 12V to 3.3V. Likewise another MP1584 provides the 3V required by the EL inverter. I picked up this type of buck converter because of its very low quiescent current, which is important for battery operation.

An electric junction box is used to waterproof the electronics. The transparent box is a camera (another project)

Led strips can draw quite a bit of power (a few amperes), so 3 transistors are used to switch each color on and off. I use a N channel MOSFET such as IRL510 or the IRL520n. More on that at the end.

The switching of the EL wire uses a 4th MOSFET, which turns on/off the power supply of the EL inverter.

The led strip consists of tens of individual RGB led. Here the blue led is lit.
EL wires like these come in various colors and lengths.
Hardware diagram
Debugging on the living room table. The MicroPython application is developed with VScode on a Win10 desktop, and downloaded to the ESP8288 with USB.

The excellent Blynk platform is used to control the ESP8266 from a smartphone.

Blynk comprises 3 components:

  • A MicroPython application, running on the ESP8266.
  • A user interface application, build without any code, just by assembling widgets directly on the smartphone (or tablet).
  • A Blynk server, which manages bidirectional, asynchronous communication between the MicroPython application and the user interface.
The smartphone application. All its widgets communicate with the MicroPython application running on the ESP8266

The user interface provides sliders to define , in real time, how fast any of the 4 lights sources (led strip’s RGB, and the set of EL wires) should blink.

Since the application is battery powered, I had to be power conscious. I use the deep sleep mode of the ESP8266, a mode where the microcontroller almost does not draw any power.

The MicroPython application is programmed to wake up the ESP8266 every so often (60 sec), and check the status of a ‘start’ widget (see above). If start is off, the ESP8266 immediately goes back to deep sleep, and the cycle starts again.

You would do that when you do not feel like having your lights blinking and don’t want to draw power from the battery.

At any time, one can turn the start button on. The state of this button is stored in the Blynk server and checked every time the ESP8266 wakes up. When the state is ‘on’ , the ESP8266 starts blinking all the lights, according to the sliders’ position.

As soon as one turns the start button off, an event is sent to the ESP8266, which immediately goes back to deep sleep (after all, there are other sources of happiness in life than blinking led)

The system has several visual clues. When it wakes up, it will lit every led, pulse the green led to indicated the Wi-Fi is connected, and blink the blue one to indicate it is going to deep sleep.

Blynk provides a Blynk server in the cloud. This may be the easiest way to start using Blynk.

But one can also run a private Blynk server on a home computer. That is what I am doing, using a Raspberry PI.

A system that keeps waking up and going back to deep sleep can be a pain during application development (during deep sleep, one cannot communicate with the ESP8266 to update the MicroPython application). So a simple physical switch is added (acting as a ‘stay on’ button). At wake-up, the MicroPython code checks this switch, and if on, will not enter deep sleep. Instead, the webrepl is enabled, allowing to update the MicroPython application via Wi-Fi from any desktop at home. A smartphone notification and an email are also sent, to remind you the system is consuming power.

Handy when the whole thing is hanging somewhere in the garden, and a bug needs to be fixed.

Note: as explained in this other article (This computer will run forever) , it is also possible to implement the stay on button using Blynk itself.

You may be wondering what is this ‘42’ on the user interface. At each wakeup, the ESP8266 sends a random number. It’s just a clue the system is working properly. The fact that the screen-shot show 42 is purely accidental.

Finally the system in action :

Note that this is a version without the EL wire

I may add some neopixels led strips in the future. Unlike the RGB led I am using, the neopixels leds are individually addressable. This would allow to create a fancier light scheme.

The code is available in this github repo.

In the meantime, Happy blinking, Happy Blynking ….

PS: do not cross this line if you do not like MOSFET.

I initially picked up a BS170 MOSFET. It can draw 500ma, which seemed OK for one RGB channel (there is a separate MOSFET for each channel).

But I was wrong. As seen below (look at the bottom curve), with 3.0 Vgs, the current flowing (the Y axis) is almost nothing. So the led were barely lit.

Vgs is the term used in MOSFET land to specify the voltage of the ESP8266, i.e. 3.3V.

To get 500ma, I would need the ESP8266 to supply 4+ volts, which it would do for me for a brief instant, before turning itself into smoke.

A IRL520 is a more adequate choice. At 3v (the 2nd curve from the bottom), the current (Y axis) is already more than 1 ampere (12V power means x axis = Vds = 12V).

I told you not to cross …

--

--

pascal boudalier

Tinkering with Raspberry PI, ESP32, RiscV, Solar, LifePo4, IoT, Zigbee, energy harvesting, Python, MicroPython, Keras, Tensorflow, tflite, TPU. Ex Intel and HP