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.

Advertisements

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:
{channel=”wifiled:wifiled:DC30B89D35CE:power”}
{channel=”wifiled:wifiled:DC30B89D35CE:white”}
{channel=”wifiled:wifiled:DC30B89D35CE:color”}
{channel=”wifiled:wifiled:DC30B89D35CE:programSpeed”}

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
DOWN 0xFFB847
OFF 0xFFF807
ON 0xFFB04F
R 0xFF9867
G 0xFFD827
B 0xFF8877
W 0xFFA857
1 0xFFE817
2 0xFF48B7
3 0xFF6897
FLASH 0xFFB24D
4 0xFF02FD
5 0xFF32CD
6 0xFF20DF
STROBE 0xFF00FF
7 0xFF50AF
8 0xFF7887
9 0xFF708F
FADE 0xFF58A7
10 0xFF38C7
11 0xFF28D7
12 0xFFF00F
SMOOTH 0xFF30CF

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
    GPIO.setmode(GPIO.BCM)
    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)
       time.sleep(0.001)
       GPIO.output(pin, GPIO.HIGH)
       time.sleep(0.001)
    GPIO.cleanup
  • 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

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

Dimming an AC lamp via MQTT

Years ago I published a TRIAC dimmer that could be controlled by a simple microcontroller such as an arduino. Times have changed and right now it is of more importance to be able to control that lamp from a home automation system such as OpenHab, Homematic or NodeRed.

So, let’s have a look at the circuit first:
This is a rather classic circuit. It detects the zerocrossing on the grid and then a microcontroller ignites the TRIAC with a time delay that determines the brightness of the attached Lamp.  The full cycle is 10mS (at 50Hz)
A circuit like this is easy to make for very low cost (1-2 Euro).

However, if you shy away from soldering  such a circuit, there are ready made modules available as well:
You should not need to pay more than 3-4 USD for such a module. Anything above that is robbery

Software to control this dimmer would look something like this:


#include <Ethernet.h>
#include <PubSubClient.h>
#include  <TimerOne.h>

#define CLIENT_ID       "Dimmer"
#define PUBLISH_DELAY   3000

String ip = "";
bool startsend = HIGH;
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x07};

volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin1 = 4;// Output to Opto Triac
int dim1 = 0;// Dimming level (0-100)  0 = on, 100 = 0ff

int inc=1;          // counting up or down, 1=up, -1=down
int freqStep = 100; // make this 83 for 60Hz gridfrequency

EthernetClient ethClient;
PubSubClient mqttClient;
long previousMillis;

void zero_cross_detect() {    
  zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
  i=0;
  digitalWrite(AC_pin1, LOW);       // turn off TRIAC (and AC)
}                                 

// Turn on the TRIAC at the appropriate time
void dim_check() {                   
  if(zero_cross == true) {              
    if(i>=dim1) {                     
      digitalWrite(AC_pin1, HIGH); // turn on light       
      i=0;  // reset time step counter                         
      zero_cross = false; //reset zero cross detection
    } 
    else {
      i++; // increment time step counter                     
    }                                
  }                                  
} 

void setup() {
  
  attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
  Timer1.attachInterrupt(dim_check, freqStep);
  pinMode(4, OUTPUT);

  // setup serial communication

  Serial.begin(9600);
  while (!Serial) {};
  Serial.println(F("dimmer"));
  Serial.println();

  // setup ethernet communication using DHCP
  if (Ethernet.begin(mac) == 0) {
    Serial.println(F("Unable to configure Ethernet using DHCP"));
    for (;;);
  }

  Serial.println(F("Ethernet configured via DHCP"));
  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());
  Serial.println();

  ip = String (Ethernet.localIP()[0]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[1]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[2]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[3]);
  //Serial.println(ip);

  // setup mqtt client
  mqttClient.setClient(ethClient);
  mqttClient.setServer( "192.168.1.103", 1883); // <= put here the address of YOUR MQTT server //Serial.println(F("MQTT client configured")); mqttClient.setCallback(callback); Serial.println(); Serial.println(F("Ready to send data")); previousMillis = millis(); mqttClient.publish("home/br/nb/ip", ip.c_str()); } void loop() { // it's time to send new data? if (millis() - previousMillis > PUBLISH_DELAY) {
  sendData();
  previousMillis = millis();

  }

  mqttClient.loop();
  Serial.print("dim1 in loop = ");
  Serial.println(dim1);
}

