PID for Motor control.

So im looking to control motor speed for landing autonomously based on feedback from an ultrasonic sensor.

I recently came across using a PID Library.

However im not sure how to go about it.

Thanks.

Below is the example from the library

/********************************************************
 * PID Basic Example
 * Reading analog input 0 to control analog PWM output 3
 ********************************************************/

#include <PID_v1.h>

#define PIN_INPUT 0
#define PIN_OUTPUT 3

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

void setup()
{
  //initialize the variables we're linked to
  Input = analogRead(PIN_INPUT);
  Setpoint = 100;

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  Input = analogRead(PIN_INPUT);
  myPID.Compute();
  analogWrite(PIN_OUTPUT, Output);
}

Do i calculate the distance in cm and directly put that into the input variable with the Setpoint as 0 for landed?

LimeAy:
So im looking to control motor speed for landing autonomously based on feedback from an ultrasonic sensor.

Landing what? Can you explain your project and existing hardware?

Power_Broker:
Landing what? Can you explain your project and existing hardware?

Apologies - im using an ultrasonic sensor to land a quadcopter drone by sending PWM signals according to a PID controller.

Existing hardware - drone esc attached to motors

Ultrasonic sensor

Arduino Nano board powered via battery.

Are you sure that a single PWM signal will be sufficient for all 4 motors of the quadcopter?