$5 Can Get You a Smart Bed

Build a smart bed occupancy sensor, using parts you probably have

Qizhen Li
7 min readApr 23, 2020
A cozy bed with a dim reading light
Photo by Jp Valery on Unsplash

Many of us use motion detectors to turn on and off lights in our rooms, but one of the biggest problems home automation geeks like me face is the lights turn off when you are not moving. This is when, as I like to call it, “are you still here” devices come into play. These are sensors that can make sure you are still in the room, such as…

The TV sensor knows that we are watching TV and keeps a dim light on, while the chair sensor knows I’m sitting at my desk and keeps the room lights on. There is only one more thing I haven’t solved yet, the bed. Finally, today, after months of thinking and researching, I have found a perfect way of monitoring bed occupancy…

What you need…

This sensor will communicate using MQTT, so you’ll need an MQTT broker and a smart home hub (e.g. Home Assistant, OpenHAB, Hubitat) that can receive MQTT messages.

Note: If you are using Home Assistant this sensor can integrate natively using the ESPHome API, you DO NOT NEED MQTT.

  • An MQTT broker (I recommend Mosquito, or you can use a cloud broker)
  • A smart home hub with MQTT capabilities (I highly recommend Home Assistant)
  • An ESP32 NodeMCU (ESP8266 will not work for this, because it does not support capacitive sensing with ESPHome)
  • A micro USB cable (to power the NodeMCU)
  • Some wires… Doorbell wire or any (not super hard) electrical wire should work fine.
  • Jumper wires.
  • Tin/aluminum foil.
  • A4 or other sized paper.

Optional (but recommended): Sheet protectors.

The total cost, assuming you are already running the software, is around $5 depending on which platform you plan to purchase the NodeMCU.

The ESP32 NodeMCU
The ESP32 NodeMCU

Let’s make it…

The big idea for this sensor is that we make a capacitive pressure mat that changes capacitance based on the pressure applied. We monitor the change in capacitance using the ESP32 and in turn, can tell how much pressure is applied.

Step 1

We will start by making the pressure mat… Cut a piece of tin/aluminum foil around the same size as the paper you are using.

Aluminum foil cut roughly to the same size as the paper
Aluminum foil cut roughly to the same size as the paper

Step 2

Cut the left and right sides of the aluminum foil to be around 1 cm/0.3 inch narrower than the paper on each side. Tape the top side (no need to cut it) to also around 1 cm/ 0.3 inch shorter than the top of the paper. Then strip a wire and tape it to the bottom side of the aluminum foil.

The sides are cut and a wire is stripped and taped to the bottom
The sides are cut, and a wire is stripped and taped to the bottom

Step 3

Fold the bottom side of the aluminum foil, with the wire taped inside, so it is 1 cm/0.3 inch shorter than the bottom side of the paper. Tape all the sides of the aluminum foil to the taper.

Flip the paper around and repeat steps 1–3.

All the sides of the aluminum foil are taped to the paper
All the sides are taped to the paper

Step 4

Now you should have aluminum foil taped to both sides of your paper.

Voila. You have made your first DIY capacitive pressure mat! Optionally you can put it in a sheet protector for additional durability.

Now… you just have to make some more depending on the size of your bed. I would recommend 4 for a queen-size bed and 6 for a king-size bed.

Four pressure mats placed inside a sheet protector
I made 4 for my queen size bed

Quick note

How does this sensor even work you ask? The two pieces of aluminum foil with the paper in between form a basic capacitor. By no means am I an expert on capacitors, but a capacitor works as two conductive plates (in this case, the two pieces of aluminum foil) are separated by a dielectric/insulant (in this case, the paper). Charges cannot pass through the dielectric, hence positive charges accumulate on one plate while negative charges accumulate on the other.

A computer motherboard
Capacitors are often used in motherboards

The distance between the two plates affects the capacitance, which is how this sensor works. When there is a large distance between the plates, even a single charge on a plate would repel further charges from entering the plate. As the distance between plates decreases, they can hold more charges due to attraction from the other oppositely charged plate. That is why sitting on the bed (decreasing the distance between the two aluminum foils/plates) changes the capacitance.

