MP1584 buck converter module

This small module MP1584 buck-converter module seems to be a good solution to power small circuits from higher voltages. Especially cool with this chip is that it accepts input voltages up to 29V. This makes it a perfect candidate for additional circuits that connect to a KNX bus. But it’s not limited to KNX buses. If you want to build a WiFi interface for your Roomba, you also have to down-regulate the 15V battery voltage to 5V or even 3.3V. You want to something to power a circuit from a car battery? This seems to be a perfect circuit for there use cases.

mp1584With it’s tiny dimensions of 17 x 22mm it’s size is around the size of 2 MicroSD cards (and much smaller than a single SD card).

The output voltage of the module is controlled by a tiny potentiometer. You will need a multimeter to check the output voltage before connecting it to a circuit.

While the plain voltage range data seems to be quite good, how does the circuit behave on different loads? Some Chinese dealers claim that similar modules should handle 3A load. If you just look at the size of the inductor you might already start thinking that this most likely isn’t true. However, DC resistance of the inductor is approximately 10mOhm (I miss my Kelvin probes and therefore could only measure this very roughly).

Let’s start with 29V input voltage – this will bring everything to the limit. Output voltage is fixed to 5V in this experiment. Without any current drawn, it still looks a bit rough:

29_0

However, a voltage swing of (Vpp) 98mV is no problem at all. Between 0.1A and 0.9A the circuit behaves quite well. Vpp is around 0.2V (a bit lower on low currents, higher in higher currents)

29_0.5

We reach the end at 1A. Now the regulator doesn’t provide a stable output voltage anymore:
29_1

But what about lower input voltages? Let’s go the the other “extreme”: 9V (it will work with even less, but let’s give it a bit headroom).

Almost perfectly clean with no load:
9_0

A bit more stable than at 29V for currents between 0.1A and 0.7A:

9_0.4

And at 0.7A we reach the end of the useable current range:

9_0.7

Conclusion: This module works fine for almost every embedded use case: Arduino, ESP8266 WiFi modules and even a Raspberry Pi without any additional USB devices plugged in.

Here are 2 animation that shows the output voltage under different loads:

29V input, 5V output, 0-1A in 0.1A steps:

29v_animated

9V input, %V output, 0-0.7A in 0.1A steps:

9v_animated

Cool sensors for your next automation project

Home automation isn’t about controlling a lamp with your smartphone. It is called “automation”. A home automation software should do things automatically. Do do this, it needs to have an idea what’s going on in your apartment (or outside). For this, you need sensors. While some sensor types are well-known you might not know what is available already on the market for a small budget. The following sensors can be integrated easily with your Arduino, EPS8266 or Raspberry Pi.

Temperature

35040-img_0674Almost everybody already did experiments with temperature sensors. The DS18B20 is a 1-Wire sensor that can be connected easily to many platforms. You will find a lot of code snippets for this sensor already. It has a 0.5 degree celsius accuracy which is more than enough for most use cases. If you only want to monitor the temperature, go for this one.

Humidity

dht-11-1The DHT-11 and DHT-22 are two sensors that combine a temperature sensor with a humidity sensor. Both are more expensive than a simple temperature sensor. However, humidity informations might help you to do better automatisations. One use case could be turning on an air humidifier when the humidity is too low. Another would be opening windows, when humidity is too high. While these sensors also use a 1-Wire-type protocol, it is different from the 18B20 sensor. You can’t connect both sensors to the same GPIO. However as the DHT-11 has the temperature sensor already included, there is no need for an additional 18B20.

Soil moisture

13995201090You’re plant are dying regularly because you forget to water them? Another use case for a sensor. Soil moisture sensors are available in 2 variants: the cheap sensors just measure the soil resistance. The problem with this sensor type is corrosion (over a longer lifetime). Capacitive sensors are becoming more popular and don’t show this problem as the electrodes are isolated. However, they are more expensive and a bit less sensitive. You need to experiment a bit to find the correct threshold for alarming.

Motion detection

pir-motion-sensor-536x408You want to know if somebody is home? PIR motion sensors are also available for a few bucks and will give you information about people (or pets) moving. If somebody is just sitting or sleeping, the motion sensor won’t detect this. However, for many use cases this is just fine.

Sound pressure

grove-Sound-SensorYou’re not at home, but your teenage son. What do you think will happen? A party! Your neighbors will tell you tomorrow. Wouldn’t it be cool to automatically reduce the volume of your stereo system when the volume exceeds a specific level? This level might be higher at 5pm than at 11pm. A sound pressure sensor will do the job – at least until your son finds our where it is located and puts some damping onto it.

Small particles

Particle sensorSmall particles in the air can be a problem for some people – especially if you suffer from asthma. These emissions can come from various sources. Measuring the number of small particles in the air might give you an idea what might be the source and do something against it. These sensors are based on optical measurements. While this might sound complicated, sensors like this are not very expensive anymore.

Hacking the H801 LED dimmer

I just received the H801 LED dimmer. I couldn’t figure out, what the “W1” and “W2” connectors on this device were. So I removed the case and checked the board. I was pleasantly surprise to see that this isn’t a 3 channel (RGB) but a 5 channel device. The W1 and W2 connectors are 2 additional channels.

