Close

Mit Servo

A project log for Arduino Skillz

Week of Arduino

ken.doken.do 01/26/2017 at 15:260 Comments

Adding a servo to any project makes it instantly cool. It's when code reaches out and touches the real world. Building on my last project, the MPU-6050, I have conneted to a servo and a change in attitude results in a change in servo position.

I noticed some jitter, haven't yet figured out how to quell it yet. I tried using a capacitor across the servo power to no avail. Will look into other options and post results here.
The positive and negative wires with no connection are actually connected to an external power source (5V). When activated connected to the 5V pin, the servo killed the Arduino, probably because of excessive current draw on servo start. I am using an 3S battery running a 5V UBEC. Should be sufficient to run a number of servos.

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Servo.h"
 
MPU6050 mpu;
 
int16_t ax, ay, az;
int16_t gx, gy, gz;
 
Servo myservo;
 
int val;
int prevVal;
 
void setup() 
{
    Wire.begin();
    Serial.begin(38400);
 
    Serial.println("Initialize MPU");
    mpu.initialize();
    Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
    myservo.attach(9);
}
 
void loop() 
{
    mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
 
    val = map(ay, -17000, 17000, 0, 179);
    if (val != prevVal)
    {
        myservo.write(val);
        prevVal = val;
    }
 
    delay(50);
}

Discussions