How To Guides

Building a bed occupancy sensor for Home Assistant

Building a bed occupancy sensor for Home Assistant

Recently after automating many of my lights with motion sensors (guide here) I found myself wanting a way to automatically switch everything off at night. The question was how to properly determine when my partner and I had went to bed for the night?

I started researching bed occupancy sensors and came across a great post from Zack at Self Hosted Home where he used an ESP8266, 4 load cells and a HX711 to build what is essentially a bed size weight scale to determine if someone was in bed or not. I decided I needed to build the bed occupancy sensor and integrate it with Home Assistant.

However when I set about building this, I ran into a few issues that required my setup to be different:

  1. How to connect 3 wire load cells, there are a lot of cheap 3 wire sensors out there but much of the documentation is for a proper 4 wire setup. Although to be clear, the post above does use a 3 wire setup also.
  2. Load Cell drift – once I had the setup working, I found my values would slowly start to drift over the course of a day and I wanted a way to overcome that.
  3. Using an exact weight rather than raw scale values – Zack uses the raw scale values in his setup rather than a calculated weight. This works great for his setup but my sport means that my weight can intentionally fluctuate quite a fair bit. For this reason I wanted to use a calculated weight in KG rather than the raw value, to make tuning my setup easier.

I also 3D printed some holders for the legs of my bed which are a little bigger than normal, these hold the sensors in place and stop the bed sliding around on them which I found was happening and causing issues with my readouts!

Let’s get into building a bed occupancy sensor for use with Home Assistant, or any home automation platform!

Parts list

There are just 3 essential parts for this build:

  • HX711 Amplifier
  • Load Cells – Note a HX711 is included in this link, I tried this one but the SparkFun one is of better quality and less fluctuations. Your mileage may vary!
  • Wemos D1 Mini or other similar board, needs 5V output.

Optionally you may want to print some holders for the cells if you have a 3D printer available and depending on your setup. There are many designs available on Thingiverse.



Here is a video guide if you prefer:

Hardware setup

These load cells are essentially a series of resistors whose value changes when flexed, the resulting change in voltage can then be measured and transformed into scale readings.

We are going to be connecting the load cells in a full wheatstone bridge in this guide which uses 4 load cells, however it is possible to use 2. The wiring for this can be a little confusing on first glance.

Firstly identify the following connections on your HX711 board, E+, E-, A+, A- (sometimes A is called S so interchange these).

The easiest way to start out is to lay all 4 load cells out into a square shape with one in each corner. You will have an Upper Left, Upper Right, Lower Left and Lower Right. In a very basic sense, the white wires join horizontally, the black wires join vertically and the red wires go to the HX711.

Upper left should go to E+, lower right should go to E-, upper right should go to A+, lower left should go to A-.

Once you have that, we need to wire the HX711 to the our Wemos D1 Mini (or whichever board you are using). Wire the 5v output of your board to the VCC pin on the HX711, do the same with the ground pins. Then we take the pin labelled D4 on our board and wire it to the DAT pin of the HX711. Finally, RX pin of our board goes to CLK of the HX711. We are using pins 2 and 3 in our code which corresponds to D4 and RX on our ESP board (GPIO 2 & 3).

It should look like this:


We are going to be using the HX711 library, found here on Github.

Go ahead and fire up Arduino IDE and set your board to the Wemos D1 Mini (or whichever you are using) – set your board speed to 74880 for the D1 mini otherwise you may get some weird characters which seems to be a known issue.

Go to Sketch > Include Library > Manage Libraries and search for HX711 and install it.

Next download both of the sketches from my Github for this project, located here. These use MQTT so that our bed occupancy sensor can speak to Home Assistant.

Test run

At this point I’d highly recommend testing everything is working before going to any effort of mounting.

Go ahead and upload the “Scale Calibration” sketch to your board and open the serial monitor at 74880. After a few seconds, you should see the monitor start to output something that resembles the following:

Initializing scale calibration.
Please remove all weight from scale.
Place known weights on scale one by one.
Reading: 0.00kg
Calibration Factor: 2400
Reading: 0.00kg
Calibration Factor: 2400
Reading: 0.01kg
Calibration Factor: 2400
Reading: 0.00kg
Calibration Factor: 2400
Reading 0.00kg
Calibration Factor: 2400

If you are getting readings from the monitor then this should mean your setup is working. If you are not, check your wiring as per above and check the troubleshooting section below.

Put some pressure on the inner part of one of the load cells with your finger, you should then start to see values change:

Reading: 0.01kg
Calibration Factor: 2400
Reading: 0.00kg
Calibration Factor: 2400
Reading 0.00kg
Calibration Factor: 2400
Reading: 4.58kg
Calibration Factor: 2400
Reading: 4.02kg
Calibration Factor: 2400
Reading 4.27kg
Calibration Factor: 2400

Don’t worry about the actual values, we will calibrate properly later.


Assuming you got readings as above, you should be safe to mount the hardware in its final position.

This part is obviously very person dependant, but I’ll show you how I mounted mine.

Originally I had tried a number of different 3D printed holders for the load cells, they all worked but I found due to the nature of the legs of the bed, that the bed would very easily slide around on top of them meaning that the values would change daily which was very frustrating when trying to get a consistent setup!

The other issue was when manually placing the sensors under the bed, it was hard to get them in the same position under the bed at each corner, so that one sensor did not have more or less weight on it than another corner.

So I spent 20 minutes 3D modelling some casters/holders for the legs of my bed, so that the sensors would always be in the same position and the bed was unable to move.

Here was what I came up with:

This design allows the legs of the bed to sit perfectly inside and allows for zero movement of the load cell, meaning each corner is weighted identically.

Once mounted, we can move onto calibrating the scale for your setup.


To get the load cells calibrated for your setup, ensure there is no additional weight on the scales apart from what would normally be on there at a “zero weight” – duvet, pillows etc.

We can fire up the calibration sketch again and open the monitor at 74880.

  1. Allow the monitor to print the zero weight a couple of times, again this should be 0 kg or very close to it. Expect a little fluctuation, this is normal.
  2. Once this has settled, place a known weight on the scale, 5kg is a good weight to use. Observe the output on the monitor and see how close (or far!) it is from being correct.
  3. Adjust the “calibration_factor” variable in the code and re-upload – start by adjusting the factor with larger increments of say 500 till you start to get closer to the actual weight, then maybe 100 points then 10 points.
  4. Re-upload the code – don’t forget to take your known weight off each time. Keep repeating until you have an accurate reading.

