Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Quadcopter with IR sensors (Obstacles avoidance)

Newbie89Newbie89 Club Members Posts: 11
Hi all,

I'm currently working on a project on quadcopter with obstacles avoidance capability, I am using a KK2.0 flight controller, Arduino Uno board and sharp infrared sensor (Kingduino GP2D12).

The IR sensor measures the distance between the sensor itself and the object and sends it to the arduino in the form of analog signal (value range: 0 to 1023). We should make a setpoint say 30cm from the IR sensor so if the setpoint reaches below 30cm the PID algorithm in the arduino will start its work by sending out the correction value to the stabilization board (kk2.0).

I came across a similar project done by cyril.anthony777 online but have some queries on the codes he shared. The following is the code,


Servo aileron,elevator;

int setpoint=60,Kp=3; // Kp is (present error) Tuning Parameter

void setup()






void loop()



int left,right,front,back;

int d1,d2,d3,d4;

int temp,temp1,temp2,temp3;

int e1,e2,e3,e4;

int op1,op2,op3,op4,opx,opy;





for(int i=0;i<10;i++)


    left = analogRead(A0);





for(int i=0;i<10;i++)


    right = analogRead(A1);





for(int i=0;i<10;i++)


    front = analogRead(A2);





for(int i=0;i<10;i++)


    back = analogRead(A3);


















Question1: How is the Setpoint value of 60 and its Min of 20 and Max of 150 obtained?

Question2: Is Kp refering to the present error (Tuning Parameter) and how is it derived?

Question3: What is this op1=e1*Kp? why must the e1 value (difference between setpoint and actual distance) by multiplied by Kp?

Question4: And lastly opx=1500-op1+op2, what is is about? I understand that the aileron (Roll) control is affected by the left and right pairs of motors and elevator (pitch) control is affected by the front and rear pairs of motors. Is the 1500 refering to the 1500us which is the center or neutral value for roll pitch and yaw?

It would be great if any guru out there can help me with this, thanks in advance! Peace


  • TheQuestorTheQuestor TXClub Members Posts: 629
    since the data sheet shows it has a max of 80 cm they probably set the max to 60 in the sketch as anything beyond that gets sketchy.

    It's not much better than the horrible cheap sonar [SR04] people have tried to use. That said I just ordered one on ebay for 5 bucks to play with.

  • WTWUKWTWUK CornwallClub Members Posts: 13,511 ✭✭✭
    Those horrible SR-04's work just fine - provided they get a good voltage supply. I ran one on my old MultiWii Pro and got some pretty accurate flights with it BUT only when I powered it with 5V direct supply. Anything lower and it was indeed awful.

    WTWUK2016-05-31 23:03:18
  • Newbie89Newbie89 Club Members Posts: 11
    Hi TheQuestor,

    sounds quite logical, according to his mapping code, I'm assuming the minimum detection range of the sensor is 20cm and maximum range is 150cm. The setpoint is declared as 60 (cm) because he intend to make a setpoint of 40cm from the IR sensor so if the setpoint reaches below 40cm the PID algorithm in the arduino will start its work by sending out the correction value to the stabilization board (kk2.0 flight controller).

    But of what I know the code author is using a Sharp GP2Y0A21YK, detection distance is also 10cm to 80cm, not sure how does he come out with the 20cm to 150cm.

    Thanks for helping image

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file