Okay, now back to making the bed sensor…

Step 5

We are will now connect the mats to the ESP32… Take 1 wire from each pressure mat and solder them each in a pair to a jumper wire. For example, if you have 4 pressure mats, you would solder 2 wires from 2 pressure mats to a jumper wire and the other 2 wires from the other 2 pressure mats to another jumper wire.

Two pairs of wires, each pair connected to a jumper wire
This is how you should connect them

Step 6

Now take the other wire from each mat and solder them individually to a jumper wire.

Four wires, each connected individually to a jumper wire
This is how you should connect them

Step 7

We are almost done… Place your pressure mats under your mattress, each in a different position to get the best coverage.

Pressure mats at each corner  of my bed
I placed my pressure mats at each corner of the bed

Step 8

Connect the pairs of wires to the GND pins on the ESP32 and the individual wires to any of the following pins:

  • GPIO0
  • GPIO2
  • GPIO4
  • GPIO12
  • GPIO13
  • GPIO14
  • GPIO15
  • GPIO27
  • GPIO32
  • GPIO33
ESP32 placed under my bed with wires connected to it
The ESP is placed under my bed

Step 9

Install ESPHome, the software that the ESP32will run. It is supported on multiple platforms and the installation steps are documented on their website.

The ESPHome dashboard
The ESPHome dashboard

Step 10

Upload this code to the ESP32, using ESPHome. The uploading steps can also be found on their website.

# ESP32 NodeMCU
esphome:
name: bed_sensor
platform: ESP32
board: nodemcu-32s
# Change this to your Wi-Fi config
wifi:
ssid: your_wifi_ssid
password: your_wifi_password
ap:
ssid: “Bed Sensor Fallback Hotspot”
# Set a password in case Wi-Fi fails
password: “change_this_please!”

# Fallback portal
captive_portal:
# Logging
logger:
# Uncomment this if you use Home Assistant
#api:
# password: “change_this_please!”
# Fill your MQTT information
# Delete this if you're using Home Assistant

mqtt:
broker: 10.0.0.2
username: livingroom
password: MyMQTTPassword
# Over-The-Air updates
ota:
password: “change_this_please!”
# Setting up the pressure mats
esp32_touch:
# Delete this after you are done with the setup process
setup_mode: true
# All the pressure mats
# Change the pins to the pins you have used

binary_sensor:
- platform: esp32_touch
name: "Top Right"
pin: GPIO14
# See step 11
threshold: 5
id: top_right
- platform: esp32_touch
name: "Bottom Left"
pin: GPIO33
# See step 11
threshold: 4
id: bottom_left
- platform: esp32_touch
name: "Top Left"
pin: GPIO15
# See step 11
threshold: 4
id: top_left
- platform: esp32_touch
name: "Bottom Right"
pin: GPIO27
# See step 11
threshold: 4
id: bottom_right
# The main sensor that is created using the all the zones
- platform: template
name: "Bed Sensor"
lambda: |-
if (id(top_right).state ||
id(top_left).state ||
id(bottom_right).state ||
id(bottom_left).state) {
return true;
} else {
return false;
}
Compile and upload of the ESPHome code
Compile and upload of the ESPHome code

Step 11

Once you have uploaded the code, go to SHOW LOGS and you should see many lines with numbers filling up the screen. Sit on your bed and the numbers will decrease, stand up and the numbers should increase back. Change the threshold value in the code to whatever number you want the sensor to trigger. After you are done, delete setup_mode: true from the code and re-upload.

The setup logs in ESPHome
The setup logs in ESPHome

All Done!

We are finally done! Configure the sensor with MQTT (or the ESPHome API if you use Home Assistant).

Enjoy!

The different zones and entities in Home Assistant
The different zones and entities in Home Assistant

--

--

Qizhen Li

There was a 1 out of 7,819,953,083 chance we would have ever crossed paths, yet here we are!