Arduino PID for propeller pendulum to balance horizontally

Thread Starter


Joined Sep 28, 2021
Dear all,

this hobby project I setup is using an encoder to make the stick to be 90 degress which is at 1500 steps (0 degrees=perfectly horizontal using PID control). My encoder counter code is obtained from video here and I have tested it which works perfectly well. Since this encoder is 1200 steps/rev. it would mean it take 300 steps for 90 degrees rotation. My encoder initial position are shown in diagram below which is 1200 steps initial starting point. May I know the arduino code to balance the stick at 90 degress or prefectly horizontal using PID? I have also checked when PWM increase, the fan will increase fan speed which will rotate clockwise :) . I have attached a encoder counter code below and I have checked that all PID code should be placed inside the IF statement in order for counter to work properly and not skipping any steps. I have attached my encoder counter code below.

Thank you for reading and have a nice day .




Joined Aug 31, 2022
An interesting idea! Have you checked that the motor/fan when turned fully on will pull itself from vertical to horizontal - and beyond? If not, maybe extend the stick above the encoder and add some weight at the top so the fan has less work to do.

If I were doing this on the cheap I might use a potentiometer instead of an encoder so the position is a variable resistance measured by the analogRead() but let's stick with the encoder as you have it. I think the code should start with driving the motor with a relatively low value of analogWrite() and check the stick angle. With this you have enough information looking at the force vector to calculate roughly what PWM value you will need to reach horizontal. So then drive with say 80% of this calculated value to check the position again. The problem with overshooting horizontal is that the stick will keep going!

As you get close to horizontal the sensitivity to changing analogWrite will increase but your vector calculation from the above process will give you information about how sensitive. When close, I would increment the analogWrite() value one step at a time with maybe 100ms delay between steps. As soon as you overshoot maybe reduce by five steps then increment upwards with a bigger delay, maybe a second, between steps. You should reach a condition where you can toggle between two steps and start to modify the (different) delay between up and down steps.

This is not PID, it's treating the control as a state machine - but I think it should work. The code should be written piece by piece - first step maybe to use a potentiometer as a potential divider to find the actual analogWrite() value which works to hold the stick close to horizontal.