Introduction: Sonoff R2 Enclosure & ESPHome

About: To learn is to live!

The Sonoff Basic R2 is a great switch for home automation. In this Instructable I will show you how to configure it for use with ESPHome and then print a custom case so that it looks a little nicer in your living/bedroom environments.

Besause is is not possible to upload FreeCAD files to the Instructable website, I have create a DropBox share here. This is only necessary if you wish to modify my design. Just one disclaimer, I am not an expert at FreeCAD and so I am sure there are much better ways that this design could have been created :)



Please note, none of these links are affiliate links. I simply share them for convienence.


  • HomeAssistant (The Instructable assumes that you already have HomeAssistant up and running. If you have not, here is a tutorial.)
  • ESPHome addon installed in HomeAssistant. The above HomeAssistant tutorial also included setting up ESPHome.
  • FreeCAD (If you wish to modify the drawings)

Step 1: Prepairing the Snoff

While not necessarily essential, add the header makes things much easier and does not get in the way of the casing so it is worth doing. See attached photo for placement.

Using the the dupont jumbers, connect the Sonoff to the FTDI as follows:

  • VCC - VCC
  • GND - GND
  • TX - RX
  • RX - TX

Plug the FTDI into an USB port. This should result in another COM port appearing. While there are a number of ways to check what the new port number is, I find the easiest and quickest is, before plugging the FTDI in, to simply run the Arduino IDE and check what ports appear. Then plug the FTDI in and go and see what new port has appeared.

If no new port shows up then you will need to google as to how to install your FTDI driver.

From there, go to the ESPHome Dashboard in HomeAssistant and add a new device. The below is the code that I used:

  name: smartplug-rumpus

  board: sonoff_basic

# Enable logging

# Enable Home Assistant API
    key: !secret ekey-sp-rumpus

  password: !secret ota_password-sp-rumpus

  ssid: !secret wifi_ssid
  password: !secret wifi_password

# Device Specific Config
  - platform: gpio
    id: push_button
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    internal: true
      # Prevents unintended LED lit states.
          - switch.is_off: relay
          - switch.turn_on: blue_led
          - switch.turn_on: relay
          - switch.turn_off: relay

  # The relay switches on the red side of the LED when active.
  - platform: gpio
    name: "smartplug-rumpus"
    pin: GPIO12
    id: relay
          - switch.is_on: blue_led
          - switch.turn_off: blue_led
  # With this we can control the blue side of the LED.
  - platform: gpio
    id: blue_led
      number: GPIO13
      inverted: True

The first install will need to be done via USB. This can usually be done via the Home Assistant, but needs to be using Chrome or Edge. In my case I had issues trying to use an old (already programmed) device so was forced to manually download the firmware and then flash it using the ESPHome Flasher. When doing so, I needed to hold the button down on the Sonoff until the flashing started. Going forward, once complete, I could update the Sonoff wireless.

The Sonoff can now be used with your HomeAssistant to automate switching a device on or off.

Step 2: Printing the Enclosure

Step 3: Putting It All Together

Remove the Sonoff from it's original white case that it comes in and install it in the 3D printed case.

The case fits together very tightly, with not much room to attach the electrical cables. These will need to be cut as short as possible and then pushed firmly into the case (only thin cable ties will fit). If the cable is too think, you may also need to drill the hole through which the cables pass, out a little.

As such, once wired up and with everything in place, small cable ties need to be tightly attached (as in the photo) to help prevent the cord from being pulled out of the socket.

Two things to be careful about:

  1. By referring to the original casing, make sure that you wire the power to the input pins and the light to the output pins. As the PCB does not appear to be labelled, you will need to double check by referring to how the PCB sat in the original casing.
  2. Be careful not to damage the push button when installing the board i.e. ensure that it lines up with the hole before you pushing everything together. In my haste, I did not do this and managed to break part of if off of the board.

Remember that you will be working with high voltages so ensure that they cables are both secure and not going to be cut by the case (or wear through at the entrance holes).

The base plate can then be screwed in place. I used 8mm M3 screws, with a flat countersunk head, but these would also have worked.

Step 4: Using the Sonoff

I use these Sonoff Switches in the following situations:

  • Depending of the time of day and presence of people, automatically toggle lamps on or off in our main living areas.
  • Linked with a wake-up alarm time, switching my beside light on.
  • Because our HRV is not a smart device, I use one of these to turn it off when our air conditioners turn on (so that the HRV is not pumping hot air into our rooms while the air conditioners are trying to cool them).