I've been trying to get my head around the best method of controlling the throttle channel on my KK tricopter to get it to stick to a set altitude (anywhere between 0.5 to 8m) using just my XL sonar I pulled from my APM based quad and an Arduino Uno board.  

To start with, I coded the sketch to use channel 5 to turn the alt hold code on or off.  When off, the source throttle PWM value is just passed to the throttle out as is.  When on, the current altitude and input throttle PWM are captured and used as a reference.  When the height drops below the captured height the heigh difference is added to the captured PWM (hover PWM) to push the tricopter back up.  If the measured hight is higher than the captured reference height the height difference is subtracted from the hover PWM value to allow the tricopter to come back down.

This is obviously a very simplistic approach to trying to control the throttle to make it stick to a set height.  In practice this just results in an ever increasing bounce up and down as soon as I flick the switch on channel 5.

I did try a measured pulse up to start it coming back up and then a measured throttle drop as soon as the hight was obtained but as soon as wind was brought into the equation this just resulted in the tricopter drifting away from the desired height constantly.  

The frustration of all this is getting the better of me so now I am looking into what PID calculations are and how they could apply to this requirement.

I have also ordered a 3 axis accelerometer to see if detecting the fall/rise motion as well as the height will increase the accuracy.

I know this is a large request, but has anyone got some code lying around which specifically uses PID algorithms to calculate the required output throttle given the following input values -

1) hover PWM (the PWM captured when the mode is changed to alt hold)

2) desired height (cm captured when initially set to alt hold)

3) current height (cm)

4) optional z acceleration

I did have a look at the code for arducopter but there seemed to be a number of calls that were not applicable to the Uno, just the Mega.  

Any simplified examples of PID code for height control would be handy right now.


You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –


  • Hey guys, this post turned into a gold mine for my weekend project!

    I can't wait saturday to read the entire discussion since last week!!

  • I've somewhat moved on from the KK board and tinkering with an external Arduino/sonar to get alt hold.

    My thing now is the MultiWii platform.  I'm now stuck trying to work out what is involved in getting my I2C sonar working with it. The MW 2.1 code support the sonar I have however the data is only fed to Debug and not actually used for height control.  Still waiting for someone to post how they have been able to get the code to work.

  • Hello guys, i'm working on it

    here http://blog.giuseppeurso.net/low-altitude-hold-auto-takeoff-autolan...

  • Im in the same boat. How's it going?

  • Hi there!

    I'm actually trying to do EXACTLY the same thing as you are..... ( except mines not for a Uno ) - just a mega.

    I've only JUST starting to program with Arduino, but can I possibly take a look at what you have so far?,- ( possibly ) between the 2 of us, we could find some sort of answer to this project.

     Yes, I DID say I was "NEW" to this,.... but I have been programming for many years.. -your thoughts?


This reply was deleted.


sam liked Jimmy Oliver's profile
Aug 25
Mike Whitney liked Mike Whitney's profile
Jul 19
Abdulatif Tlaib liked Ryan's profile
Jun 24