Installation isn’t complicated. You connect the power supply and the LEDs and turn on the device.

With a laptop or mobile device, you connect to the WiFi network HCX_856705 (the numbers might be different) and use the password “88888888”.  Now, the trouble begins if you don’t have an Android phone. I was expecting there is at least a simple web interface available that allows to configure WLAN credentials. Nope! The only way to control the device with the initial firmware is an Android app.

You now have 2 options if you don’t own an Android phone: flash another firmware or install an Android emulator on your PC. It used Droid4X. However, I wasn’t able to connect to the device. I’m note sure if this Application supports using the WLAN connection from the Mac.

Ok, back to the start. Flash another firmware. This is usually something that I don’t do easily, but on a device that costs less than 20$ and isn’t useable for me otherwise, I tried this. Luckily the board design makes it easy to flash a new firmware. The board is already prepared with 2 headers: RX/TX/GND/3.3V and J3. Just solder headers and use your existing ESP8266 programmer.

h801-header

As software I used the Arduino sketch from Eryk.io and adapted it slightly. My sketch can be downloaded from Github.

The GPIOs are used as follows:

Pin Function
15 Output red
13 Output green
12 Output blue
14 Output white 1
4 Output white 2
1 Internal LED green / Signal
5 Internal LED red / Power

433MHz window sensor

On eBay, Alibaba or other web sites, you can find cheap Window sensors that will send a data packet to inform you about the state of your windows. They often costs less than 10$ per unit, which makes it easy to test them.

They look like this:

Newest-High-quality-1Pcs-433-MHZ-Wireless-Home-Security-Door-Window-Sensor-Detector-with-Battery-for

But what’s inside? Let’s have a look. The part that is mounted on the window itself is basically just a magnet. You might already know what will be on the other side: a reed relay. But what else:

The electronic circuit is based on the HS1527. The nice thing about this chip is the 1uA standby current. Even with a small battery, the battery life will be relatively high. It is powered by a small 12V battery – I haven’t seen this battery type before.

The good thing about the chip is that you can use a lot of cheap 433MHz receivers connected to a Raspberry Pi or Arduino to receive the signals.

However, there is one major downside: This sensor sends only a data packet when you open the window, not when you close it. This is ok if you want to use it to detect break-ins, but it won’t work if you want to detect if a window is open (e.g. if you want to turn off the heating).

Therefore I can’t recommend this sensor type for home automation use.

Hue emulation on a ESP8266

You might already know that I like the ESP8266. There is a cheap LED dimmer named H801 available on AliExpress and other Chinese web sites that uses the ESP8266 to control RGB LED strips.  You find a bit technical information about in on this Dutch web site.

H801_small

One issue is the simple protocol this device is using. While it has been reverse-engineered, the protocol is still proprietary and that makes it hard to integrate it into existing home automation solutions.

One option is emulating a Phillips Hue. There is the Github project ESP8266HueEmulator that implements this. Have a look at this!

Note that the Chinese device shown above is not certified to be used within the US or EU!

Battery powered ESP8266 sensor

The ESP8266 is a powerful and inexpensive device. There is almost no need anymore to use proprietary RF protocols like the nRF24 chip that was popular some years ago. However, there is one major problem when using WLAN: power consumption.

When connected to a WLAN, the ESP8266 consumes at least 80mA – sometimes even more. If you use 3 2400mAh that means, they will be empty in a bit more than a day. This doesn’t sound like a good deal.

Luckily, for sensor applications, you don’t have to be connected to the LAN the whole time. You don’t even have to have the ESP itself running. To save as much power as possible, you have to do the following:

  1. Collect your data. If this takes time, do it before connecting to WLAN. If it takes just a few millesconds, it doesn’t matter much.
  2. Connect to WLAN.
  3. Send data.
  4. Go to deep sleep mode. You use the ESP.deepSleep() method for this.

You have to look at a few things when using the ESP.deepSleep() method:

  1. To wake up after sleeping, GPIO16 of the ESP8266 has to be connected to RESET. Otherwise the device won’t wake up again.
  2. The delay parameter is defined in microseconds (not seconds or milliseconds). If it looks like your device isn’t sleeping at all, you might have used a value that is too small (you won’t even notice if the device sleeps for 3600 microseconds)
  3. There is a second parameter that can have the values WAKE_RF_DEFAULT, WAKE_RFCAL,WAKE_NO_RFCAL, WAKE_RF_DISABLED. The WAKE_RF_DISABLED looks good, but beware to use it. Using this does not mean that the RF subsystem is disabled during sleep (it always is), but that it is disabled when waking up again. Your ESP8266 won’t be able to do any network communication anymore.

A few other tips to save energy:

  1. Do not turn on LEDs for a longer time. Flashing them a few milliseconds is usually a good visual feedback and will consume only a tiny bit of energy.
  2. If you need to have LEDs on for a longer time, dimm them. analogWrite is your friend for this.

Check out this sample code. It uses WiFiManager which helps to connect to configure your WLAN SSID/password and even the name/IP address of the MQTT broker.

