Project using an arduino, servo, and ultrasonic distance sensor. This script will allow a drone to sense when it is 3 feet from the ground so it can deploy its landing gear.
If you want to change the distance, change the conditions in lines 63 and 67 to match the distance you would like. It's in centimeters.