-
Notifications
You must be signed in to change notification settings - Fork 5
Programming a Sonoff
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
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.
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
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.
Easy to do, the base just pops off.

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.
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

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).
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°.