Adding ADC channels to an ESP8266

One of the drawbacks of the ESP8266 is that it only has one ADC port.
In case you need more ADC ports, you could add an I2C ADC converter like the PCF8591 or the ADS1115, but there is a cheaper way that is easier to program as well.
You could use an analog multiplexer like the HCT4051, that can switch 8 ADC channels individually to the ADC input of your ESP8266.
Does this method have drawbacks? yes, you need 3 I/O pins to control the HCT4051 and the ESP8266 does not have an abundance of pins to begin with.
The circuit looks like this:

I have chosen GPIO12-14 to control the multiplex chip. If all control lines are LOW, the A0 line is patched through to the ADC of the ESP8266. If all control lines are HIGH, then the A7 line is patched through to the ADC channel of the ESP8266, the table below, shows the state of the I/O pins need to be in to read every individual analog channel

      S0      S1      S2
      GPIO14  GPIO13  GPIO12
A0     0       0       0
A1 0 0 1
A2 0 1 0
A3 0 1 1
A4 1 0 0
A5 1 0 1
A6 1 1 0
A7 1 1 1


ESP8266-01 Neopixel module

In Chinese webstores a “ESP-01S RGB LED Controller Module for Arduino IDE WS2812 Light Ring Smart Electronic DIY” module is sold for less than a dollar  (if you buy it without the ESP8266-01). The module contains everything that is needed to have an ESP8266-01S control a Neopixel strip

The module uses GPIO2 as output for the Neopixel(s). Suitable Software can be found here. Make sure you use the new ESP8266-01S with this module. The ESP8266-01S has the necessary pull up resistors already available. If you use the standard ESP8266-01 you need to attach the required pullups yourself.

Mini WiFi RGB/RGBW/RGBWW ledstrip controller (MagicHome/Arilux AL LC0x)

Chinese webstores offer the I presume by now well known Mini Ledstrip controllers that depending on the model work with WiFi, WifFi/RF, or Bluetooth.

Model Color Voltages Control Price Link
LC01 RGB 5-28V WiFi ~€7 Banggood
LC02 RGBW 9-12V WiFi ~€14 Banggood
LC03 RGB 5-28V WiFi/IR ~€9.50 Banggood
LC04 RGBW 9-12V WiFi/IR ~€11 Banggood
LC08 RGBWW 5-28V WiFi ~€10 Banggood
LC09 RGB 5-28V WiFi/RF ~€11 Banggood
LC10 RGBW 9-28V WiFi/RF ~€12 Banggood
LC11 RGBWW 9-28V WiFi/RF ~€10 Banggood
LC12 WS2812 12-24V WiFi ~€10 Aliexpress
LC13 RGBW 12-24V Bluetooth ~€6 Aliexpress

I added the links (and prices) only as an example, they may or may not be cheaper at Aliexpress (check here for other brand names as well) or at another supplier. One example of unexpected price difference for instance is the LC10/LC11 difference, where the controller with more functionality is in fact 2 euro cheaper.

Here, I will focus on the WFi controller.
There are basically 3 models: one with 4 pins, one with 5 pins and one with 6pins. The 4 pin model is for an RGB strip and the one with 5 pins is for an RGBW strip, the one with 6 pins, so you can control RGB, warmwhite and coolwhite. The remaining pin is usually the common anode. And then there is a model to be used with WS2812 programmable LED’s.

The models can take various voltages (see table above) and usually work with a 12 Volt LEDstrip. At this moment it isnt quite clear to me whether attaching 28 Volt will still make it useable for a 12 Volt strip (as I have no 28 Volt PSU), but for now I am not risking anything trying that.

The WiFi model is usually controlled by the MagicHome App or MagicHomePro App.

The setup for it to recognize your device is not that hard, but not always intuitive. You should be able to do it within a matter of minutes though. Roughly the process consists of the app recognizing the controller, then you switch to its WiFi accesspoint (it is the one with the weird name in your SSID list) and fill out your WiFi credentials.
With your device a little card should come that contains the link to a full instruction PDF

Once you have done that, the app allows you to switch to direct contact, so your commands will be sent to the device within your network, so you are not depending on a server somewhere in China. Upto 8 phones will be able to control the device.

Inspite of its less than perfect rating on the play store, it has some cool features. It can synchronize your lights with music or a microphone….if you are in that sort of thing

