# Connected Cat Feeder Using a Strain Gauge and an ESP32

4,603

19

3

Feeding our beloved animals is a rewarding job. We do not want to forget it. So, let’s build a connected scale, which alarms us when the food bin is empty. Of course, we will be able to use the same principle to measure the weight of any other good like the honey in remote beehives, or even your own weight to visualize it somewhere in the cloud…

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Watch the Video

The video should give you a basic understanding on how to measure weight and to build a connected cat feeder. The next steps will contain additional information to make recreating this project simpler.

## Step 2: Measuring Weight

The usual way to measure weight is with strain gauges. Strain gauges are relatively simple devices: Thin “PCBs”, or carriers, with copper traces. These PCBs are sensitive to all kinds of forces and change their resistance accordingly.

If we stick such a strain gauge on a metal bar and apply a force to one end, the resistance of the copper traces increases or decreases, depending on the direction of the force. That’s all. In principle. You can imagine that the distances these gauges are extended are minimal if the forces are small. This is, why we have to use some tricks. But we are Tinkerers and Engineers, and we love tricks to make things work. So, the first trick is to not only place one strain gauge on the bar. We place two of better four of them on both sides of the bar. Trick number two is: We weaken the rod that it bends easier. This can be done by drilling precise holes through the bar, right under the gauges. So, with a defined force, we get a bigger movement. And trick number three uses these two or four signals from the sensors and combine them in an interesting manner. It is called the Wheatstone bridge because Sir Charles Wheatstone popularized this concept already in the 19th century. Its main advantage is its ability to measure resistors extremely accurate and it cancels temperature effects. This is exactly what we need here. So, we combine our two or four gauges into such a bridge. Fortunately, we can buy such devices with everything mounted and connected for a few dollars. They are called "Load Cells". These bars are made for a particular weight, for example for 2, 5, or even 40 kg. And they are announced to be very precise.

## Step 3: How to Connect Load Cells

The colors of the wires seem also somehow standardized. Red, black, green, and white. If your sensor has different wire colors, you easily find which is which if you search for the wires with the higher resistance between them. These are then the opposite wires.

Now we should be able to see very small changes in voltage if we bend the metal bar. But these are really small voltages as we see here. If I bend the bar quite a lot, we only measure around 2 mV.

Now we have a Sensor to measure forces, not weight. Sir Isaak Newton, also an Englishman, also a long time ago, discovered a way to convert weight in forces and vice versa. This is what we will use now.

Load Cells usually have threaded holes on each side. I mount one of them to a base plate and make sure it can move a little. For this prototype, I use an old PCB which I found in my lab. Then, I mount a bowl on top of the opposite side. Like that the sensor is bent a little if I put cat food into the bowl and we should be able to measure a voltage created by this bending force. And now, the star of this video enters the scene. She is called Dishka and is always hungry. Fortunately, she has long hair, so nobody sees her belly. But even if she is hoggish we have to measure her food in grams per day. So, our bowl has to be very sensitive. And this is why we need trick number four: A very sensitive and accurate analog-to-digital converter, also called ADC. And of course, this ADC should not cost more than the cat itself…

## Step 4: How to Measure These Small Voltages Precisely: the HX711

Because these days everybody is interested in measuring weight, such ADCs are produced in extremely high series. This makes it possible, that we can buy a 24-bit ADC with a built-in amplifier for a dollar if we buy a few of them. Its name is HX711. Cool. We connect the top and the bottom wires, in my case black and red, to the E+ and E- pins and the other two to A+ and A-. the B+ and B- pins are not used. Here, you could connect a second strain gauge.

## Step 5: Connect the HX711 to a Microcontroller

Because these days everybody is interested in measuring weight, such ADCs are produced in extremely high series. This makes it possible, that we can buy a 24-bit ADC with a built-in amplifier for a dollar if we buy a few of them. Its name is HX711. Cool. We connect the top and the bottom wires, in my case black and red, to the E+ and E- pins and the other two to A+ and A-. the B+ and B- pins are not used. Here, you could connect a second strain gauge.

Now we only have to decide on the microprocessor and search for a library. If we want to use an Arduino Uno, we find a few libraries which work fine. We only have to connect 5V, ground, clock, and data to the respective pins, load the example sketch and are ready to go. We already see some values, and if I press the bowl, we see the values moving.

As always with scales, we first have to zero the device, and then, we have to calibrate it using a known weight.

## Step 6: We Want a Connected IOT Feeder!

But wait: We wanted also to get a tweet if the bowl is empty. So, we need a connected device. And because the newest kid on the block is an ESP32, I will use one of these new boards with an OLED. So, let’s check if the library works also with this new processor. The only thing we have to do is to adapt the data and the clock pins to the pins of the ESP32. I use pin25 and 26.