Once you are happy with the reading, take note of your calibration factor, we will need it for the next section!

Bed Sensor

We can now take a look at the final sketch, the Bed Sensor, which is the one that will integrate our bed occupancy setup with Home Assistant.

Fire up the sketch, and open the change your view to the config.h file:

We need to change a few simple variables here:

  • SSID and Password – change these to your Wifi setup.
  • MQTT_Server – change this to the IP or hostname of your MQTT server.
  • mqtt_username and mqtt_password – change these if you have authentication enabled.
  • Calibration factor – change this to the value you obtained from the calibration steps in the last section.

You can optionally adjust the MQTT topics if you wish. These topics are for:

  • State topic: this is where the kg value will be published to, which will be picked up with Home Assistant.
  • State raw topic: I am also publishing the raw scale values to Home Assistant, these can be pretty useful for checking drift.
  • Availability topic: When the scale starts up, it will publish a message to this topic so Home Assistant knows it is online.
  • Tare Topic: The scale will subscribe to this topic, when it receives a message to this topic, it will tare (reset) the scale. Very useful for combating drift.

Upload the sketch to your board and open the monitor at 74880 speed. You should see it connect to your Wifi, connect to MQTT then it will tare the scale. Then it will start to output the weight and raw value every 3 seconds.

Once again, make sure that no additional weight is on the bed when powering on.

Place a weight on the bed again as a final test and make sure it is reading correctly.

Now we can finish integrating our bed occupancy setup in Home Assistant!

Home Assistant

Open your configuration.yaml file and add a new entry:

  - platform: mqtt
    name: "Bed"
    state_topic: "home/bedroom/bed"
    unit_of_measurement: "kg"
    availability_topic: "home/bedroom/bed/available"
  - platform: mqtt
    name: "Bed Raw"
    state_topic: "home/bedroom/bed/raw"
    unit_of_measurement: "raw"
    availability_topic: "home/bedroom/bed/available"

Reload Home Assistant and go into Dev Tools > States and see if you are getting information through:

Head back to your configuration file and we are going to make some template sensors based on these values. I am using Zack’s code here as it works well.

First we add some template binary sensors:

  - platform: template
        friendly_name: "Lewis in Bed"
        value_template: >
          {{ states('sensor.bed')|float >= 70 }}
        friendly_name: "She who must be obeyed in Bed"
        value_template: >
          {{ states('sensor.bed')|float > 40
            and (states('sensor.bed')|float < 60
               or states('sensor.bed')|float >= 100 )}}

This does a couple of checks:

  • If the weight is greater than 70kg, Lewis is in bed.
  • If the weight is greater than 40kg, but less than 60kg, she who must be obeyed is in bed. Or if the weight is greater than 100kg, she is also in bed.

I found using a slight range works well.

Then we add another template sensor to calculate the number of people in bed, which is a great sensor to have when doing automations:

  - platform: template
        friendly_name: "Number of People in Bed"
        unit_of_measurement: 'people'
        value_template: >-
          {% if is_state('binary_sensor.lewis_in_bed', 'on') and is_state('binary_sensor.she_who_must_be_obeyed_in_bed', 'on') %}
          {% elif is_state('binary_sensor.lewis_in_bed', 'on') or is_state('binary_sensor.she_who_must_be_obeyed_in_bed', 'on') %}
          {% else %}
          {% endif %}

Restart home assistant and check your dev states again.

Time to give it a test and see if the results are as expected. It will probably take a little tweaking to get it correct for your setup.

Next steps

Remote taring

If you find your setup is working, but the perhaps the values are drifting over the course of a day, or the scale isn’t always going back to zero after getting up in the morning, you can use the remote tare function.

One method is to create an automation within Home Assistant which tares the bed occupancy sensor everyday at midday:

  alias: Remote tare
  - at: '12:00'
    platform: time
  - below: '1'
    condition: numeric_state
    entity_id: sensor.num_in_master_bed
  - data:
      payload: 'on'
      topic: home/bedroom/bed/tare
    service: mqtt.publish

This will reset the scale to zero everyday at midday, only if the number of people sensor is at 0.

Useful automations

A couple of really useful automations using this sensor are:

  • Ensuring that when we are both in bed, every light and media player is turned off, door locks are set and the house alarm is set to “armed home”.
  • I have lights that are automated through Home Assistant when motion is detected, I add a condition to these automations not to trigger these lights when we are both in bed, this is so that our dogs do not trigger lights all night.
  • During night hours, the bathroom light comes on at a very dim brightness if one of us exits the bed during the night. This is great for not being blinded when getting up to the bathroom during the night!


  • The vast majority of issues will be wiring related. If you are having issues I’d suggest double checking your wiring, then triple checking. Its very easy to make the slightest error which will cause the whole setup to not work correctly.
  • Ensure you are using 5v output, 3.3v will technically work, but it is not recommended and you won’t get great results if you don’t use 5v.
  • Ensure you are using the correct pins on your dev board. Do not make the mistake of thinking pin 2 and 3 in the code corresponds to D2 and D3 physically on the board. Look up the pinout and ensure you are using GPIO pins.
  • Check your voltages, if you have everything correct use a multimeter and measure volts across E+ and E- where you should get 4.3v. Then measure A+ and A- where you will get roughly half of that. If you are not, check your wiring again.
  • If you are getting readouts but they are in negative values, switch the E+ and E- wires around and that should correct the issue.
  • Solder and heat shrink your wires, they seem to be highly sensitive to interference so do everything you can to minimize this.

Let me know in the comments if you have any further issues or as always, what you want to see next!

Want to support me?

If you like the content I put out and would like to support me, you can do so using the options below.

I can't state enough how much any support is greatly appreciated and will go directly back into funding new/bigger projects and website upkeep.