void sendData() {
  char msgBuffer[20];
  if (mqttClient.connect(CLIENT_ID)) {
    mqttClient.subscribe("home/br/sb");
    if (startsend) {
     
      mqttClient.publish("home/br/nb/ip", ip.c_str());
      startsend = LOW;
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  char msgBuffer[20];
  
   payload[length] = '\0';            // terminate string with '0'
  String strPayload = String((char*)payload);  // convert to string
  Serial.print("strPayload =  ");
  Serial.println(strPayload); //can use this if using longer southbound topics
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");//MQTT_BROKER
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  Serial.println(payload[0]);

dim1=strPayload.toInt();

}

The value freqStep is usually set at 75 or 78 with this type of circuits, which allows for 128 steps of dimming at 50Hz gridfrequency. I have set the value here purposely to 100 allowing for only 100 steps, which is convenient for use in OpenHAB as  the slider goes from 0-100%.
The calculation is as follows:

50Hz
As we have two zerocrossings per sine wave, the frequency of zerocrossings is 100Hz. Therefore the period between two zerocrossings is 10mSec is 10000uS.
So if you want to have 100 levels of dimming the steps you need to take are 10000/100=100uS

60Hz
The frequency of zerocrossings is 120Hz, therefore the period between two zerocrossings is 8.3mS is 8300uS. So if you want to have 100 levels of dimming, the steps you need to take are 8300/100=83uS -> freqStep=83

 

Adding a popular 5Volt 4 channel relay board to a 3V3 processor (beginners)

Chinese webshops sell a 5Volt 4 channel relay board that is quite popular amongst hobbyist. Sometimes I get the question if it can be used with a 3V3 microprocessor. This ofcourse is very basic Ohms law, so just skip this article if you already know, but for those who still have questions: read on.

Well in short: any relay can be made to work with any microprocessor, but lets look at how this can be done in an efficient way.

Normally, if you have a bare 5Volt relay, it would only require a transistor or a FET to drive the relay with any voltage sufficient to open the transistor. The relay board in question though already has some electronics around it, so lets have a look at the circuit:

It is quite clear that this is not the standard one transistor relay board: the Transistor driving the relay in itself is driven by an optocoupler, that also inverts the signal: It is a LOW on the INx input that will  bring the optocoupler to open, thus feeding the base of the transistor, that will subsequently open and activate the relay. The circuit also shows that the optocoupler can be fed from the same  voltage source as the  relay, or from a different voltage source.

First question we have to ask ourselves: is a 3v3 circuit enough to drive the board?
Well, suppose we feed the board entirely with 5Volt. If we then would connect a 3V3 processor pin such as from a raspberry or ESP8266 or  a modern Arduino, a LOW would indeed activate the relay, as described above, but what happens if we do a HIGH?
Well, with a HIGH there would be a voltage of (5-3.3=) 1.7 volt over the series resistor, the optocoupler and the LED. Given the fact that the  forward voltage of the  optocoupler is about 1.5 Volt and the forward voltage over the red LED is on average 2.2 Volt, that basically absorbs the 1.7 Volt making it safe to say there will be no current flowing through the optocoupler when the IO pin is made HIGH (3V3).
Factually, even if the ‘HIGH’  would only be 2.5 Volt, chances are slim that there would be any current flowing through the optocoupler.

Now ofcourse there are people who shrug at the idea of  having any 5Volt source connected to their 3V3 pins, so what happens if we  take away the Voltage jumper, feed the relay with 5Volt and the optocoupler with 3V3.

Well, suppose we again make the IO pin LOW, then there will be 3V3 over the optocoupler, the LED and the resistor.
The LED and the optocoupler will  have a voltage drop of 1.5+2.2=3.7 Volts. Ergo, there will not be any current flowing through the optocoupler and the relay will not be activated.

Therefore: yes, the relay board can be used with a 3V3 processor, but  you will have to feed the entire board with 5Volt.

But, what if I do not want the signal to be inverted, if I want a LOW to deactivate the relay and a HIGH to activate it?

Well, that is fairly easy: do that with an inverter. One can either use an inverter IC such as a 7404, 7414 or a 4069, but ofcourse then you’d have to wonder whether 3V3 would be enough input for those IC’s  (it is).
Another possibility is just a transistor with a base resistor 470-1000 Ohm (1x for every channel).