The HX711 also runs on 3.3 volts. So, no problem.

## Step 7: Problems...

Unfortunately, from time to time, we see outliers in the results. Not good! What is the problem? As always with intermittent problems, finding the root cause is not easy. But because we know, that it worked with the Arduino Uno, we can suspect, that it could have to do with the higher speed of the ESP32. Speed is not always good.

If we look at the timing of the two signals, we see, that the shortest signal is a little more than 100 nS. And if we look at the datasheet, we see, that this is too fast for the chip. It has to be more than 200 nS.

## Step 8: And Solutions...

Usually, we can add a few delays here and there to fix this issue. Not so here, because the library uses the ShiftIn command, which reads 8 bits in a row without intervention possibility. And here, we have 3 shiftin() commands to read 24 bits in a row. So, the easiest way to solve this problem is to slow down the ESP32 chip itself. The ESP32 is a very fast chip and we can afford to slow it down for this application.

Slowing the ESP32 down is quite easy. Just include this library:

#include "soc/rtc.h"

and add this command at the beginning of the sketch:

void setup() {

rtc_clk_cpu_freq_set(RTC_CPU_FREQ_80M);

Then, our ESP32 runs with only 80 MHZ and the signal is now 3 times longer, much better for the HX711. Now, the outliers disappeared. And fortunately, the Serial speed, as well as the Wi-Fi communication, is not influenced.

## Step 9: The Sketch

The whole prototype is based on the example file of the HX711 library.

https://github.com/bogde/HX711

The sketch: https://github.com/SensorsIot/Weight-Sensors

Next, I make sure the OLED shows the values. This is very comfortable. Even if the compiler warns us, the library works with the ESP32. Now we have a nice setup to test the accuracy of our bowl. The calibration commands are included in the sketch. Please uncomment it the first time you use it.

First, this routine adjusts the sensor to zero and then waits for a defined weight to be added.

scale. set_scale();

scale.tare();

displayString("Calibrate", 64, 10); delay(2500);

displayFloat(scale.get_units(10), 64, 15);

The display shows: 11691

while(1==1);

Because Dishka is not interested in grams, I use cat food for calibration. I counted 50 pieces, and enter 50 as a calibration value. Now, the scale is ready to count the pieces and we can comment these lines.

This is why we take the number (11691) and divide it by the number of pieces (50). The resulting number has to be keyed in here.
scale.set_scale(233.82);

## Step 10: Connect the Feeder to the World: MQTT and Node-Red

The next step is now to connect our ESP32

to the Wi-Fi network and to add the PubSub MQTT library, which works also with the ESP32. And because I already have a Raspberry Zero running Mosquitto and Node-Red, I easily can connect my new cat bowl with this system. If you do not know how to set up a Raspby for this purpose, you can watch video #126.

Because my bench light is connected to my MQTT broker, I could now switch my work light off for a moment if Dishka emptied her bowl. But we said we want a tweet. To achieve this, we add an MQTT receiver node and make sure it receives only the actual number of remaining pieces in the bowl. Then, we add a switch node to make sure, the tweet is only sent if less than 4 bites are in the bowl, a trigger which makes sure the tweet is not sent too often, and a twitter node to connect to Twitter. Done. You find the flow also here.

https://github.com/SensorsIot/Weight-Sensors

Just copy it to the clipboard and import it from there to node-red.

Now, I get alarmed if poor Dishka is hungry.

## Step 11: Connect the ESP32 to the MQTT Broker: PubSub Library

The next step is now to connect our ESP32

to the Wi-Fi network and to add the PubSub MQTT library, which works also with the ESP32. And because I already have a Raspberry Zero running Mosquitto and Node-Red, I easily can connect my new cat bowl with this system. If you do not know how to set up a Raspby for this purpose, you can watch video #126.

Because my bench light is connected to my MQTT broker, I could now switch my work light off for a moment if Dishka emptied her bowl. But we said we want a tweet. To achieve this, we add an MQTT receiver node and make sure it receives only the actual number of remaining pieces in the bowl. Then, we add a switch node to make sure, the tweet is only sent if less than 4 bites are in the bowl, a trigger which makes sure the tweet is not sent too often, and a twitter node to connect to Twitter. Done.

Now, I get alarmed if poor Dishka is hungry.

## Recommendations

• ### Internet of Things Class

21,646 Enrolled

## 3 Discussions

can i get a true pieces with another food, i think it very nice if i can the get unit per pieces with one calibrate. is there a solution for this? like auto calibration so i can use this to count anything with one calibration? im very confused to make this

Thank you for sharing the good work..But initialisation of scale is giving some random value irrespective of putting known weights..screenshot of the same is attached ..why is it coming? how can i get real value?

Very interesting. I was wondering what the minimum weight is that you can measure. Any idea?