Many people though will be interested to control their devices from within their own home automation system.
there are in fact various possibilities to do that.

1-Open a MagicHome account, if you havent already done so.
You can do that via your App, as described in the instruction PDF
Then use either GoogleHome or Alexa to add the MagicHome service and you should be ready to go. As per now, I understand the GoogleHome App offers more functionality for this device than Alexa.

2-You don’t have/don’t want GoogleHome or Alexa (no worry, google knows you don’t want it and has your name on a list)
Well, if you happen to have OpenHAB, there is a binding that will let you control this device directly. The process is described thoroughly in the given link, but it comes down to the following steps:
Power up the LED Strip
open PAPER UI and install the WIFI LED Binding
in PaperUI inside THINGS should be something like “AK001-ZJ100” (WiFi LED Device) with corresponding MAC-Adress
click on it and u see something like: “wifiled:wifiled:DC30B89D35CE:power”
add this to your xxx.items File, using the following channels:

3-You just want to use python
Then this is for you: After installing the flux_led software, you will be able to use command line commands to control your WiFi LED

4-You do not have OpenHAB, or Alexa, or GoogleHome (but still want your own access)
Though I am sure that if the controller can be controlled from OpenHAB, it can be controlled from other Domotix systems such as HAS, but I am not sure if anyone already developed that. In that case, you have the option of flashing your controller, as it has an ESP8266 in it. It is not as easy as reflashing a sonoff, as you will need to do some soldering to tiny pads, but it is doable. Xose ‘tinkerman’ Perez describes how. Have a look here as well. Beware though that older replacement software, may not always be compatible as somewhere along the development apparently pin assignments were changed.
Also beware that not all replacement software out there  will implement e.g. your IR or RF functions if you happen to have a combined model.
The IR programming follows the NEC protocol with  the following codes:

UP 0xFF906F
OFF 0xFFF807
R 0xFF9867
G 0xFFD827
B 0xFF8877
W 0xFFA857
1 0xFFE817
2 0xFF48B7
3 0xFF6897
4 0xFF02FD
5 0xFF32CD
6 0xFF20DF
7 0xFF50AF
8 0xFF7887
9 0xFF708F
10 0xFF38C7
11 0xFF28D7
12 0xFFF00F

If your flashing goes wrong, supposedly the original firmware can be found here, but given the fact that there are various models, I presume the bin file somehow is configurable.

The 16 relay module and the Raspberry Pi: not an ideal marriage

  • If you need lots of Relays to switch, the 16 channel relay board(above) that is available at various Chinese webshops seems like a good idea, and in fact it is if you use 5Volt logic.
    It is a complete different matter if you like to use it with a 3v3 system such as the raspberry Pi, or  the ESP series.

To understand why, it is best to have a look at the circuit:

and as that may appear a bit daunting, let’s bring it back to one channel:
The relay is driven by an ULN2803 and the ULN2803 is driven by an optocoupler. In this configuration ofcourse that is completely useless as there is no galvanic separation between the microprocessor and this board and the optocoupler doesn’t add much other than a problem.

  • First problem: the optocoupler inverts the signal: you need a LOW to activate the relay and a HIGH to deactivate the relay.
  • Second problem: you need a HIGH that is pretty close to 5Volt to deactivate the relay:
        • Suppose that you use a raspberry that in its HIGH state puts 3Volt on a GPIO pin. That means that over the optocoupler and R1 there is a voltage of 2Volt.
        • Suppose that the optocoupler has a forward voltage of 1.2Volt, that means that a current of 0.8mA will flow. Not much, but it might just be enough to keep the relay activated.

    On the internet you will find people using this relay board with a raspberry without problems while others struggle. That is simply a matter of variation in specs: suppose you have a module in which the LM2576 step down regulator is at the low end of its specs with 4.8 Volt, the HIGH the raspberry puts out is closer to 3.3 Volt, the optocouplers forward voltage is 1.3Volt and R1 is 1100 ohm rather than 1k.
    Then with a HIGH there is 1.5Volt left over the optocoupler and R1, so a current of 0.18mA will flow which is hardly enough to ignite the optocoupler.
    So, what to do when you have this board and want to connect it to your raspberry and it will just not switch off. Well there are a few options and none is perfect:

    1. You could replace the LM2957 with a 3V3 type, but that is quite an undertaking.
    2. You could get 16 NPN transistors and 32 resistors and build the below circuit 16 times. That will  adapt the raspberry to the  required level of the board and it inverts the signal so you have an active HIGH relay again.The irony though is that each of those transistor circuits in itself is capable of driving a relay.
    3. Instead of the transistor you could try to add an LED to each input line: it still will be OK for when the input signal is O volt, and this time when it is HIGH, the voltage drop of 1.7 volt will be over 2LEDs and a resistor with virtually no current flowing.  Add the LED in the same direction as the optocoupler, so with the cathode towards the raspberry output)
    4. You could just remove the optocoupler and link the emitter and kathode contacts (suggestion from reader Jeroen)

    A python program to test the relays could look like this:

    import time
    pins = [2, 3, 4, 17, 27, 22, 10, 9, 14, 15, 18, 23, 24, 25, 8, 7]
    for pin in pins:
       GPIO.setup(pin, GPIO.OUT) 
       GPIO.output(pin, GPIO.LOW)
       GPIO.output(pin, GPIO.HIGH)
  • Other relay module posts:
    The 16 relay module and the Raspberry Pi: not an ideal marriage
    Re-inverting an inverting relay
    Simple WiFi relay board (3)
    Simple WiFi relay board: a 4 channel DIY WiFi relay board (2)
    Simple WiFi relay board: an overview (1)
    Adding a popular 5Volt 4 channel relay board to a 3V3 processor (beginners)

