I like Sonoff Basic R2 Smart Home Switch as it is low priced but very functional multipurpose device. Most of all, it contains ESP8266 chipset which means it is easy to replace original firmware with custom one. I use ESPHome firmware but Tasmota is also good.
By looking at the board of this little device I found out that it contains two-colored LED. Actually, two LED-s are in one package with three contact legs. Green LED is used in factory assembly, and it is connected to GPIO13. The second LED is red colored and not used. It is actually meant to be used by optional RF module. for one of my projects I thought that second LED would be good to be used for additional status signalling. So I investigated how to make use of it.
Examining schematics showed me that there is complete LED driving circle on the board for red LED. It just needs signal to turn it on and off. LED is connected to additional connection header on the board (which is not installed) and not used.
To be precise, it is actually connected to ESP8285 chip (variant of ESP8266) to MTCK pin (in Sonoff Basic schematics it is also marked as PWM1). That is GPIO13 on the chip. I actually tried to use it but it created just strange effects. As this is port that has special use, I did not bothered a lot about using it. Maybe I will play with GPIO13 later.
Next what I tried was to use GPIO2 pin that has exposed soldering contact on the board. That did not work either. GPIO2 must not be connected to GND when device is turned on and that is exactly what happened when I connected LED circuit to it. So it was unusable for this purpose but may be used for something else.
Then I decided to try with RX port of serial connection. That is GPIO3 on ESP8266. It can be connected to the GND in any time. And indeed, LED lighted on. And it did not even interfere with normal usage of serial port (for uploading firmware).
So that was simple solution: one short wire soldered to connect RX pin on board with red LED circuit. Easy to do if you ever soldered anything. You just have to clean up everything up after soldering and pay attention for unwanted short contacts.
This actually gives three colors. As LEDs are controlled independently, that means they may be turned on at the same time. This gives orange-greenish color. May be useful.
Well that was fun part of the project. Now comes even more fun:
Setting up firmware to activate newly connected LED was easy. I will show how I did it in ESPHome but it may be easily done on other platforms.
I will not go into details how to prepare and load firmware to Sonoff Basic. I expect you know how to do that. just add new device to ESPhome and use this hints to configure it for your purpose.
First set up outputs for LEDs:
output: - platform: esp8266_pwm id: green_led_output inverted: true pin: GPIO13 - platform: esp8266_pwm id: red_led_output inverted: false pin: GPIO3
Outputs are set for green and red LED on pins diodes are connected to. For red LED we need to set inverted to false as it’s circuit is not inverted.
Then set lights for LEDs:
light: - platform: binary id: green_led name: Green LED output: green_led_output - platform: binary id: red_led name: Red LED output: red_led_output
This provides nice interface to control lights. If you want more fun, then play with various platform values and light effects. Check Light Component for ideas.
As main function of Sonoff Basic is to be a switch, you should set up relay control:
switch: - platform: gpio id: relay name: Relay pin: GPIO12 restore_mode: ALWAYS_OFF on_turn_on: - light.turn_on: red_led - light.turn_off: green_led on_turn_off: - light.turn_on: green_led - light.turn_off: red_led
Other than setting up switch at port GPIO12, I decided to set restore_mode to make sure relay is off when Sonoff Basic is powered up. I also prepared actions on_turn_on and on_turn_off to set LEDs according to state of relay. For this example I decided that green light means relay is in off state and red means it is in on (hot) state. This is just to show off, you can use LEDs as you wish.
Last component left is controlling the button on this Sonoff Basic. I decided to keep it simple: each press on button toggles relay on and off.
binary_sensor: - platform: gpio id: button name: Button pin: number: GPIO0 mode: input: true pullup: true inverted: true on_press: - switch.toggle: relay
I guess configuration is self explanatory. Momentary switch is connected to GPIO0. When pressed it toggles relay state. LEDs would light according to relay state. This configuration does not influence secondary function of the button to activate firmware update mode.
And for the last, set up initial state of the switch using on_boot action.
esphome: name: $devicename on_boot: then: - switch.turn_off: relay - light.turn_off: red_led - light.turn_on: green_led
It makes sure that relay is turned off. that should also trigger on_turn_off action for relay to set LED states, but it does not happen each time. Just to be sure, set up initial states for LEDs according to relay state: red is off and green is on.
And it is working!
Download full code
Provided code excerpts should be easy to use, but sometimes it is easier when seeing big picture. Here is whole configuration in one piece available for download: sonoff-basic-r2-two-led.yaml (351 downloads) .
Naturally, you should use it just as reference and adjust it for your own preference and need.
Sonoff Basic is used connected to mains voltage that is dangerous for life!
Before opening this device for modification make sure it is disconnected from the mains! Keep mains plug at sight to make sure it is not connected! Check regularly if it is disconnected.
Do not connect serial adapter cable for firmware update while Sonoff Basic is connected to mains power!
Never assume! Assumption is mother of life danger! Do not connect it to mains while it is open! With high voltage you can screw up just once!