Sunday, March 23, 2025

Bed Weight Sensor v3

One of my favorite sensors I'm using with Home Assistant is a home made bed weight sensor.  This lets me know when someone is in bed to do things like dim lights, and stop automations that would announce things.  I also have a DIY burglar alarm set up to detect when a door opens while everyone is in bed.  If you search Amazon for "weight sensor" a lot of listings like this one show up.  That listing includes the strain gauges themselves and also the HX711 board you use to combine 4 of those into a single reading.  If you search around the internet you'll see a lot of tutorials for how to wire those up to measure with an ESP32 (I'm using ESPHome to get that into Home Assistant).  People commonly use 4 gauges, in each of the 4 corners of a platform, but using 2 or 1 is also possible and you can find examples of those as well.

weight gauges and typical wiring

This picture from the above Amazon listing shows how 4 gauges are commonly wired together in what is called a "Wheatstone bridge".

The problems with Bed Weight Sensor v1 and v2

When I first set these up for my bed, I just did a test by putting 2 gauges between the frame and my foam mattress.  That worked ok; it was enough to sense "someone is in bed", but it wasn't very accurate beyond that.

Version 2 used 4 gauges in a row across the bed, with little wooden squares cut out and placed under each, which were then all attached to the frame.  The sensor measured the total weight as a whole, rather than trying to measure the left side of the bed vs the right side, which is another common solution I've seen.

The v2 sensor worked pretty well; it was very accurate at knowing if the bed was empty or not, and was pretty good at knowing how many people were in bed.  However, it didn't do a great job at knowing who was in the bed.  I didn't have many automations that relied on knowing which person was in bed, but there were some.

The sensor also drifted over time, and every few months I had to recalibrate it, which wasn't too much work, but was annoying to have to do.  Frankly, it was amazing it worked at all, considering the gauges were just under the foam mattress, with plenty of the weight on the mattress able to spread out past the gauges and go directly to the frame.

The dream that became Bed Weight Sensor v3

My dream was to build a sensor which put the gauges between the legs of the bed and the floor, so that 100% of the weight of the bed was going through them, and so both sides of the gauge were on a hard surface.  The one problem I had preventing me from doing that was that my bed frame has 12 legs.  While the gauges are cheap enough, I could never find an example for how to wire 12 of them together to get one measurement.  I even asked this question and the advice I got was just to set up 3 sensors, each with 4 gauges, and then just sum their weights in software.  That seemed silly and wasteful to me, but the main problem I had with it was that I had no way to calibrate each of those 3 sensors independently.

To calibrate one of these sensors, you just put two known weights on it, and record the reading each gives you.  Generally you use 0 as one weight, and then some other known weight for the other one, which would be "me" in this case.  So, with 3 of these, one for the left side, one for the right side, and one along the middle, it would be easy enough to get a 0 reading, but how was I supposed to know how much weight was on just the left hand side when I got into bed?  My best hope would be to try to lay across the bed and assume my weight would be evenly spread, but that just didn't seem like a path to success.

Instead, I was sure there was a way to just wire up all 12 gauges to give a single unified weight measurement.  I knew these gauges were just varying their resistance when a strain was applied, and the HX711 was just a high precision ADC that would measure the voltage across the combined resistors and convert that into a digital reading.  So, it seemed pretty clear that the resistors could be wired up to allow the HX711 to read all 12 the same as if they were a group of 4.  Certainly, the specific resistance for a given weight would change, but the calibration step would account for that.

Testing

4 weight gauges set up to measure the change in resistance when weights are put on them

I began just by confirming how these gauges worked with 4 arranged in the common Wheatstone bridge.  I used this StackExchange answer as a reference quite a bit while working on this, and the below image from it shows how one gauge works.

datasheet for a weight gauge

There are 3 wires coming out of each gauge, and there are two resistors inside the gauge.  One resistor is between the white and red wires and its resistance goes up when a weight is applied down on the gauge.  The other resistor is between the red and the black wire, and its resistance goes down as weight is applied down on the gauge.  This means the overall resistance across the entire gauge, from the white wire to the black wire is always the same (2 kΩ for these).  That's why these are often arranged in a Wheatstone bridge, using 4 gauges, each with their black and white wires connected together to form a square, with a voltage applied across 2 of the red wires and then the resulting voltage measured across the other two tells us what the overall reading is.  This image (also from that StackExchange post) shows an example.

example of a Wheatstone bridge with 4 gauges and a weight on 1 of them

This image shows what happens when gauge 4 (G4), on the right hand side has some weight applied to it, which causes one half of it to increase in resistance slightly and the other side of it to decrease slightly.  The other 3 gauges have no weight on them.  Without knowing much about electronics you can trace the path from the positive voltage at G1Red and see that it has less resistance to go through to get to the left hand side (labeled Sig+), and more resistance to get to the right hand side (Sig-).  This means we'd expect the voltage at Sig+ to go up relative to Sig-.  Then tracing up from the bottom, where the negative voltage is applied, we see that the path to Sig- is less resistance, so we'd expect the voltage at that spot to drop further relative to Sig+.  That is how we use a Wheatstone bridge to measure the change in these 4 gauges.

How can we do that for 12 gauges though?  Well if you connect two resistors in parallel you just get a new combined resistor with a lower overall resistance.  There's no reason simply connecting two of these Wheatstone bridges in parallel wouldn't work, you might just lose out on some precision.  I tested this by buying a bunch of strain gauges and wiring up two independent Wheatstone bridges, then connecting them in parallel (so the same points on the red wires from each bridge connected together), and taking a bunch of measurements with different weights.

needlessly complicated data collection

I paid special attention to making sure that the measured voltage was the same however the weight was spread between the two bridges.

the data in question

two Wheatstone bridges, labeled A and E with various weights and the combined voltage

Plotting that data showed exactly what I wanted to see, the voltage scaled linearlly as the weight increased.  It didn't matter which side the weight was applied to, if it was evenly balanced or all on one side or the other, the output voltage was the same.

The next thing to solve for was that I needed some sort of stands for the gauges.  The gauges have rivets sticking out of the bottom of them and the center portion needs to be able to flex slightly, so you have to mount them supported along the outer edge, with the middle unsupported.  This was another thing I didn't consider while making v2 of the bed sensor, and another reason I'm surprised it worked as well as it did.

These gauges are common, so it wasn't hard to find a 3d print model for them.  I used this one, which I slightly modified to remove the screw holes since I wouldn't be using them.  I got a bunch of those printed at my library, and spray painted them and the gauges black so they'd blend in better with my bed frame.

weight gauges in 3d printed trays

Once I had those, I wired up the full system for the first time.  All 12 gauges, on the plastic stands, wired up in 3 parallel Wheatstone bridges, all going to a single HX711 which was connected to an ESP32 running ESPHome configured to measure weight from the HX711.  Each of the 3 bridges had their 4 gauges under the corners of a scrap piece of plywood.  Then I essentially had 3 plywood scales all wired up together reporting a single weight across all of them.

testing the full setup

This worked very well, and it was immediately clear the overall idea would work fine.  The above picture shows 55 lbs total on the system, with 50 of those spread evenly between two Wheatstone bridges and the remaining 5 lbs on the third bridge.  I moved that weight around in different arrangements, and if it was evenly spread or focused on different platforms I still got accurate measurements, generally to within a 1/10th of a pound.  I also left it running overnight and came back the next day to see very little variation.

Putting the bed in Bed Weight Sensor

With the testing done it was now time to install this sensor on my bed.  I planned on making this install a bit more robust and more hidden than v2.  My bed frame has 3 groups of 4 legs each, with one set along the left side, one set along the right side, and one set up the middle.  Thinking about it as rows instead of columns, there's one row along the foot of the bed, two in the middle, and one up at the head.

soldering in the bedroom

This meant each Wheatstone bridge of 4 gauges would have G1 on the leg by the foot, G2 in the middle, G3 above that, and finally G4 up near the head.  Each gauge connected to its neighboring gauge, using either its black or white wire.  If the black wire went down, the white wire went up.  With the exception of G1 and G4, which "wrapped" around and went along the length of the bed from the foot to the head.  That was how each bridge was wired independently, and that gave a red wire coming up each leg.  Then all the similar red wires were connected together, along the rows.  So the red wire from G1 on the left was connect to the red wire from G1 in the middle and G1 on the right.  Each group of 3 red wires was connected to a fourth wire which connected it to a single spot on the HX711.

I don't have any close up pics of the wiring.  You might think it would be helpful, but it's just a confusing mess of wires running all over the bed frame.

I started this process around 4pm, hoping to have it done by dinner time.

At this point, let's stop and do some math.  Each bridge had 4 connections between the black and white wires.  However, only one of those was short enough that the wires from the gauge could be directly connected to each other.  The other 3 needed an extension between them, which doubled the number of connections, giving 7 connections.  For the red wires, these generally also needed to be extended to reach where they all landed in the center of the bed; only the two legs in the center didn't need that.  So this means there were (3 * 7) + 10 = 31 wire to wire solders that needed to be done.  And that only got me a mess of 12 wires in the center of the bed.

Another thing you might want to notice about my bed frame is that it's a dense mesh of bars such that you can't really walk between them, but also can't really walk on them.  This led to me constantly carefully stepping through the bars to get to the center of the frame, absolutely sure I would fall at some point and break my leg.  In retrospect, sliding around under the bed, on my back, might have been a better strategy, but that didn't occur to me until I was done.

The wires on the gauges were very tiny 28 gauge stranded wire, which made them a nightmare to work with.  Once I had all 12 wires at the center of the bed, all I had to do was land them all on some prototype board.  After all the wire to wire soldering I figured that just doing some through hole soldering would be simple, and it probably would have, it it wasn't being done in the center of the bed frame, bending over to butt level.  This is the part that for sure would have been easier to do from underneath.

Anyway, I finally finished up, and had managed to not forget a single heat shrink tube, either on the connections or the legs.  I did a pretty through testing of all the connections with a multimeter, measuring both connectivity, and the resistance between various points.  At this point I had a pretty ingrained sense for what resistance value I would expect from any given two points in the network of wires.  Much to my surprise, everything checked out.  So I powered up the ESP32 and started monitoring the measurements it was reporting.  After a basic calibration, I was delighted to see it was very accurate.  I could put the roll of masking tape on the frame and easily detect that.  I sat on different points along the frame and got very consistent weights for me.

I did have a little bit of cleaning up to do, but I purposely left things not fully done in case I had to make repairs.  I left the heat shrink unshrunk, and didn't tape up the wires as much as I had intended.  I also left the v2 sensor on the bed for now, as a fall back and to collect some parallel data.

the mostly finished v3 sensor with the v2 also still attached with the beige masking tape
I proudly stood and admired a job well done.  It was 2am, my wife was asleep in the guest room, and aside from a very quick cheesesteak break, I had worked continuously on this for 10 hours.

Also visible in the above picture is the old v2 sensor still attached.  All the beige masking tape was either temporary or related to the v2 sensor.

a closeup of a weight gauge, in its stand, with unshrunk heat tubing hiding the wires

This picture shows the nearly finished product, just without the tube shrunk or a bit more black masking tape I used to keep it tight to the leg.  With the overhang of the mattress, the gauges aren't visible at all.  One thing you can see is some electrical tape I used to hold the gauge to the leg.  I expected the gauges to slide on the floor and had some rubber I was prepared to put under them, but actually the leg slipping off them was the bigger problem.  I tried a few things, including hot glue, but electrical tape worked very well, and has held up for months at this point.

Where's the data?

I know you all came here for data, so without further ado.

comparison of v2 and v3 weight sensor data over about a day
This graph shows a comparison of the two sensors over the same day.  Yellow is v2 and blue is v3.  You can see how the yellow graph jumps all over the place, basically anytime someone rolls over.  While it is "clear" there is a pattern of one person, then both, then the other person, you can imagine how hard it would be to attempt to draw a horizontal line where we can say "below this weight is person 1, and above is person 2".  And in fact, the highest point of the yellow graph on the left (my wife) is higher than the lowest point on the right (me).  Now compare that to the blue line representing v3 and it's a stark difference.  The blue line is not perfectly flat, but the slight variation you can see amounts to a pound or two of difference.  The larger square wave pattern you can see (especially visible on the far right end, but also throughout the first phase on the left and in other spots) is about 10 lbs of variation, and is our cat coming and going.  Note the v2 sensor doesn't even acknowledge that.

closeup of v3 weight sensor data over a few hours showing the variation

Here is shorter period of time, from a different day, about 2 months later with no recalibration since the initial set up.  It's a very un-cherry picked period, there are better looking sections, but this is about the worst case I see.  There is a bit of variation there, but accounting for the cat, it's within a few pounds of the same measurement.  After what I had before, I am very happy with this setup.  I would love it if the sensor were accurate enough to monitor our weights over time, but this seem unlikely, even with much more precise gauges, just due to stuff like the headboard touching the wall, or the pillows, blankets, or clothing changing, sometimes during the night.

Either way, I consider this a smashing success, and removed the v2 sensor and taped up the lose wiring after about a week.  At this point the sensor has been working for over 2 months and seems as accurate as the first day.