#include         // Generic ESP8266 WiFi routines
#include        // MQTT client
#include           // Local DNS Server used for redirecting all requests to the configuration portal
#include    // Local WebServer used to serve the configuration portal
#include         // WiFi Configuration Magic


const char* mqtt_server = "192.168.1.88";
const char* mqtt_channel_up = "active";
const char* mqtt_channel_data = "sensor/%d/adc";

const int pin_red = 15;
const int pin_green = 12;
const int pin_blue = 13;
const int pin_button = 4;
const int analog_ldr = 0;

const int sleepSeconds = 60 * 15;

WiFiManager wifiManager;
WiFiClient espClient;
PubSubClient client(espClient);


void send_sensor_data(void) {
  Serial.println("Sending sensor data\n");
  int data = analogRead(A0);
  char s_channel[50];
  char s_id[10];
  char s_data[10];
  sprintf(s_channel, mqtt_channel_data, ESP.getChipId());
  sprintf(s_id, "%d", ESP.getChipId());
  sprintf(s_data, "%d", data);

  digitalWrite(pin_blue, HIGH);
  client.connect("ESP8266Client");
  digitalWrite(pin_blue, LOW);
  if (! client.connected()) {
    Serial.println("Could not connect to MQTT broker, doing nothing");
  } else {
    digitalWrite(pin_green, HIGH);
    client.publish(mqtt_channel_up, s_id); // show that this device is up
    client.publish(s_channel, s_data); // send sensor data
    digitalWrite(pin_green, LOW);
  }
}

void setup() {
  delay(500); // This makes it easier to upload new firmware and/or press the "clean" button

  pinMode(pin_red, OUTPUT);
  pinMode(pin_green, OUTPUT);
  pinMode(pin_blue, OUTPUT);

  digitalWrite(pin_red, HIGH);

  // Setup Serial interface for debugging
  Serial.begin(115200);

  //  Bring up WLAN
  WiFiManager wifiManager;

  //sets timeout until configuration portal gets turned off
  //useful to make it all retry or go to sleep
  //in seconds
  wifiManager.setTimeout(60);

  WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  wifiManager.addParameter(&custom_mqtt_server);

  if (digitalRead(pin_button) == 0) {
    // reset settings
    wifiManager.resetSettings();
  }

  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  if (!wifiManager.autoConnect()) {
    Serial.println("failed to connect and hit timeout");
    delay(100);
    // do nothing: deep sleep, but longer than usual. Something might be wrong and
    // we should save power
    ESP.deepSleep(sleepSeconds * 10 * 1000000, WAKE_RF_DISABLED);
  }

  mqtt_server = custom_mqtt_server.getValue();

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Setup MQTT client
  client.setServer(mqtt_server, 1883);

  digitalWrite(pin_red, LOW);
}

void loop(void) {
  send_sensor_data();
  Serial.printf("Sleeping deep for %i seconds...", sleepSeconds);
  ESP.deepSleep(sleepSeconds * 1000000);
  delay(100);
}

Program an ESP8266 from Arduino on MacOS

There are a lot cheap ESP8266 boards available on the market. The modules are quite powerful and come with WiFi. While this is a problem for ultra-low-power applications (there are tricks to deal with this), it is a great platform for simple sensors or notification applications. You can even program them with the Arduino IDE. This guide shows how to do this.

Install the Arduino IDE

You can download the IDE from the Arduino web site

Connect the ESP8266 module to your computer with an USB cable

Many ESP8266 modules already come with an integrated USB interface. Just connect the module with your Mac.
Note: Some modules have a USB port on the board that can only be used to power the board but isn’t made for communication.

Check that the device is recognised

Open the “About this Mac” dialog and click on “System report”
systemreport

You should now see the a USB2.0 serial device. usbserial

Install the driver

If you now start the Arduino IDE, you can select a device like /dev/cu.usbmodem147741 (the numbers can be different) as the serial interface. However, if you try to communicate with the board, it won’t work. While MacOS recognised that the device is a serial interface, it does not come with the driver for the cheap Chinese CH340G USB/serial converters (in the past some companies used fake FTDI chips until FTDI implemented counter-measurements in their drivers that disabled some fakes). However, installing the driver is not complicated. Download it, and install it. After a reboot, you Arduino IDE should now offer an devices named like /dev/wchusbserial14460. This is the correct serial interface device now.

Install the ESP8266 modules for the Arduino IDE

Arduino does not come with out-of-the-box support for the ESP chip. To install the software, go to the preference dialog and enter
http://arduino.esp8266.com/versions/2.2.0/package_esp8266com_index.json
under “Additional board manager URLs”.

arduino-settings

Now click on “Tools/Board/Board Manager” and install the ESP8266 modules from the “Contributed section”.

espcontrib

That’s it. You have successfully installed all necessary tools to start programming your new IoT device.

If you can’t program your ESP device from the Arduino GUI, have a look at esptool.py. This seems to work better than esptool bundled with Arduino. I did not find a simple way to integrate it directly into Arduino. However, starting it from command line is not very complicated. Just check the command line arguments.