Pan and tilt control with 2 servo motors

Hi,
I'm trying to use 2 servo motors (Tower Pro SG-5010) in a pan and tilt kit using the Arduino Uno with the motor driver shield L293D. Below is the code I'm currently using that I retrieved off a similar project with the servo motors connected to pins 9 & 10 on the L293D:
( Charles' Labs - 3D Lidar Scanner )

But, when using the below code only the yaw servo motor seems to be moving 180° in a continuous loop. I've checked both pins (9 & 10) with the example servo sweep code and they to work with no issue individually.

The goal is two be able to yaw sweep 180° and back to 0° then move up 1° in pitch - to a maximum of 45° pitch angle. This would enable me to attach a LiDAR scanner later on and scan a small slope.

Any help would be greatly appreciated!

#include <Servo.h>
#include <Wire.h>

//Delay between each sample to avoid mechanical wobble
#define DELAY_BETWEEN_SAMPLES 100
//Size of the steps of YAW/PITCH in degrees (1 = full res)
#define YAW_STEP 1
#define PITCH_STEP 1
#define MATH_PI 3.1415f

//Variables

Servo servoYaw,servoPitch;
char s[15];
int yawAngle,pitchAngle;

void setup() {
  // Initialize serial connection to display distance readings
  Serial.begin(115200); 

  //Servo init
  servoYaw.attach(10);
  servoPitch.attach(9);

  
}

void loop() {
  delay(5000);
  //Sweep Yaw servomotor
  for (yawAngle = 0; yawAngle <= 180; yawAngle += YAW_STEP) {
    servoYaw.write(yawAngle);
    
    //Sweep Pitch servomotor. The direction depends on the current directory
    if(pitchAngle < 90){
      for (pitchAngle = 0; pitchAngle <= 180;pitchAngle+= PITCH_STEP){
        servoPitch.write(pitchAngle);
      
      }
 
       
      }
    }
  }

Servos need an external power supply. A 4 AA battery pack is a popular servo supply.

What happens if attach the servos directly to pins 9 and 10, bypassing the shield?

Thanks for the reply - I connected an external power supply but, I'm still experiencing the same issue.
I also originally tried without using the shield and had the same problem. Not sure what to try next?

You never do any reversing. So yaw sweeps to 180 then jumps back to 0 and starts again. And after the first 1 degree yaw move pitch will sweep to 180 so after that pitchAngle is 180 (i.e. not < 90) so it never moves again. I can't see any mention of 45 anywhere.

Steve

This pitch sweep will only happen once. After that, 'pitchAngle' is 181 so the "if(pitchAngle < 90)" is never true again.

You should put the pitch loop AROUND the yaw loop so it does a yaw scan at each pitch:

  // The goal is two be able to yaw sweep 180° and
  // back to 0° then move up 1° in pitch - to a 
  // maximum of 45° pitch angle. 
  for (pitchAngle = 0; pitchAngle <= 45; pitchAngle+= PITCH_STEP)
  {
    servoPitch.write(pitchAngle);

    //Sweep Yaw servomotor
    for (yawAngle = 0; yawAngle <= 180; yawAngle += YAW_STEP) 
    {
      servoYaw.write(yawAngle);
      // Take sample?
    }
  }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.