Chair Presence Detection using Ultrasonic Distance Sensor

This post may contain affiliate links. Please read my disclaimer for more info.

One of the first things I usually do in the morning is sit down at my desk in my office and work on this blog or catch up on the news. To add some automation to my setup I wanted to detect if I was at my desk so I could properly light the room. Using an Ultrasonic Distance Sensor with a NodeMCU, I used distance to determine if someone was sitting at the desk or not.

I’m just using this today to trigger lighting in the room. You could use this to start up your PC using Wake on LAN or anything else to automate your routine. Think of this setup as a basic form of room based presence detection. I am the main person who uses the office in my household and I’m always at my desk when I’m working. This idea could be extended to cover multiple desks within a room.

Hardware

The main hardware component here is the Ultrasonic Distance Sensor. The sensor uses two pins to send an ultrasonic sound wave to measure distance. The sensor will be mounted to the underside of the desk. When someone is sitting at the desk, their legs and body will make the distance read by the sensor shorter than if no one is in the chair. For this project, I’m not looking for an accurate measurement of how far someone is from the sensor. Instead, I’m just trying to determine if someone is in the chair or not.

An Ultrasonic Distance Sensor is perfect for this task, since it can measure from 2cm to 500cm and just requires two pins from a microcontroller. I was already using a NodeMCU in my office to measure temperature for my automated ceiling fan, so I’ll just add the distance sensor to my setup.

The circuit for the distance sensor is pretty straightforward. You need 5V and GND to power the sensor and two GPIOs for the “trigger” and “echo” pins.

Ultrasonic Distance Sensor Schematic

After soldering it up on a protoboard and putting it in a case, I mounted it underside my desk pointed toward the chair.

Soldered Board

In Box

Mounted Under Desk

Angled Under Desk

 

Software

esphome

On the software side, I’ll be using esphome again to generate the firmware for the device. The esphome project has some instructions for installation in their documentation if you’ve never used it before. I really like the direction of esphome and how easy it makes to create robust IoT applications. The Ultrasonic Distance Sensor component of esphome allows you to easily configure the sensor.

For my setup, I used the following configuration file.

A few interesting pieces of this configuration:

  • The Ultrasonic Sensor is pretty straightforward to set up. I’m using pins 4 and 5 from the NodeMCU and reading the sensor every 100ms.
  • To cut down on the amount of data transmitted, I’m using the delta filter to only send the data when it changes by 0.10.
  • Like I mentioned earlier, this also has a DHT22 to provide temperature data to automate my ceiling fan.

Home Assistant

On the Home Assistant side, you first need to enable the ESPHome integration so that Home Assistant knows about your device. Next, I wanted to create a template sensor that tracks if someone is sitting in the chair or not. After sitting in various positions, I found that if the distance measured is less than 0.65mthan there likely was someone sitting in the chair. Using some basic templating, these states are visible in Home Assistant.

This sensor compares the value of the distance sensor to 0.65. When the value is less than 0.65 than there is likely someone sitting in the chair, so the sensor state is set to Occupied else it is set to Unoccupied. In the Home Assistant user interface, you get a new sensor showing the value.

Home Assistant UI

Finally, we can write some automations based on this. Some basic ones to get started will be turning on the lights whenever the chair is occupied, and turning off the lights after the chair has been unoccupied for 5 minutes. I’m really the only one that uses the office, so this makes sense for my setup, but you may need to add some additional logic if you have multiple desks.

To turn on the lights we just trigger on the state change and call the light.turn_on service.

To turn off the lights, we trigger when the sensor has been unoccupied for 5 minutes straight. That way, if you go to the bathroom or get something to eat, the lights aren’t off when you get back.

Next Steps

Thanks for reading my tutorial on the Ultrasonic Distance Sensor. This was a pretty easy project to get up and running and overall is fairly accurate. To improve my setup in the future I could add a couple more distance sensors under my desk to reduce missed readings if I’m sitting off center. I’d also like to add more sensors to the room so that the lights could come on before I get to my desk. Finally, I plan on using the Wake on LAN Component of Home Assistant to automatically boot up my computer.

Make sure to follow Self Hosted Home on social media and signup for my mailing list to get the latest articles. Let me know in the comments other uses you’ve found for these cheap sensors.