# How to programatically calibrate Turningy esc?

I have a Turnigy ESC and I am controlling it from AVR. Now I need to calibrate it to set the range of the input. With a servo tester I managed to calibrate it without any problems, more or less by following the user guide, but when I try to do the same procedure from code, the ESC starts beeping in some confused pattern and then enters programming mode.

My code looks like this:

void calibrate_turningy_esc()
{
servo_set16(SERVO_RANGE_TICKS);
for (uint16_t i = 0; i < 10000; ++i)
_delay_ms(1);
servo_set16(-SERVO_RANGE_TICKS);
for (uint16_t i = 0; i < 10000; ++i)
_delay_ms(1);
servo_set16(0);
for (uint16_t i = 0; i < 10000; ++i)
_delay_ms(1);
}


where +SERVO_RANGE_TICKS is 2.2ms pulse length, -SERVO_RANGE_TICKS is 0.8ms pulse length and 0 is 1.5ms. The timeouts of 10s were measured during the manual calibration with a stopwatch.

I have checked with an oscilloscope that the output servo signal looks the way I would expect it -- 10 seconds of 2.2ms pulses, 10 seconds of 0.8ms pulses and then 1.5ms pulses. Edit: I made a mistake here, see my answer.

Do you have any idea what to change to calibrate the ESC?