Skip to content

Programming a Sonoff

Mitra Ardron edited this page Feb 4, 2026 · 10 revisions

Frugal IoT - Sonoff switches

Sonoff Basic switches are a great solution for basic power control - lights, fans etc.

They can be purchased online, I get them currently (Nov 2025) from about €4, including shipping to Australia.

For other countries: Indonesia - plenty of suppliers on Tokopedia: [~90k IDR](https://www.tokopedia.com/search?st=&q=sonoff basic r4);

Both the R2's and R4's work well. The main differences are.

R2 R4 Notes
LEDs 2 1 The second R2 LED just shows when its activated
Memory 1Mb 4Mb 1mb is too small for OTA
OTA No Yes

At the moment the R4's are only a little more expensive, so the OTA capability makes them preferably for me.

Some of the other Sonoff products may work well, but I have yet to try them and would appreciate reports (feel free to edit this page).

As far as I'm aware, competing projects - e.g. the TUYA series, are not programmable.

Before I describe how to program them, I want to thank Jonathan Semple for suggesting them, and porting Frugal IoT to them - it only took a few lines of code, and was a good test of the system.

For any questions or problems see Issue 108

Programming Sonoff Basic R2

Follow the instructions at Programming and Configuring a Device on PlatformIO or Arduino IDE to set it up.

The example to clone for this sensor is examples/sonoff

But ... its not as simple as plugging a cable in.

Prerequisites

FTDI programmer.

image

An FTDI programmer converts the signal from a USB port, to the levels needed for programming.

I used one I got on AliExpress for about €1, others should work the same, but check the pinout matches, or adjust the cable accordingly. (Indonesia: tokopedia 17k5 IDR

Left to right: DTR RXD TXD 5V CTS GND

FTDI to Sonoff cable

sonoff pinout

The Sonoff pinout is not the same as the common FTDIs so a matching cable is required.

You can make up a cable in many ways, I used a ribbon cable I already had, prized the per-cable sockets out of the header, and rearranged them. This sounds tough, but was a 10 minute task.

image

Step 1: Dismantle the switch

Easy to do, the base just pops off.

sonoff dismantled

The programming pins can be seen at the top right. Some people recommend soldering a socket there, but unless you plan ongoing development on one device it really isn't necessary.

Step 2: Programming

IMG_1722.1.mov

Programming is a little tricky - you have to, in order:

  • Make sure it is disconnected from mains power !
  • Press the Sonoff button
  • Insert the cable - making sure its the correct orientation, use slight pressure to keep the pins in contact with the holes.
  • Release the Sonoff button
  • Upload from Arduino IDE or PlatformIO

Step 3: Wiring for mains

IMG_1723

I cut a standard extension cord in the middle and wired the live and neutral to the input and output connectors of the Sonoff. In Australia we have a ground wire which I left intact and running behind the Sonoff (green and yellow cable, not easily visible in the picture above).

Step 4: Test it

image

Connect a light, or similar to the extension cord.

The Sonoff should connect to your WiFi the same as any other device (i.e. use the data to pre-program, or the captive portal when first installed). Once connected, you'll see a box in the UX.

Clicking that Relay box should turn the light on or off.

It should work with the standard controls (included on any device - including the Sonoff - with for example CONTROL_HYSTERISIS_WANT).

The control infrastructure works well now, i.e. it is perfectly possible to have a fan that turns on when for example temperature exceeds 30°.

Clone this wiki locally