Problem with Servo and IMU

I am trying to get data from a SparkFun 5 DOF IMU (Accelerometer and Gyro) while controlling a servo with a potentiometer. Eventually I want to control the servo using the data from the IMU, but for now I just want to get past this simpler problem.

I connected the Arduino as shown below. I get clean signals from the IMU but the servo jitters and hunts. If I remove this line: analogReference(EXTERNAL); the servo works great but I get lots of noise on the IMU outputs.

If I remove this line: master.attach(5); then I get a clean signal again but the servo doesn’t move.

This is my first program and I can’t seem to figure out what’s missing or what’s wrong. Maybe it has nothing to do with the above lines, or maybe it is the connections I made between the components.

Thanks for your help!

// Connections:

// Potentiometer on Pin A0
// IMU X-rate to Pin A1
// IMU Y-Acc to Pin A2
// IMU VREF to 3.3V
// IMU RAW to 5V

// Master servo on Pin 5

// AREF to 3.3V


#include <Servo.h>
Servo master;  // Create a servo object for master servo

int IMUA = 2; // Pin for IMU accelerometer
int angle; // Variable for IMU accelerometer
int IMUG = 1; // Pin for IMU gyro
int rate;  // Variable for IMU gyro
int potpin = 0; // Pin for potentiometer
int pot; // variable for potentiometer

void setup()
{
  master.attach(5); //Attaches the master servo on Pin 5 to the servo object !!!!!!!!!!Problem
  Serial.begin(9600);      // open the serial port at 9600 bps:
  analogReference(EXTERNAL); //use 3.3V from the AREF pin for the IMU !!!!!!!!!!! Problem
}

void loop()
{
  angle = analogRead(IMUA)-425;  //Read the accelerometer from the A2 pin
  rate = analogRead(IMUG)-789;  // Read the gyro from the A1 pin
  
  Serial.print("Accelerometer:  ");
  Serial.print(angle);
  Serial.print("   Gyro:  ");
  Serial.println(rate);
  
  pot = analogRead(potpin);  //  Read the pot value
  pot = map(pot, 0, 1023, 0, 180); // Map the values from 0 to 180 degrees
  master.write(pot); // Write the angle to the master servo
  delay(50); // to allow servo to reach position

}

Hi,
It is possibly down to power, check the two links in my signature for why you need separate servo power and a demonstration of what happens if you don't have it.

Duane B

rcarduino.blogspot.com

Thanks! That was the whole problem.