Could use some help with automatic retractable landing gear for quadcopter

Hello all,
I am still very much a newb with Arduino although I have been kind of using it off and on for the past two years with flight controllers, on screen displays, opentx and a few other things. Everything I have done so far is just changing selections in other peoples sketchs. so I kind of know how to use it but not how to write it. I have watched about two hours worth of youtube beginner videos, played with the blinking sketch and bashed my head on a few walls!

Anyways. Here are the specifics of what I am using

Arduino Mini Pro
HC-SR04 Sonar sensor
2x servoless retracts (using a regular servo right now for testing)

I followed this guys instructions found here and his code.

My wiring is a little cleaner :slight_smile:

So the problems I am having is

#1. It is inconsistent, it will trigged the servo where I set the height but sometimes the servo just starts doing seemingly random movements.

#2. The servoless retracts do not work at all with this setup… No idea why. here is a link to the retracts I am using.

Any help is greatly appreciated everyone! thank you and please use small words!!!

It sounds like the servos as well as the Arduino are not getting enough voltage and current. The servos should be getting at least 5.7V at around 500 - 800mA each.

What is the Arduino running on, 5v or 3.3v?

The servos, arduino and sonar are all hooked up directly to a 5v 3A output through a "power rail". In the picture it may look like it is all plugged into the arduino but they are not. The sonar trig and echo are both wired into pin 7 and both servos signal wires are on pin 9.

I could switch the bec output to 6v but I don't want to burn up anything.

Then your servos are not getting enough voltage because 5v is just not stable enough, it needs to be within 5.7v - 6.2v.

The sonar trig and echo are both wired into pin 7

Why both on the same pin? why not use pin 8 as the echo pin?

Both echo and trig are on 7 because that is how the other guy did his and I was following his guide. I don't know enough about arduino to switch it myself. Is it safe to run everything on 6v? I have two BECs but I don't really want to use two separate ones to power all of this! Thanks for taking the time to help me out.

I tried powering the servoless retracts on 6v with the arduino and sonar using their own 5v power supply and still not even a wiggle from the servoless retracts.

Post your code and please use code tags.

