Sonoff Basic With Touch & Movement Sensors!

I love Sonoff Basics.

They are a cheap and versatile way of adding ‘smarts’ to ‘dumb’ devices. In addition, they are dead easy to flash with alternative firmware such as Tasmota or ESPHome, providing full local control when used with Home Assistant and other Smart Home solutions.

This little project came from the following circumstance:
One of our bedrooms has touch lamps on the bedside tables. The type that increases in brightness with each tap, before turning off. On our particular lamps, the brightness variation was pretty pointless so the taps were just used to turn them on and off. The same bedroom has some rather ‘difficult’ wiring to the main lighting and the switches are on brick walls with back boxes only 12mm deep (and no neutral) so making it controllable via Home Assistant wouldn’t be straightforward. I wanted some form of smart lighting in the room, just so it could be controlled by the door opening during the dark hours. The table lamps seemed to be the obvious target but they were touch and you can’t just stick a smart plug on a touch lamp. I could have purchased new lamps but that would have meant scrabbling for a switch to turn them off/on from the bed. And well, where’s the fun in that?!

After a little research, I discovered that you can actually hook additional sensors up to a bog-standard Sonoff Basic. The RX and TX pins commonly used for flashing are actually exposed GPIOs that can be utilised for other stuff once the initial flashing is complete. There are a couple more GPIOs available (you can nick the LED, and there is a tiny solder pad for GPIO2) but, for ease of connection, I stuck with the two that were readily available. This would allow me to remove the internal touch module on the lamps and have external touch sensors, mounted (more) conveniently, on the underside of the bedside tables. In addition, I could hook up a motion sensor on the side of the bed that is directly opposite the door. Net result, lamps controllable via Home Assistant that can also be controlled via touch sensors, together with the addition of a rather useful motion sensor.

For this project, I used TTP223 capacitive touch sensors and AM312 PIR modules. Both are small, readily available (Amazon, AliExpress, Ebay etc) and dead cheap.

Sonoff Basic Wifi Smart Switch
Sonoff Basic Wifi Smart Switch

TTP233 Touch Sensor

AM312 Motion Sensor

This isn’t going to be a full and complete set of instructions but I will run through the basics of how I got this up and running. Hopefully it will give you some ideas of how you can expand a Sonoff with extra abilities!

I’m going to assume you already know how to do the flashy bit on the Sonoff Basic. If not, there is a great guide linked on the ESPHome web site and, if you haven’t delved into flashing devices before, the Sonoff Basic is a very easy device with which to get your feet wet.

Both the touch sensor and PIR are binary sensors (either off or on). They are dead easy to connect - VCC goes to the 3V3 pin and GND to GND on the Sonoff. The I/O pin (middle pin on both sensors) goes to the respective GPIO pin on the Sonoff.

There is a small complication in that both of these sensors are pulled ‘low’ by default. Unfortunately, pulling GPI01 (the TX pin) low on a Sonoff Basic at start-up will prevent it from booting. If you are only using one of these sensors, not an issue. If, however, you want to use both, something has to be done. Luckily, the touch sensor has a couple of solder pads labelled A and B that can be used to change the mode of operation.

There are lots of overly complicated diagrams on the interwebs containing mysterious translations of what A and B do. I’ll pull it down to one sentence.
Pads on B linked - Changes default touch state from momentary to latching, Pads on A Linked - Changes default state from Off to On.

On = High so if we blob a bit of solder between the A pads, our Sonoff will boot. An unfortunate side-effect of this change is the red LED indicator on the TTP223 will be permanently lit (unless the sensor is activated) but this is easily remedied by either desoldering the LED or just nipping it with some side cutters. As said earlier, you only have to meddle with this if using both sensors. If only using touch, just connect it to GPIO3 (RX) which doesn’t affect the boot sequence.

Messy Breadboard Functionality Test

Sonoff Attached To Its 3D Printed Case Base Plate

Remote Sensor Housing

The remote sensor is connected to the Sonoff using wire scavenged from a USB cable, with the VCC and GND wires being split off to each sensor (at the sensor end), the remaining 2 conductors in the USB cable being used for the I/O of each sensor. Sensors are mounted in a two-part custom designed and 3D Printed case. Bottom part holds the sensors, top part sticks to the underside of the bedside tables with 3M sticky pads. The two-part design allows easy access to the sensors for wiring and also give me the ability to swap out sensors at a later date if needed.

The Sonoff Basic is housed within a 3D Printed case, designed from scratch but based upon an existing Thingiverse model. A slot needs to be cut in the side of the standard Sonoff case (which is still used) to allow entry for the USB cable. A zip tie is used as a simple method of strain relief at the Sonoff end.

Complete Sonoff Case

PIR and Touch Sensor Housing

3D MODELS

Sonoff Basic Case: https://cults3d.com/en/3d-model/gadget/sonoff-basic-case-space-for-earth-snap-top

Modular Sensor Mount: https://cults3d.com/en/3d-model/gadget/modular-esp-sensor-mount

ESPHome YAML Configuration

Included below is a sample ESPHome config that can be used as a starting point for the combined PIR and Touch sensor. Touch is pretty much standalone as there is a small bit of code that effectively toggles the Sonoff relay with each touch. The PIR is simply exposed as a sensor which can be used within Home Assistant automations.

As these are both binary sensors, it's easy just just use one of them (I only have touch on one side of the bed).

substitutions:
# Change the lines below with your chosen device and friendly names
  devicename: sonoff-basic-with-sensors
  friendly_devicename: Sonoff Basic Touch and Movement


esphome:
  name: "${devicename}"
  platform: ESP8266
  board: esp8285

wifi:
  ssid: #replacewithyourSSID
  password: #replacewithyourWiFiPassword

# Uncomment and edit the lines below for your static IP
# Leave comments or delete if using DHCP and an IP reservation
#  manual_ip:
#    static_ip: 192.168.0.140
#    gateway: 192.168.0.1
#    subnet: 255.255.255.0


  ap:
    ssid: "${devicename}-hotspot"
    password: ""

captive_portal:
logger:
api:
ota:
  - platform: esphome
web_server:
 port: 80

# Set up Sonoff Button
binary_sensor:
  - platform: gpio
    id: push_button
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    internal: true
    on_press:
      if:
        condition:
          - switch.is_off: relay
        then:
          - switch.turn_on: relay
        else:
          - switch.turn_off: relay
# End of Sonoff Button Setup

# Set up Touch Sensor on GPIO1
  - platform: gpio
    id: touchsw
    pin: 
      number: GPIO1
      inverted: True
    name: "${devicename} Touch"
    device_class: power
    
    on_press: 
      if:
        condition:
          - switch.is_off: relay
        then:
          - switch.turn_on: relay
        else:
          - switch.turn_off: relay
# End of Touch Sensor Setup

# Set up PIR on GPIO3          
  - platform: gpio
    id: pir
    pin: 
      number: GPIO3
    name: "${devicename} Motion"
    device_class: motion
# Lines below add a delay to the PIR turning off
#    filters:
#      - delayed_off: 60s    
# End of PIR Setup

# Set up the Sonoff Relay
switch:
  # The relay switches on the red side of the LED when active.
  - platform: gpio
    name: "${devicename} Relay"
    pin: GPIO12
    id: relay
# End of Sonoff Relay setup

# Following lines add a reboot switch    
  - platform: restart
    name: "${friendly_devicename} Restart"
    id: reset

Comments

You should also read:

IRONING WITH SIMPLIFY 3D

Ironing is now included with Simplify 3D Version 5 which may well render this post irrelevant. However, it may still be of use…