Monday, April 3, 2017

Arduino Pan-Tilt controller

I'm working on a project to make an RC controller from Arduino parts and picked up a previously unfinished project where I was wanting to use servo's to control a Raspberry Pi camera with a pan-tilt mechanism hookup up to some micro-servos.

Parts :


  • Arduino Board - I used the UNO in this example.
  • Joystick or 2 potentiometers.
  • Pan-Tilt head
  • 2 Sub-micro servos.
  • Connecting wires

Connections

  1. Connect the center of each pot to the inputs (A0 and A1). 
  2. Connect the other sizes to of the pot to V+ and Gnd.
  3. Connect the orange signal wire of the servos to D9 and D10. 
  4. Connect the red servo wire to V+. It is best to connect to a separate power supply if you are using larger servos and/or expect a load on the servos. 
  5. Connect the brown servo wires to ground. These should be connected to the same ground as the Arduino controller.

Code:

Required Software Libraries:

  • Servo.h

Source Code:

UNO
/*
  Pan-Tilt controller
  By Aaron Woehler 3 Apr 2017

  Transform 2 analog inputs into 2 pwm output to control pan-tilt servos.

Pins:
Output
D9 - Servo0
D10 - Servo1

Input:
A0 - Center pin of potentiometer 0
A1 - Center pin of potentiometer 0

 */
#include

int sensorPin0 = A0;    // select the input pin for the potentiometer
int sensorPin1 = A1;    // select the input pin for the potentiometer

int sensorValue = 0;  // variable to store the value coming from the sensor
Servo servo0;  // create servo object to control a servo
Servo servo1;  // create servo object to control a servo

int mapped;
void setup() {
  myservo0.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo1.attach(10);  // attaches the servo on pin 10 to the servo object
}

void loop() {
    // read the value from the sensor:
  sensorValue = analogRead(sensorPin0);
    //Adjust the read value which is in the range from 0 to 1024 to degrees 0 through 180.
  mapped = map( sensorValue, 0, 1024, 1, 180 );
    // tell servo to go to position in variable 'pos'
  myservo0.write(mapped);              

  sensorValue = analogRead(sensorPin1);
  mapped = map( sensorValue, 0, 1024, 1, 180 );
  myservo1.write(mapped);
  //stop the program for 50 milliseconds:
  delay(50);
}


No comments: