2 Axis Servo Project

This is a little project that I worked on that involves using servos in a 2 axis setup.  This is a really cheap project that outlines some of the great things that you can do with a few servo motors.  This setup can be used to swivel a small camera on 2 axis.  We used it to create a Lego tilt-a-whirl.

Materials

  • Computer (to upload Arduino code)
  • Arduino or Arduino compatible device (between $2 and $20 depending on the device you purchase)
  • 2x SG90 Servo motors.  ($1 each on eBay)
  • 2-Axis SG90 Gimbal Bracket for SG90 servos (pic below) ($1.80 on eBay)
  • Male to Male jumper wires ($1.50 on eBay for 40 pack)

Wiring

The red wires for both servos will connect to the 3.3v pin on the Arduino.  The brown/black wires for both servos will connect to ground on the Arduino.  The orange wire for the bottom servo will connect to pin 9.  The orange wire for servo the top servo will connect to pin 10.  The Arduino will get power from the USB port from either your computer or from an optional USB battery bank.

Code

I will build a few variants that you can use to upload and test.  You can use these an example to get you started working with this setup for your own projects.  These servos typically rotate 180 degrees and you will see these positions used in the code to point the servo where we need them.

 

Project 1 – Swivel/Tilt

I attached a little Lego man at the top so that you can see the tilt from the second servo motor.   The top motor tilts at 45 degrees and the bottom motor does a 180 degree swivel before both return home.  Video and code below.

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

void setup() {
 myservo1.attach(9); // attaches the servo on pin 9 to the servo object
 myservo2.attach(10); // attaches the servo on pin 9 to the servo object
}
void loop() {
 myservo1.write(0); //moves bottom servo to position 0
 myservo2.write(0); //moves top servo to position 0
 delay(800); //Delays were added to give the motors time to complete the action 
 
 myservo1.write(180); //turns bottom servo 180 degrees
 myservo2.write(45); //turns top servo 45 degrees to tilt down
 delay(800);
 
 myservo1.write(0); //moves bottom servo back to position 0
 myservo2.write(0); //moves top servo back to postion 0
 delay(800); 
 }

 

Project 2 – Randomized Motor Position

I use similar code as above but I changed it so that servo positions were picked at random.  I set the bottom servo to 180 degrees and I limited the top servo to 90 because the Lego man would not survive otherwise.

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

void setup() {
 myservo1.attach(9); // attaches the servo on pin 9 to the servo object
 myservo2.attach(10); // attaches the servo on pin 9 to the servo object
}
void loop() {
 myservo1.write(random(0,180)); //Randomly moves the bottom servo up to 180 degrees
 myservo2.write(random(0,90)); //Randomly moves the top servo up to 90 degrees. 
 delay(800); 
 }

Project 3 – Crazy Randomizer

This code is similar to project #2.  I turned the delay down and limited the positions.  Chaos ensues for Lego Man.

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

void setup() {
 myservo1.attach(9); // attaches the servo on pin 9 to the servo object
 myservo2.attach(10); // attaches the servo on pin 9 to the servo object
}
void loop() {
 myservo1.write(random(45,180));
 myservo2.write(random(0,80)); 
 delay(75); 
 }

 

Project 4 – Pencil Precision

I removed Lego man and added a pencil so you can see the full range and motion with attached devices. You can use loops and other forms of control to create slashing movements or slowly pan one position at a time.

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

void setup() {
 myservo1.attach(9); // attaches the servo on pin 9 to the servo object
 myservo2.attach(10); // attaches the servo on pin 9 to the servo object
}
void loop() {
 myservo1.write(0);
 myservo2.write(0); 
 delay(750); 
 myservo1.write(180);
 myservo2.write(180); 
 delay(750); 
 myservo1.write(0);
 myservo2.write(0); 
 delay(750); 
 myservo2.write(180);
 delay(800); 
 myservo2.write(0);
 delay(800); 
 myservo1.write(90);
 myservo2.write(90); 
 delay(800); 
 myservo1.write(180);
 delay(800); 
 myservo2.write(45);
 delay(800); 
 myservo2.write(135);
 delay(800); 
 myservo2.write(0);
 delay(800); 
 }

Leave a Reply