Re-inverting an inverting relay

I needed a relaymodule and I specifically needed one that is the standard Input LOW -> relay not active, input HIGH, relay active.
Once that used to be the norm for relaymodules, but lately many relaymodules have an optocoupler on board, that inverts the signal.
Anyway, I got myself a module kinda like in the picture above and it stated ‘Active HIGH’. Judging from the picture indeed it seemed OK, just having the standard transistor or FET, driving the relay, a flyback diode and 3 1k resistors that I presumed to be for the basis of the transistor and for the two LED’s  for “Power ON” and for “relay activated”.
I ordered it a Aliexpress.
It turned out te be a decent relay, but sadly it worked just the other way around: Active LOW. Darn!

Anyway, ofcourse I could slve it in Software, by always sending a HIGH signal to deactivate the relay in the setup and I could opt to use the NC contact rather than the NO contact, but that would mean the relay would be on if ‘not in use’.
It seemed better to just add an inverter, but before I wanted to do that I was wondering what the circuit on the board looked like. Well, that wasn’t so hard to find out. As I already expected it turned out to be a PNP transistor (the 2TY  or S8550) driving the relay. Why? I do not know.

Apart from having an inverted signal response, this circuit has another drawback: The signal voltage needs to be near the Vcc to shut off the relay. The Vbe saturation voltage is -1.2Volt. So if fed with 5 Volt, anything lower than 5-1.2=3.8V will keep the transistor in saturation.That means that you cannot feed the circuit with  5Volt  and use a 3.3 Volt microprocessor to give the input signal, because a HIGH  of  3.3 Volt is not high enough to close the PNP transistor. That means that if you want to use it on a raspberry Pi or an ESP8266, you will have to use the 3.3 Volt pin to feed the relay. The relay is a 5 Volt type, but it will work on 3.3 Volt but at 70 ohms it draws an extra 47mA from the 3v3 LDO.
Would this have been a standard NPN driven relay, the Voltage needed for the relay would have been irrelevant. The only thing that would matter is if the trigger voltage would be high enough to open the transistor.

To invert the signal transistor seemed the easiest. It would require 1 NPN transistor, a 10k collector resistor and a 1 k base resistor. But add a diode and you already have a fully functional driver for a bare relay. However, from an earlier project, I had a spare inverter gate,  a 74AHC1G04  that seemed ideal for the job, albeit a tad tiny for my eyes.

The question remains then if using that inverter would also solve the issue of the relay voltage. he datasheet teaches u that roughly the HIGH output of the gate is 0.5 Volt below Vcc. So at 5Volt, we may have just enough to close the relay. However, at 5Volt Vcc the gate needs at least 3.8 Volt on its input to see it as a HIGH.That means that with using the inverter, if we supply the circuit with 5Volt, we MAY be able to shut off the relay, but we will likely not be able to switch it on again with a 3.3Volt signal.

