How to add an I2C interface to a Sonoff WiFi Smart Switch.

The Sonoff brand is a whole series os IOT devices that use the esp8266 WiFi processor internally. The devices are easily hacked and are cheap. The Sonoff WiFi Smart Switch is the most straightforward device, which can turn on or off 110/220 Volt 10 Ampere remotely. That means you can have your ceiling fan, lamp or anything else connected t mains power remotely controlled by your phone or computer. But the software that comes with it isn’t very flexible and cannot be integrated into a home control system like NodeRED, OpenHAB or any other home automation system.

Firmware

The Sonoff devices are easy to hack, so you can put a better firmware like ESPEasy on it and integrate the device with any system. ESPEasy has an extensive list of extra modules that you can connect, like temperature, barometric pressure, humidity, light sensors, PIR sensors and a long list of other things.

But most modules connect with I2C, which is a serial interface for interconnecting ICs using a clock and a data line. The Sonoff Switch only have one extra pin available, making it impossible to connect more advanced sensors. But there is a way of fixing that. Here’s how to add that to add an I2C connector to your $5 Sonoff and open up a whole field of stuff you can do.

Let’s upgrade the firmware!

To be able to upgrade the firmware, you have to open the enclosure and find the five holes in the center of the PCB and solder a female header for easy programming. As you can see in the picture, I’ve already soldered in a 5-pin header. (Click image to enlarge). I have marked the pinouts in the i.Sonoff WiFi Smart Switch pinout photo

USB TTL to Serial Adapter photoTo be able to program the new firmware, you need a USB to TTL Serial Adapter. You need an adapter that is 3.3 volts compatible. Connect the 3.3 Volt on the USB adapter to the 3.3 Volt input on the Sonoff. Do the same for ground. RX on the adapter should go to TX, and TX on the adapter goes to RX. Yes, they are crossed over, too young to remember RS232 and Null-cables, you whippersnapper?

But don’t connect mains power! You should never have the Sonoff device connected to mains power when upgrading firmware! The USB Serial Adapter provides 3.3 Volts needed by the esp8266. Doing it wrong is a good way of getting a punk hairdo or maybe wings and a harp…

Now, all you have to do is to download the firmware and follow the instructions here to install it. The size of the memory on the Sonoff WiFi Switch is 1024k as standard. Either do the upgrade with the available memory or maybe you want to increase the memory of your Sonoff? Read on…

Upgrading The Memory

If you want to make your Sonoff future-proof, there is a way of updating from 1MB of memory to 4MB. This is handy as the ESPEasy firmware evolves and get’s more substantial. Or maybe you want to write your own code and need the space? If you plan to do OTA[1], you need double the memory of the size of your program. You can do wireless updates without having to resort to using the USB to Serial adapter after the first install. This isn’t a must because ESPEasy works with just 1MB, but it’s better to have 4MB.

I prefer to isolate plastics and other heat sensitive parts with heat resistant Capton® tape. I use a hot air soldering station to remove the old memory IC, but you can use a soldering iron. Just fill the tip of the iron with a big blob of solder and heat all of the legs on one side of the IC at the same time while bending the chip up carefully with a screwdriver. Then you do the same thing on the other side.

Image of the Sonoff WiFi Switch Memory IC

The replacement IC is called Winbond W25Q32FVSIG, and you can get 10 of them for around $3 on eBay. Make sure that the dot-marking indicating pin 1 is lined up correctly and solder on the new IC. Now you have a 4MB Sonoff! Naturally, if you flashed the firmware before upgrading you need to flash the firmware on the new memory chip.

Sonoff WiFi Smart Switch with I2C

Now let’s add I2C to the Sonoff Switch.

You’ll find the esp8266 IC on the underside of the PCB. We need to solder two small wires to the pins on the esp8266 to get I2C. We need to solder the wires directly to the esp8266 IC because the pins are not connected to anything on the PCB.

Sonoff Switch PCB underside photo

The processor doesn’t have hardware I2C support, so you can use two free pins to connect, but usually (and in ESPEasy), GPIO4 and GPIO5 are used for I2C connections. Here I have soldered a thin wire to pin 24, which is GPIO5(SCL).

Microscope view of the Sonoff board with a red wire soldered to the GPIO5 pin on the corner of the ESP8266

After you soldered the wire, make sure to glue the wire to the PCB. Hot glue works fine. This keeps the wire in place and doesn’t snap when routing the wire later. Then I soldered a wire to pin 16, which is GPIO4(SDA) and hot glued it to the PCB. You now have the hardest part done!

The GPIO4 and GPIO5 wires held to the board with blobs of hot glue so they cannot snap off the ESP8266 pins

Yeah, it looks like an enormous amount of glue, but it’s a couple of millimeters.

You need a good magnifier glass or preferably a microscope designed for working with SMD components.

If you use thin wire, you can just take it up between the enclosure and the PCB. I added a 4-pin female header connector and soldered a 90-degree male header that I connect to the programming pins. Then I just soldered the 3.3 Volt, GND, the two I2C wires to the connector. Everything you need to connect a module using I2C.

The opened Sonoff enclosure with four coloured I2C wires run from the board out past the edge of the case

On the first one I made, I glued the female header on the PCB approximately where you can see it in the image above and made a hole in the enclosure cover. On the latest one, I just made an opening on the top of the box and hot glued the connector there. Install the cover and connect it to the thing/things you want to be able to turn on/off. Now with I2C, it can do double duty as a temperature sensor, dust sensor or many other possibilities. And now your Sonoff can connect several I2C sensors on the same pins. Pretty cool!

Configuring ESP Easy

Now it’s time to try it out. After you’ve done the initial setup of the Sonoff device, select “Devices” in the setup menu. Select the sensor you just connected to your Sonoff. In this case, I have attached a BME280 Temperature, Barometric Pressure, and Humidity sensor.

The ESP Easy device dropdown open on a Sonoff named Bedroomfan, with Temperature & Humidity & Pressure - BME280 highlighted

When you select the sensor, you get the setup screen for the selected device.

 The ESP Easy task settings page for the BME280, with Send Data ticked and the temperature, humidity and pressure value names filled in

Now you can specify the number of decimals and even do calculations, for example, adjusting the Pressure to accommodate for height over sea level. Just enter %value%+4.0 in the Formula Pressure field to add 4 to the hPa value. Just remember that you can’t use spaces in formulas.

The values are sent to the MQTT broker that you configured, or to another Home Automation System.

 A Node-RED dashboard for the bedroom showing gauges for temperature, humidity, barometric pressure and battery, above a ceiling fan toggle and its on and off temperature sliders

Here is an example of my Node RED server with Mosquitto MQTT broker that I use for controlling all the stuff at home. This one turns on the ceiling fan when it gets too hot in the bedroom. I can adjust the settings for temperature via a web browser.

A wall-mounted Sonoff with its Wi-Fi LED lit green and a blue DHT11 temperature and humidity sensor hanging off the front on a short ribbon

A small blue BMP180 breakout board taped to the front of a Sonoff, its SDA, SCL, GND and VCC pins labelled

A tiny OLED module fitted to the front of a Sonoff, displaying uptime, the IP address 192.168.1.61, 23.97 C and 31.29 percent humidity

The Sonoff board out of its case, with the I2C wires soldered on and heatshrink over the joints where they pick up power

An unmodified Sonoff Wi-Fi smart switch, rated AC 90-250 V and 10 A max load

  1. Over The Air updates. A way of programming the esp8266 via WiFi instead of having to upgrade with a USB to Serial adapter cable.  ?