Tagged , ,
  1. Puneit Singh

    This is one of the most concise and well written guide on this topic. Bed occupancy sensor has been on my mind of an year and your guide helped me make the purchase for the hardware required.
    I would make a tiny suggestion .. please link to the arduino code and also share your STL files. It will really help to make it one stop guide for the whole thing.

    If you have done other cool stuff, please share. Really enjoyed the flow of your guide. Thank you

  2. Lewis Barclay

    Thanks, glad you enjoyed it! I have linked the code, please find the code on Github. I didn’t share the STL files because it would only fit my bed.

    Please see my other how to guides, more coming soon!

  3. Morten Mortensen

    Very well written walk-through of your bed sensor project, thanks a ton for sharing it. πŸ™‚

    How does the 3D printed load-cell holders hold up to the pressure over time, I’ve noticed that there is a dimple on top of the load-cell that it used to concentrate / centre the load, does that not dig into the top cover of the 3D box you printed?

    Or maybe that’s not a concern and just me over-thinking it? πŸ™‚

    1. Lewis Barclay

      Thanks, glad you enjoyed it!

      They actually hold up very well! The dimple is actually flush with the rest of the casing, so that’s why I didn’t have concerns since the outer rim takes much of the strain. I hope that makes sense. the nature of this makes it hard to photograph!

  4. mike

    ” I found myself wanting a way to automatically switch everything off at night. ”
    Uhhh….. every try “Hey alexa, turn off lights.”

    1. Lewis Barclay

      A few issues with that approach:

      Not everything integrates with Alexa
      There is more to automation than just lights
      I only have one Alexa at the moment which is located downstairs, if I were to turn off all the lights, I can’t see to go upstairs

  5. fabian

    First of all: nice project and thank you for sharing! I was wandering how accurate the load cells can be when exceeding their max load. My bed has a wooden frame and a quite heavy mattress – I guess the empty weight would well exceed 100kg maybe even 150kg. Would the approach with 4x50kg load cells still be feasible or should I look for 100kg cells. After all I am not interested in measuring my weight but only in determining the occupancy state of the bed.

    1. Lewis Barclay

      Thank you!

      I had the same concerns and to be completely honest, I am unsure on that one! I personally think it will work fine as an occupancy sensor even if the weight is over 200kg so I think you will be fine. If you needed it to be 100% accurate I would not recommend it but since you don’t, I think you would be good to go!

  6. Rick

    Thanks for this very detailed guide !

    I have 0 experience with electronics and this motivated me to venture into my first diy electronics project !
    Got all the parts except for the HX711 board, which is a different version from the one you are using :
    https://c.76.my/Malaysia/load-[email protected]1.jpg

    Mine has DT and SCK instead of DAT and CLK, can you please help with how to wire this one to the Wemos D1 board ? I followed the scheme and replaced DAT and CLK directly with DT and SCK but it doesn’t work like that, so I guess it needs some changes .

    Totally lost here, spent a good 2hours soldering everything together and I managed to get to Wemos D1 sketch part but was not getting any readings .


    1. Lewis Barclay

      Hey Rick,

      That’s great to hear you got started with this!

      I’m pretty sure you have DT/DAT and SCK/CLK wired correctly, so you might want to check elsewhere. You can try and reverse DT and SCK (I have seen some boards where they are labeled incorrectly) so give that a try, you won’t break anything. Which pins have you wired to on the Wemos side?

      Also check you are getting voltage between E+ and E- and between A+ and A-, I think it should be around 5v and 3.3v respectively if I remember from the top of my head!

      Hope that helps a little.

  7. Rick

    Hi Lewis,

    Thanks for your fast reply !

    On the Wemos I have tried with both D4 / RX from your guide and D1 / D2 (changed the gpio in your sketch to match those) and in both cases I did not get any readings .

    Just tried checking voltages with my multimeter and I am getting a hard time registering any values but after a lot of fiddling around I managed to read 3.8V on E+ / E- (although many times it reads smaller values and most of the times I can’t get any readings at all) and on the A+ / A- the maximum voltage I was able to see was 1.2V so the problem might be there ?

    It’s strange though because my continuity test shows that all the solderings from the wires connecting the cells between each other are ok .

    Will give it a try with the DT / SCK change but now that I got these low voltage readings the problem is probably related to it .

    1. Lewis Barclay

      Hey Rick,

      OK sounds like you have made a little headway!

      Are you using the 5v supply from the Wemos I assume?

      Do you want to drop me an email (lewis”at”everythingsmarthome.co.uk, some photos of the setup would be great!) and I’ll do my best to help!


  8. Nick

    Any help you could provide would hugely appreciated. This could really help with my sleep disorder.

    How do I tell if a wemos has a 5V output? Could you provide a link to one you reccomend.

    Also, will it matter if the weight on my bed is not evenly dispersed? The bed is really heavy, has 5 points of contact, and the headboard is much heavier than the footboard.

    Also, do you have any recommendations for sensors that detect room presence. I need to detect what times I enter and leave several rooms in my house.

    Thanks in advance.

    1. Lewis Barclay

      Hey Nick,

      The Wemos D1 Mini is a great board for this application and has 5v, it’s what I used. You will find the link above!

      No it shouldn’t matter if the weight isn’t dispersed evenly as each sensor is taken into account individually.

      I haven’t tried them myself (yet!) but you could look into something like a thermal image sensor might work well!


  9. Lipown

    Thanks for the guide! The sensors arrived and I tested it. Firstly all to breadboard and it seemed to work fine but the values were negative and if I put some weight on to it, it risen towards zero so I calibrated it acc. to it. It worked. I put it under bed. Worked quite fine for 20 minutes. Even the values were above zero, so Irecalibrated it accordingly.

    I need to emphasize that I am using it on Weemos D1 mini with ESP Home, not your script.

    – I tried some jumps on the bed and it froze on big value. Somehow after few minutes it went back to working normal but after half an hour it stuck on same big value and that stayed all the night.
    – I have the sensors on double bed, am I damaged them or is there maybe only a contact issue?
    – Is there a way to found out which out of four is damaged/have bad contact? Any idea please?

    1. Lewis Barclay

      Hi there,

      Glad you got it working!

      I’m really not sure to be honest, I haven’t used esphome with this so maybe an issue there? Might be worth trying the above code and seeing if that works first and if you can re-produce the error, then you will know where the problem lies, software or hardware!

  10. Lipown

    I feel very sorry for disturbing you. It was a cable. Took me a couple of hours to investigate. I just found your 3D model on the thingiverse. Is there a litle space between the upper and lower part so the sensor might spread or how is it actually working please? Thanks!

    1. Lewis Barclay

      Glad you figured it out!

      That section of the model isn’t mine, the model just holds the sensor in place and provides a flat bed to place things on. Hope that helps!

  11. Steve

    I just finished installing something based off this πŸ™‚ It worked well, thanks. you helped a great deal there.

    The One problem I had was that I wasn’t getting a reading with the bed on the sensors. I think it’s because the cases I printed for the sensors was taking the whole weight. So I put a 2p coin on top of each (being smaller than the flexible bit of the case) and it started registering properly.

  12. Uros

    First great job with this torial.
    But I have problems. I dont get more then 0,5v on e+ and e- and i tray 3pcs of hx711 and on all 3 is the same. I’m usind d1 mini and esphome. I check the load cells and they work ok if i conect 5v direct on cells and measuring voltage on the oder side of bridge i get up to 5mv if i push all cells down.
    I the voltage from e+ and e- controled by the software or the hx711 do the job.

  13. Sam Hoyt

    I used your guide to install this on my bed last weekend, and it works great. My bed has posts on the slats underneath, so they hold some of the weight. This does mean the weight reading isn’t accurate, but it still reads consistently enough that I can use it.
    Thanks for putting this together. It made the job really easy.

  14. Sam Hoyt

    One of the best things I’ve done with it so far is I set up an alarm to wake me up in the morning. It starts going off at the time I set, and the only way to turn it off is to get up. If I get back in bed before the time I’m supposed to be at work, it starts going off again.

  15. andilge

    Hi Lewis,

    Thnx for this detailed and interesting post, the amount of feedback shows where a lot of us are going to.

    I’m about to roll this off at my home as well but I’m hesitant about the sensors for 2 things.
    – I don’t have a 3D printer and the printed holders look like a show stopper to me.
    – Since the topic is not really measuring my wight the sensors are probably over precise.

    What about using seat occupancy sensors that are in every car? They are build to support human weight, it’s possible to install them (one per person, not 4 per bed) on the slatted frame instead of the 4 feat. This would dramatically facilitate the installation, wiring becomes easier (like binary switches), cleaner installation where cables can be hidden better and no need for the individually printed holders.

    Here an example sensor if you don want to tear it out of your car

    1. Lewis Barclay

      Hey, thanks for the comment and feedback, glad you liked the post!

      That’s a pretty interesting sensor, thanks for sharing, I had not thought about that before!

      The only issue I can see is that because it cannot detect weight, it would not be suitable for 2 people since there is no way to tell if 1 person or 2 people are on the bed!

      I understand the issue with the 3d printer, it would be good if there was another solution! Tiny wooden casters maybe?


  16. Nate

    Hey there! I’m glad I finally ran into your post – this is a project that I’ve been working on intermittently over the past year.

    I have the exact same load cell – is it a half bridge load cell? How would I use it with just two load cells? Any tips on wiring?

    The reason I want to use just two is that I can’t quite figure out how to connect all 4 legs of my bed without splicing additional length of wiring on the load cells, which I want to avoid.

    1. Lewis Barclay

      Hey Nate! I’m glad you liked it!

      2 load cells should be fairly simple but I haven’t tried it in “production” so I don’t have any tips on the wiring.

      I have seen this post before which looks correct to me, hope it helps (very last post from Wawa):


      The rest of the guide should be the same once you get it reading. Please do let me know if it works or how you get it to work, I’d love to add that information to the post!

  17. Bob

    Hi Lewis,

    Well done, your instructions are clear, and it works well at the test ‘setup’ with a wooden ‘shelf’. Next step should mount it under my bed. Hope the WAF (Wife Acceptance Factor) is high enough to get it working for that goal πŸ™‚

    Although, one problem still exist. I adjusted the calibration factor to 2386.
    It shows my weight in Home Assistant 10 times higher; 2 kg is 20 kg.

    Should I adjust the bed_sensor script?

    1. Lewis Barclay


      Glad you got it working! Yes you just need to adjust the bed_sensor sketch with the calibration factor you worked out in the calibration sketch. This is the intended way to do it!

      Hope that helps.

  18. Pablo

    Hi Lew, first of all thanks for this guide!

    I mounted the bed sensor with your guide and it seemed to work properly, weight values ​​are very precise but after few minutes freezes and I only can fix it rebooting sensor. Something similar happened to another user (Lipown) and in his case the problem came from a cable but he didn’t say which was. What should I do?

    1. Lewis Barclay

      Hi Pablo,

      I would start by replacing the power (USB) cable to see if that helps. Double check all your wiring, have you soldered all connections?

      Let me know how you get on!

  19. Pablo

    Hi Lewis! First thanks for your reply!

    Yesterday I replaced the wiring between Wemos and HX711 and issue remains (now I think the problem occurs a little earlier). I have found that if I don’t put on weight everything works correctly but it stays frozen within a few minutes of loading weight.

    I have a 5V-2A mobile charger connected to the Wemos and today I will replace it to see if the problem persist, I will also check volts across E+ and E- as you said in troubleshooting section. On the other hand, do you think the issue can be caused by bad wiring between load cells?


  20. Pablo

    Hi! I think I resolved the issue! First, I changed USB cable and the power supply (phone charger) by another one and sensor became more stable, it stays online all night without any problem. Nevertheless, after one day sensor freezes another time (I think due to lack of communication with MQTT server). Then I soldered a Wifi antenna to Wemos D1 and it has worked until today without problems.


  21. Jan

    Hi Lewis,

    Great project! Just connected the load cells to a Wemos D1 Mini and uploaded the Scale_Calibration sketch. Seeing some readings in the Serial Monitor, but when pressing on a load cell, it keeps 0.00 kg. When seeing the ‘Reading: 0.00 kg’, I guess the loadcells are connected the right way?

    12:32:21.373 -> Reading: 0.00 kg
    12:32:21.373 -> Calibration factor: 2400
    12:32:24.789 -> Reading: 0.00 kg
    12:32:24.789 -> Calibration factor: 2400
    12:32:28.202 -> Reading: 0.00 kg
    12:32:28.202 -> Calibration factor: 2400
    12:32:31.604 -> Reading: 0.00 kg
    12:32:31.604 -> Calibration factor: 2400

    1. Lewis Barclay

      Hi Jan,

      I would certainly suggest checking the wiring, if you are getting zero readings it may be that the wiring to the HX711 board is correct, but the wiring from the load cells to the HX711 board is not. I would suggest double checking. Does the raw value change?

      Let me know!

  22. Jan

    Hi Lewis,

    Even though I’d checked the wires several times, the problem was there. Reconnected everything again, and now it’s working. Great!

    Another question: Is it possible to make the Wemos reconnected to the MQTT broker when the MQTT broker is restarted and offline for a short period of time?
    After restarting the Wemos, it connects to the MQTT broker, but it would be nice if restarting the Wemos is not necessary.
    I’m using Mosquitto MQTT broker as a Home Assistant add-on, so restarting HA after an update, makes the MQTT broker restarting as well.

  23. Pieter

    I followed your instructions about a year ago and have been extremely happy with the results ever since. Thank you for putting so much effort into this!

    That being said, I have a quick question. We’re getting a new bed soon, which had 8 legs total (4 per side of the bed). How would you approach this?

    Could I just string together more load cells to the same HX711 or would I need a separate HX711 for wacht side of the bed. The latter seems straightforward except for altering the code, which definitely isn’t my field of expertise.

    I have been having a little trouble with drifting values over the past year. It seems to be very sensitive to changes in temperature and humidity. The registered weight sometimes creeps up by over 40% a night. Any tips on how to tackle this?

    1. Lewis Barclay

      Hi Pieter,

      Thanks for the feedback, that is great! Glad you’ve been using it!

      Would it not be possible to just put the sensors on the legs closest to the corners still using 4 legs only? I’m not sure if you can use 8 sensors, it’s not something I’ve ever looked into. If you need to use all 8 legs then using a second HX711 for the other side is a good idea and might give you more accuracy overall.

      Drift can be down to wiring so I would double check you have solid connections everywhere. Do you automatically reset/tare the scales every day?

  24. Jan

    Just calibrated with 4KG of weight and figured out that a calibration factor of 20500 suits best.
    Kind of high, for doesn’t that matter?

  25. Huz

    Hey thanks for this tutorial! I am having issues following along though. I am getting stuck at

    Reading: 0.00 kg
    Calibration factor: 2400

    i did a continuity test from the load cell amplifier to the red wire solder point, and they all look good. Any other reasons i could be getting zero values? I believe my wiring configuration is correct, but just pulling out my hair thinking of something that I could be missing.

    1. Lewis Barclay


      In my experience, the reason for zero or strange values is 99% of the time, wiring! Perhaps you could double check if the wiring is the correct way around?

      Let me know how you get on!

  26. Silviu

    Hey Lewis,

    Great tutorial just started following it and already ran into some arduino errors, wondering if you can help me out.
    This is what I’m getting:
    Sketch uses 264644 bytes (25%) of program storage space. Maximum is 1044464 bytes.
    Global variables use 27080 bytes (33%) of dynamic memory, leaving 54840 bytes for local variables. Maximum is 81920 bytes.
    esptool.py v2.8
    Serial port COM5
    Connecting…….._____….._____….._____….._____….._____….._____…..____Traceback (most recent call last):
    File “C:\Users\silvi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1/tools/upload.py”, line 65, in
    File “C:/Users/silvi/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py”, line 2890, in main
    File “C:/Users/silvi/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py”, line 483, in connect
    raise FatalError(‘Failed to connect to %s: %s’ % (self.CHIP_NAME, last_error))
    esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
    esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header

    When trying the sketch for the calibration.
    The board is a wemos d1 mini clone it seems, ESP-12F, vendor DOITING
    Any idea what is going wrong?

    1. Lewis Barclay

      Hey Silviu!

      It looks like your ESP is not being detected correctly when is trying to upload the sketch. What settings do you have set for the board in the Arduino IDE?

  27. Silviu

    You can ignore my comment above, turns out one of the USB ports doesn’t like the D1 mini board but the other does so I got as far as executing the calibration sketch.
    The values are all over the place.
    20:47:27.801 -> Reading: -94.51 kg
    20:47:27.801 -> Calibration factor: 2400
    20:47:31.228 -> Reading: -90.41 kg
    20:47:31.228 -> Calibration factor: 2400
    20:47:34.627 -> Reading: -86.37 kg
    20:47:34.627 -> Calibration factor: 2400
    20:47:38.058 -> Reading: -67.99 kg
    20:47:38.058 -> Calibration factor: 2400
    20:47:41.486 -> Reading: -54.62 kg
    20:47:41.486 -> Calibration factor: 2400
    20:47:44.917 -> Reading: -38.40 kg
    20:47:44.917 -> Calibration factor: 2400
    20:47:48.344 -> Reading: 167.90 kg
    20:47:48.344 -> Calibration factor: 2400
    20:47:51.772 -> Reading: 164.28 kg
    20:47:51.772 -> Calibration factor: 2400
    20:47:55.170 -> Reading: 169.05 kg
    20:47:55.170 -> Calibration factor: 2400
    20:47:58.598 -> Reading: 164.73 kg
    20:47:58.598 -> Calibration factor: 2400
    20:48:02.029 -> Reading: 161.79 kg
    20:48:02.029 -> Calibration factor: 2400
    20:48:05.461 -> Reading: 162.17 kg
    20:48:05.461 -> Calibration factor: 2400
    20:48:08.889 -> Reading: 161.15 kg
    20:48:08.889 -> Calibration factor: 2400
    20:48:12.286 -> Reading: 156.15 kg
    20:48:12.286 -> Calibration factor: 2400
    20:48:15.718 -> Reading: 150.46 kg

    This is with the 4 sensors wired up and on my desk, not in their final location yet
    Any idea why their values are jumping all over the place?

  28. Silviu

    Nothing on them at all. I triple checked the wiring, it is exactly as your diagram, I also soldered the wires between the sensors to make sure they are not loose and still the same:
    21:09:52.626 -> Initializing scale calibration.
    21:09:52.626 -> Please remove all weight from scale.
    21:09:52.660 -> Place known weights on scale one by one.
    21:09:54.021 -> Reading: 4.42 kg
    21:09:54.021 -> Calibration factor: 2400
    21:09:57.451 -> Reading: -398.81 kg
    21:09:57.451 -> Calibration factor: 2400
    21:10:00.845 -> Reading: -437.59 kg
    21:10:00.845 -> Calibration factor: 2400
    21:10:04.275 -> Reading: -432.59 kg
    21:10:04.275 -> Calibration factor: 2400
    21:10:07.703 -> Reading: -434.40 kg
    21:10:07.703 -> Calibration factor: 2400
    21:10:11.132 -> Reading: -467.01 kg
    21:10:11.132 -> Calibration factor: 2400
    21:10:14.559 -> Reading: -474.45 kg
    21:10:14.559 -> Calibration factor: 2400
    21:10:17.986 -> Reading: -505.54 kg
    21:10:17.986 -> Calibration factor: 2400
    21:10:21.415 -> Reading: -495.21 kg
    21:10:21.415 -> Calibration factor: 2400
    21:10:24.812 -> Reading: -505.62 kg
    21:10:24.812 -> Calibration factor: 2400

    1. Lewis Barclay

      Can you try setting the calibration factor much lower? That high calibration factor assumes there is some weight on them in the first place. Try setting to 100 or so.

  29. Silviu

    That makes it way bigger.
    21:21:46.874 -> Initializing scale calibration.
    21:21:46.906 -> Please remove all weight from scale.
    21:21:46.906 -> Place known weights on scale one by one.
    21:21:48.261 -> Reading: -1051.43 kg
    21:21:48.261 -> Calibration factor: 100
    21:21:51.688 -> Reading: -6618.08 kg
    21:21:51.688 -> Calibration factor: 100
    21:21:55.113 -> Reading: -9459.63 kg
    21:21:55.113 -> Calibration factor: 100
    21:21:58.544 -> Reading: -10500.27 kg
    21:21:58.544 -> Calibration factor: 100
    21:22:01.938 -> Reading: -10983.61 kg
    21:22:01.938 -> Calibration factor: 100

    Also tried, swapping E+ and E- but it made no difference

  30. Silviu

    Yes I tried 15000 which just about gets to in the + but still not close to 0
    21:42:40.592 -> Reading: 9.50 kg
    21:42:40.592 -> Calibration factor: 15000
    21:42:44.022 -> Reading: 8.02 kg
    21:42:44.022 -> Calibration factor: 15000
    21:42:47.452 -> Reading: 7.75 kg
    21:42:47.452 -> Calibration factor: 15000
    21:42:50.878 -> Reading: 21.89 kg
    21:42:50.878 -> Calibration factor: 15000
    21:42:54.306 -> Reading: 28.87 kg
    21:42:54.306 -> Calibration factor: 15000
    21:42:57.734 -> Reading: 20.45 kg
    21:42:57.734 -> Calibration factor: 15000
    21:43:01.131 -> Reading: 20.98 kg
    21:43:01.131 -> Calibration factor: 15000
    21:43:04.565 -> Reading: 21.68 kg
    21:43:04.565 -> Calibration factor: 15000
    21:43:07.995 -> Reading: 3.85 kg
    21:43:07.995 -> Calibration factor: 15000
    21:43:11.426 -> Reading: 6.26 kg
    21:43:11.426 -> Calibration factor: 15000

    It went to 21kg and above when I stood on the sensors with my body weight, then back to a lower value so it detects something, albeit the wrong values.

  31. Silviu

    I have never seen it as 0, not even once. I wonder if the load cell amp is to blame here.
    I was able to get closer to 0 using a pretty high calibration value like below:
    08:47:28.261 -> Initializing scale calibration.
    08:47:28.261 -> Please remove all weight from scale.
    08:47:28.296 -> Place known weights on scale one by one.
    08:47:29.661 -> Reading: 0.00 kg
    08:47:29.661 -> Calibration factor: 50000000
    08:47:33.058 -> Reading: -0.06 kg
    08:47:33.058 -> Calibration factor: 50000000
    08:47:36.485 -> Reading: -0.06 kg
    08:47:36.485 -> Calibration factor: 50000000
    08:47:39.914 -> Reading: -0.06 kg
    08:47:39.914 -> Calibration factor: 50000000
    08:47:43.345 -> Reading: -0.06 kg
    08:47:43.345 -> Calibration factor: 50000000
    08:47:46.773 -> Reading: -0.06 kg
    08:47:46.773 -> Calibration factor: 50000000
    08:47:50.170 -> Reading: -0.06 kg
    08:47:50.170 -> Calibration factor: 50000000

    But putting weight on it has no effect, the value is always the same.

    1. Lewis Barclay

      The first value should always be zero after taring, I’d look into the module, is it the HX711 you are using? Are you using the stock calibration sketch with just the calibration factor tweaked? One thing that was actually important for me was the sleep, reading too fast seemed to cause issues like this.

    1. Lewis Barclay

      That’s the same one I used, I can only suggest sanity checking the wiring again. Have you done a test with a meter to make sure the values are correct. You should get 4.3v across the E+ and E- wires, and around 2v through the A+ and A- wires. Can you check that?

  32. Silviu

    So when I checked E+/- it goes 5.4v, 4.6v, 0 then again 5v… etc
    On A+/- however, I don’t get anything on my multimeter, just stays at 0

  33. Silviu

    Also when I checked the gnd/vcc on the hx711 I get 6.35v, is that a bit too high? the board says 2.7 – 5v for VCC

  34. Silviu

    I am getting around 2v on either A+ or – but with SD (yellow) and it does tare to 0 now however pushing down still keeps the reading at 0.00 kg

    1. Lewis Barclay

      Sounds like you’ve made some progress. How are you supplying the HX711? You should only be getting 5v at the HX711.

      It sounds like you have a wiring issue if the E+/- voltage is fluctuating, did you sort this now?

      You might be getting 0 until you run through the calibration process.

  35. Silviu

    So I replaced the cables and no joy, If I comment out delay(3000); then I get constant voltage (5.4v) on E+/- but still 0.1v on A+/- so the flapping must be because the on/off/delay in the sketch.
    If I combine A+ or A- with SD, I get constant 2.8v so there is something there.
    Also, isn’t 6.4v a bit too much power coming from the D1 mini board plugged into my PC’s usb port? Surely that should be 5v max?
    Not sure what else to try, when I have configured exactly as per your diagram, the readings are wrong but it does pick up on me adding weight to but when it the value goes down it never goes to 0

    1. Lewis Barclay

      6.4v is way out of spec yes, I would see if you can get a dedicated power supply? They can take a while to return to zero so it’s not an issue per say.

      Have you tried calibrating now you are getting it to tare?

  36. Silviu

    I also tried it on a few other laptop USB ports and getting the same voltage readings, same with a standalone USB Samsung wall charger. The tare doesn’t work when I am wired into E+/- and A+/-, just doesn’t go to zero but it does detect weight on all sensors just not the correct values.
    The fact I am not getting any voltage between A+ and A- sounds like hx711 is not great, maybe I’ll try another brand.
    Will also replace all cables today and see if I get anything else.
    Also I would definitely need some 3D printed legs for the sensors, have any links to drawings, thingsverse, etc? Thanks

    1. Lewis Barclay

      I am trying to now remember if 6.4v is normal, there is something that rings a bell but I can’t quite remember!

      Yes you definitely need to have voltage across A+ and A- otherwise something is off. I hope a new board works for you.

      I used these but also made a further holder that these slot into for my bed. I suggest use these as a template then make something similar to the photos I’ve posted here.

  37. Silviu

    Thanks for all your help on this. Do you have a link to where you bought the hx711? Maybe I can get the same one?

  38. Arthur Morgan

    Hey thanks for the tutorial!
    I’m new to these kind of setup, but how does it connect to power supply? can’t see it in your sketch.


    1. Lewis Barclay

      Hi Arthur!

      It just uses the micro USB port for power but you can also supply power to the 5v pins if you wish.

      But micro USB is easiest since we only need 5v for this application.

      Hope that helps!

  39. Silviu

    Got a bit further, printed all four supports and setup home assistant, however I am getting a weight of 280kg when I set on the bed (my weight is 75kg) but the thing is the bed is quite heavy (one of those beds with storage inside) so I am not sure if I will ever be able to get it right due to that.
    Any suggestions on how to adjust the weight so it is closer to real life?

  40. Silviu

    Still working on tweaking the taring/weight but one issue I am having is that whenever I reboot my HA instance the bed sensor goes to unavailable until I manually reset it from the small wemos switch which is a pain since it is tucked under the bed out of reach.
    Any idea how to make sure HA picks it up on restart?

    1. Lewis Barclay

      I thought so!

      Anyways that shouldn’t be an issue, make sure in the Serial logs that the reconnect function is working as it should, it should automatically reconnect as soon as the broker comes back online. Are you leaving enough time for the broker to come back online?

  41. Silviu

    How would I check that, in mqtt configuration it just has this?

    logins: []
    anonymous: false
    active: false
    folder: mosquitto
    certfile: fullchain.pem
    keyfile: privkey.pem
    require_certificate: false

    Unplugging/plugging the d1 mini also makes it come back, just need to get it so that it connects automatically on HA reboot

  42. Silviu

    I have the serial monitor connected in the arduino ide and when HA gets restarted the D1 doesn’t register anything, it still keeps printing the weight/calibration/raw values. Anything I need to set in mqtt in HA or on the D1 for it to reconnect?

    1. Lewis Barclay

      The function called reconnect in the D1 code should handle the reconnection. Feel free to join us on Discord (link in the menu) and I’d be happy to have a closer look!

  43. Arthur Morgan

    So it took some long time for all the different parts to arrive from AliExpress, but finally I could start this project with your great tutorial.
    So I finished with the wiring part, and I uploaded the Scale_Calibaration to the board, and I get weird readings. it doesn’t start from 0. it’s starting from different values, sometimes negatives. then it keeps going with different values, it does go up when I press my finger on them, then it takes long time getting back down, still showing above 1kg for a while. then it goes back to arround zero, but never zero.
    What do you think seems to be the problem?

    1. Lewis Barclay

      Hi sorry, I didn’t see this for some reason!

      When you say weird readings at the start, do you mean the KG reading or the raw reading? The raw reading will never start at 0 so don’t sorry about that.

      Sometimes it takes a little while to settle after a weight is removed.

  44. Arthur Morgan

    Thank you for your quick responses!
    seems like everything work for me except for the correct readings. I triple checked that the wiring is correct, unless there’s some defect in my cells / wires / hx711.

    The KG never start from zero, it starts from close to it, but then goes up and down without a reason. I don’t do anything and it goes slowly up. sometimes it shows negative kg and sometimes positive.

    I can see it goes up when I tough the cells with my finger, then it goes slowly down but never to zero. each time I push them harder, it shows higher measure even after no pressure at all.

  45. Arthur Morgan

    I tried to replace the Amp to the red Sparkfun Load Cell amp instead of the one that arrives with the cells, but I still get really crazy readings. it never starts from 0, and goes up and down all the time even when not touching anything πŸ™

  46. Tim


    Thank you for posting this tutorial, I picked this up as my first project with Arduino and have already learned much since.

    I did encounter a problem.
    When I tried your calibration program, the left lower load cell gave me a positive change and the upper right cell gave me negative change.
    The other two load cells don’t give a change in value whenever I press them.

    I tried changing around the E- and E+ pins, this reversed the problem.
    The voltage across the E- and E+ pulses from 0 to 3.4~3.9.
    I got no voltage across the A- and A+ pins.

    Is it possible my amplifier is faulty?
    Or is it still probably my wiring, which I, of course, quadruple checked?

    Thank you for your time.


    1. Lewis Barclay

      Hey Tim,

      Hmm that is a strange one. Generally its wiring but you have checked that. How do you know which corner is giving you a positive or negative value? Feel free to hop on Discord with us and I can help you troubleshoot. Link is in the menu.


  47. Sam+Hoyt

    Still using this on my bed, and it’s working great!
    I have moved from using your code to ESPHome, though. The programming is quite a bit easier, and it gives me the option to update it wirelessly. Plus, it integrates directly with Home Assistant so I don’t have to use MQTT anymore.
    I never had any issues with your code, as it did exactly what it was supposed to, but the benefits above made it worth switching for me.
    Just figured I’d check in and mention this in case you want to check it out.

  48. Tim

    Hey very nice project! I have two questions:

    Can you please provide the 3D printer files. You have this nice little top for the load cell. I did not find a file with this on github. The other file for the foot of the bed would also nice, I know i have to resize it but it looks incredible!

    I have wired up everything on my desk and when I’m testing I gt values like -0.19, -0.13, -0.06. Is this okay?

  49. Sam Hoyt

    This is the sensor part of the code. I used the lambda function to filter out any low values I didn’t care about and convert to pounds.

    – platform: hx711
    name: “Bed Sensor”
    dout_pin: GPIO4
    clk_pin: GPIO5
    gain: 128
    update_interval: 5s
    – calibrate_linear:
    – 1685436 -> 0.000
    – 1696767 -> 5.216
    – lambda: if ( (x * 2.2) < 2 ) { return 0; } else { return x * 2.2; }
    unit_of_measurement: lb
    accuracy_decimals: 2

  50. Drew Finkel

    Lewis, thanks so much for the guide! It’s great and I ALMOST have everything working.
    Calibration- check
    Installed- check
    Realizing I have 6 legs on my bed…. Crap!
    Is there a way I can wire in 6 weight sensors instead of 4? Do I need 2 hx711s and add the values together. I’m so close!
    Any help is appreciated. I’d love if I could just ass 2 more sensors but am unsure if b+ and b- would work.

  51. Dan

    From what I can see, my bed has very similar legs as yours. Is there a place you posted your 2-part 3D printed casters? Thingiverse?

    I LOVE IT!

  52. Vali


    Firstly, many thanks for putting all this together! Nice job!

    I have trouble with integrating it in Home assistant. It shows in the Developer Tool as:
    sensor.bed unavailable

    I can see from the serial monitor that is connected to IP and MQTT server.
    The onli way I can see any readings in Home assistant is if I listen to one of the topics in the MQTT integration configuration but that is temporary until I close the Configuration window.

    Any idea where am I doing it wrong?

    Many thanks !

  53. Val


    Yes it is, but I have a lot of movement.
    I placed a 15kg weight on it and its jumping up and down from time to time. any Idea why would that be?

  54. Pete

    Having only just got on to the channel and site I am very interrested in this however I have the same concerns that the bed plus 3 in the bed (me, Partner and German Sheprd, unless having a fun night lol) would go over the 200kg mark and thought if you were to split the 4 legs so each leg has 2 points of loading and put 2 sets of load cells under each leg with 2 wheatstone bridges and then added the two readings together if that would work as there are no cheap load cells that I can find at say 75kg or 100kg – what is peoples thoughts – is this a cost effective way to get a max of 400kg?

  55. Vman

    to clarify, my sensors show as unvailable in home assistant, Mqtt is running off separate pi, and sensor is showing in mqtt on the pi and home assistant is connected to my mqtt as well

  56. Alessandro

    Hello, thank you for this writeup.

    I’ve noticed that I have to restart the sensor by disconnecting it from power every time that I restart Home assistant, or the sensor will stay “unavailable” any advice for that?
    My understanding is that the sensor does not reconnect automatically to the MQTT server, I’m not a coder but maybe something can be implemented in the code to make sure it keeps the connection alive?

  57. Lee

    Great project, i’m in the process of doing this myself. Maybe i’m being a bit dim, but once i have uploaded the calibration sketch, and calibrated the sensors, do i need to overwrite that sketch with the bed sensor sketch ?

  58. Vlad

    Hello, Lewis. First of all I want to thank you for taking your time to write this article for us.
    I managed to get as far as integrating it in Home Assistant. The issues I’m facing is that I can’t get the sensor to show up in Dev Tools. I pasted the code you used but it’s not working for me for some reason. Could you advise?

  59. Stooovie

    I got stuck on the esp8266wifi library. I have a ESP-WROOM-32 board and for the life of me I am not able to get that library to run and compile.

  60. Spider2

    Have the same setup. D1 mini, HX711 green board and 4x 50kg load cell.
    Tried everything, but not working. Arduino and tasmota too. 2 different laptop and pc. Changed usb cables and ports.
    Used D1 mini 5V GND D1 and D2 ports. Tried 3.3V too.
    Have 2x HX711 and 8x loadcell not working at all. Only D1 mini can’t change yet. Maybe that is the problem?
    Tried E- to GND but nothing changed, or sometimes drifting the data up and down.
    When I measure the load cells all are good E+ and E- 2kohms and the same between A+ A-.

  61. Tomi

    Drifting values:
    First of all, thank you so much for this awesome project. What a cool feature and so many ideas what to use it for. I have got it running but I have an issue I would like to ask for some feedback.
    If I lay down on my bed, I get a value immediately. But the value keeps rising without any movement for quite some time before the stop. Do you experience the same?
    2021-11-18 00:33:49 Raw: 2226658 Scale: 135,1 kg
    2021-11-18 00:33:53 Raw: 2267057 Scale: 145,7 kg
    2021-11-18 00:34:03 Raw: 2258479 Scale: 143,5 kg
    2021-11-18 00:34:18 Raw: 2269069 Scale: 146,3 kg
    2021-11-18 00:34:22 Raw: 2265418 Scale: 145,3 kg
    2021-11-18 00:34:32 Raw: 2270499 Scale: 146,6 kg
    2021-11-18 00:34:37 Raw: 2285538 Scale: 150,6 kg
    2021-11-18 00:34:46 Raw: 2313646 Scale: 158,0 kg
    2021-11-18 00:34:51 Raw: 2309172 Scale: 156,8 kg
    2021-11-18 00:35:11 Raw: 2313647 Scale: 158,0 kg
    2021-11-18 00:36:18 Raw: 2317178 Scale: 159,0 kg
    2021-11-18 00:38:33 Raw: 2320696 Scale: 159,9 kg
    If not. I will check the wires again. Or can it be that the wires to each sensor are differently long (My Board is in one corner of the bed and the sensors on all 4 corners, so the wires of the sensor next to the board is of course shorter than the one on the opposite site.

  62. Duke

    Hey Lewis, thanks for this article, I did everything and the whole setup was working fine. But after a couple of days my reading got stuck at 0.00, no matter what I do, the reading is still 0.00, even if I calibrate it again and again or even reset it using the button on wemos, still the reading was 0.00. Then, only when I take off all the load from the load cells, and then reupload the sketch, my reading changes back to normal and then the loadcells work fine. But after putting the load cells back under the bed and after some time the reading again gets stuck at 0.00. I dont know what to do … I tried changing the wemos but it happened with the new wemos too. What do you think can be the problem ?

Leave a Reply

Your email address will not be published. Required fields are marked *