Click the button </>, its the one right above the smiley faces and next to the big bold B.

 /* Auto Landing gear sketch by Benbojangles 2015

This sketch is to allow you to connect your landing gear to an arduino

so that the arduino will trigger the landing gear to activate at a user defined altitude.

I have used a HC-SR04 sensor as sonar which, for me, seems to measure from 0cm-to-300cm, 

depending on your landing gear trigger needs this may not be acceptable and you might want

to research other sonar senors with greater range.

to set trigger altitude change "int trigger_altitude = 30" to your desired alt itude in cm


   The circuit:

    *HC-SR04 Vcc connection attached to +5V Arduino

    *HC-SR04 GND connection attached to gnd Arduino

    *HC-SR04 Trig + Echo connection both attached to digital pin 7

   This example code is in the public domain.


#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 

Servo myservo2;

const int pos1 = 15;    // variable to store the servo position 

const int pos2 =90;

const int pos3 = 165;    // variable to store the servo position 

const int pos4 =90;

//data Pin number of the Arduino:

const int pingPin = 7;

int trigger_altitude = 30; //here we define the height in cm that we want to trigger

void setup() {

  // initialize serial communication:


  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

  myservo2.attach(8);  // attaches the servo on pin 9 to the servo object


void loop()


  //distance in centimeters:

  long duration, cm;

  // The sensor is triggered by a HIGH pulse of 2 or more microseconds.

  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

  pinMode(pingPin, OUTPUT);

  digitalWrite(pingPin, LOW);


  digitalWrite(pingPin, HIGH);


  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the sensor: a HIGH

  // pulse whose duration is the time (in microseconds) from the sending

  // of the ping to the reception of its echo off of an object.

  pinMode(pingPin, INPUT);

  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance

  cm = microsecondsToCentimeters(duration);




 if (cm < trigger_altitude)






 else if (cm > trigger_altitude)








long microsecondsToInches(long microseconds)


  // According to Parallax's datasheet for the sensor, there are

  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

  // second).  This gives the distance travelled by the ping, outbound

  // and return, so we divide by 2 to get the distance of the obstacle.

  // See:

  return microseconds / 74 / 2;


long microsecondsToCentimeters(long microseconds)


  // The speed of sound is 340 m/s or 29 microseconds per centimeter.

  // The ping travels out and back, so to find the distance of the

  // object we take half of the distance travelled.

  return microseconds / 29 / 2;


I also found this today while researching my issue.

Maybe you can make out what is going wrong there.

I am going to rewatch all of those YouTube tutorials. This project seems so simple but so complicated at the same time, I knew I should have studied robotics instead of helicopters!

Well your code does work as intended so that means it all comes down to power and how clean it is.
Running two servos and an arduino off the same battery is not usually good. By the way, what are you using as your power source?

Try using a separate power supply with the grounds connected and see if your servos work.

Very simple servo test code you can try to see if the servos position as commanded. You can attach both servos control wires to the single arduino pin to see if they move together, Note that the servo, external power supply and arduino grounds need to be connected together.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.println("servo-test"); // so I can keep track of what is loaded

void loop() {

  while (Serial.available()) {
    char c =;  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    Serial.println(n); //so you can see the integer

The way I am powering everything right now is using a set of pins that are 6x3 (the common pins that servos plug into) The middle row is all positive and one side is negative. The other side has two of the pins connected to pin 9 on the arduino and one more is connected to pin seven. So nothing is actually being powered by the arduino, I have a ubec (used on rc aircraft to power lower voltage electronics off of higher voltage packs) the ubec plugs into the set of 6x3 pins supplying 5v at 3 amps.
The arduino plugs into the next set of pins, getting power from the ubec, then the sonar and then the two servos. I will get up close pics tomorrow. Thanks again for the help everyone.

Attached is a pix of basic wiring setup for servos with external power.


95% of servo issues we see here is either:

  1. too feeble a power supply
  2. trying to power a servo from the same supply at Arduino's 5V

Basically I'd never advise the latter, and suggest budgeting 1A per servo minimum.

All high current wiring (servo and its supply) should be twisted pair and kept well away from
microcontroller and sensors. Use a single common ground. Never run power and sensor/logic
wires together in parallel unless all cables are screened.

Sorry I haven't replied in awhile. I had surgery last week and have been out of it!

So, I completely rewired everything so that the servo would be powered with a 6V 5A power supply and everything else would be powered with a 5V 5A powered supply. everything using a common ground, I got the exact same results! The regular servo would work just fine and the servoless retract would not work at all. I can plug the servoless retract into a 5V servo tester and it works just fine.

I think I am going to just abandon the project, I have no idea why the servoless retracts will not work. I have tried two different power supplies and I am not trying to power it from the Arduino. A regular servo works just fine and the servoless retract does nothing!

I appreciate everyone trying to help me out though!


The regular servo would work just fine and the servoless retract would not work at all. I can plug the servoless retract into a 5V servo tester and it works just fine.

Servo testers only put out a control range of ~1000 to ~2000us, Are you using this control range in your setup?

Well if the "servoless retract" is not a servo, then what actually is it ?

Well if the "servoless retract" is not a servo, then what actually is it ?

It may operate more like a continuous rotation servo. I'd have to see how the device is controlled from an RC controller.

Actually its just a dc motor that turns a worm gear. The gear also moves a bar connected to a sliding potentiometer.

It seems like the servo code would work, but something is different about it.


This code works perfectly for me.

I made some modifications. PWM, on the Arduino Pro Mini, works on ports 9 and 10.
The servo working range is 0 to 180.
I am sending my code attached.
My configuration is:
trigPin 11
echoPin 12
Servo port 10

Bruno Frota

LandingGear.ino (1.21 KB)