So it seems the  single gate is not the best solution, which brings us back again to using a transistor:

The 10k resistor is probably not really necessary, as it is sufficient to pull down the collector (and thus the signal line of the module) to activate the relay. Yet it might still be a good practice to add it.
A tiny 5×3 hole perfboard is enough to house 2 connectors, a transistor and 1 (or 2) SMD resistors.

With this slight ‘extension’ it is now possible to use a 3V3 signal as input and 5Volt on the relay. Also the signal is no longer inverted: A HIGH on the input will pull the collector LOW and thus activate the relay board. A LOW on the input will pull the collector HIGH (if a 10k resistor is used).

As a final word, the added resistor when left floating, will function as an amplifier and even small voltage on the base (like when you are keeping it in your hand, will lead to a current through the collector and thus pull the base of Q1 a bit low(er). Q1 will also work as an amplifier and will start allowing current to flow through the relay and the LED. The Green LED may start to glow and possibly the relay may begin to flutter.
This should not happen if the entire circuit is attached to a micro’ s GPIO pin that has a defined LOW or HIGH state. You could always opt to have a 10k resistor pull the input of our circuit LOW

Simple WiFi relay board (3)

In an earlier post, I discussed how to quickly  make  a one channel WiFi relay with a relay module an ESP8266-01 and some Dupont cables.
In a later post, I showed how to make a 4 channel WiFi relay with an ESP8266-01 and a 4 channel relay board.

Both posts were a consequence of 2 videos by Ralph Bacon, discussing his slightly disappointing adventures with 2 commercially available Wifi relay boards, based on an ESP8266-01.
Out of interest I bought one of those boards -knowing the shortcomings Ralph pointed out- that I presumed I could fix.

The board comes with an ESP8266-01s. It has a Reset button, which isn’t really useful. The GPIO0 drives the relay through a 7002 FET, GPIO2 drives an LED and TX and Rx are not connected to anything.

The crux of this board is that it is really designed for the ESP8266-01S, although it can be made to work with the regular ESP8266-01. The ESP8266-01S is a version that already has pull-ups on GPIO0 and GPIO2, albeit that one of those pullups is through an onboard  LED. The regular ESP8266-01 does not have these pull ups and the relay board  does not provide them.

The relay module uses only one pin, GPIO0. It is connected to the gate of a 2007 FET that switches the relay and that is exactly where the problem starts. During startup, the ESP needs GPIO0 to be HIGH, unless you want to go to flash mode, when it needs to be LOW.
But as the ESP8266-01S correctly pulls pin GPIO0 HIGH, the relay will immediately be activated at startup. Hence he designer added a pull down resistor. Due to the presence of the Pull up on GPIO0 on the ESP8266-01 board, that needed to be a very strong pull down resistor and a 2k resistor was chosen. But now the GPIO0 pin is pulled LOW at startup, putting it in flash mode.

There are some simple solutions for this:

  • remove the 2k resistor. However this will activate the relay at start up.
  • reroute the triggersignal to  come from pin GPIO1 (Tx) or GPIO3 (Rx) (but you still need to remove the 2k resistor)

The latter solution is fairly simpel because of the long PCB trace going from GPIO0 to the gate of the FET



A capacitive soil humidity sensor: Part 4

Earlier, I wrote some posts on a Capacitive Soil Humidity sensor, Two years down the line this still functions well.
However, a recent video of Andreas Spiess, presented a rather interesting capacitive soil sensor that works according to a somewhat different principle

My earlier sensor was an oscillator in which the soil sensor formed the capacitor determining the frequency of the oscillator. This frequency was subsequently measured with PulseIn

The sensor that Andreas touched upon  does it different: There is a fixed frequency oscillator, built with a 555. The square wave generated is then fed to the sensor that -as we know- is in fact a capacitor. To a square wave signal that capacitor however has a certain reactance, or for arguments sake a resistance that forms a voltage divider with a pure ohm type resistor (the 10k one on pin 3). The wetter the soil, the higher the capacitance of the  sensor, the smaller the reactance to the square wave, the lower the voltage on the signal line.
The voltage on the signal pin -that can be measured by an analog pin on the Arduino‐ is thus a representation of the humidity in the soil.

It is simple to build but economically it is wiser to just buy one as they are dirt cheap