Servo, Load Cell Arduino Code Help

Thread Starter


Joined Nov 22, 2022

I am an engineering student completing a project for a senior-level strength of materials class. The project uses a servo-360, load cell, and Arduino MEGA 2560 to dispense pet food/treats into a bowl. I would like to have the servo spin and dispense x amount of food until a certain weight (in grams) is reached in a bowl on a load cell. In other words, say there is a bowl with 250 grams of pet food on the load cell. When the bowl weight goes below a certain threshold, i.e. 100 grams, the servo will activate and dispense for x amount of time/revolutions.

I have 3D printed and assembled a mechanism that will allow me to do this, and all of the individual components are wired and working. The load cell is calibrated and reading properly, and the servo is working. They are currently working separately using different codes. I would like to combine the codes so that my desired functionality can be obtained. I can post the codes that I am using below. My servo is connected to pin D9 of the Arduino MEGA.

Is anyone able to assist me with the creation of this code? Any guidance/help would be greatly appreciated. Thanks, and happy holidays!



Joined Jul 10, 2017
Create a flow chart of a complete loop of what you want to happen. Once you include all the details necessary to make it work, you can easily take the tested code for the various components and combine them, using the flow chart as a guide..


Joined Aug 31, 2022
It honestly sounds like you (or maybe others if this is a team project) have done all of the hard work. Surely, if you are checking the weight maybe every minute you can use a simple "if" statement to trigger the servo to dispense more food if the lower threshold has been passed. Ideally, instead of dispensing food every time that threshold is passed it would be better to keep rotating the servo until an upper weight has been passed.

Combining the codes is mosty a matter of copying and pasting code already written and making sure that you haven't used the same variable name for two different variables. This all sounds simple enough to do briefly without calling subroutines but if you already have chunks of code doing the individual functions you mention then it might be quicker and easier to follow if you call subroutines - for example, if weight below threshold, call servo subroutine.

Frankly, if you are serious about being an engineer I really think you should be eager to work this out for yourself - the arduino web site is really well documented and you will only learn if you try to research and write the code, including working out why something doesn't work! Always write small chunks at a time and test to see you get the expected result. You are lucky that the Arduino compiles so quickly - in my day I wrote Fortran, punched cards by hand, and only found out that I'd missed out a comma the following day.

Once you have it working you might want to think of ways to improve the project. For example, best not to trigger the servo until the weight has been fairly constant below the threshold for a while to avoid dispensing food whilst the pet is still eating. And triggering an alarm if the weight really falls too low, or doesn't go up by the expected weight for each servo rotation suggesting the food hopper is empty or the servo is not working.