loading

ADXL345 accelerometer 2 servo motor control application.

Step 1: Arduino Code

// ADXL345 accelerometer 2 servo motor control

#include <Wire.h>

#include <Servo.h>

Servo myservox;

Servo myservoy;

#define DEVICE (0x53)

#define TO_READ (6)

#define TRIGGER 16 byte buff[TO_READ] ;

int i;

void setup()

{ pinMode(TRIGGER,OUTPUT);

digitalWrite(TRIGGER,HIGH);

Wire.begin();

myservox.attach(9);

myservoy.attach(10);

myservox.write(90);

delay(25);

myservoy.write(90);

delay(25);

writeTo(DEVICE, 0x2D, 0);

writeTo(DEVICE, 0x2D, 16);

writeTo(DEVICE, 0x2D, 8);}

void loop()

{ int regAddress = 0x32;

int x, y, z; digitalWrite(TRIGGER,LOW);

delay(10); digitalWrite(TRIGGER,HIGH);

readFrom(DEVICE, regAddress, TO_READ, buff);

x=0; y=0; z=0;

for(i=1;i<=5;i++)

{ x += (((int)buff[1]) << 8) | buff[0];

y += (((int)buff[3])<< 8) | buff[2];

z += (((int)buff[5]) << 8) | buff[4];

delay(10);

}

x/=5;

y/=5;

z/=5;

if(x<-255)x= -255;

else if (x>255)x=255;

if(y<-255)y= -255;

else if (y>255)y=255;

x=map(x, -255, 255, 0, 180);

y=map(y, -255, 255, 0, 180);

myservox.write(x);

delay(25);

myservoy.write(180-y);

delay(25);

delay(200);}

void writeTo(int device, byte address, byte val)

{ Wire.beginTransmission(device);

Wire.write(address); Wire.write(val);

Wire.endTransmission(); }

void readFrom(int device, byte address, int num, byte buff[])

{ Wire.beginTransmission(device);

Wire.write(address);

Wire.endTransmission();

Wire.beginTransmission(device);

Wire.requestFrom(device, num);

int i = 0;

while(Wire.available())

{

buff[i] = Wire.read(); i++;

}

Wire.endTransmission();

}

<p>I keep getting code crash at this</p><p>&quot;#define TRIGGER 16 byte buff[TO_READ] ;&quot;</p><p>Can you help, Im using and ADXL335 but its my code thats crashing.</p>
<p>hi i'm adi from indonesia</p><p>i have a project to control two servo motor in accordance to yaw and roll, and i use mpu6050 as a sensor </p><p>can you plese help me ?</p>
Hey i want to control two stepper motor in accordance to yaw n pitch of gyroscope .can u please help me?
<p>i want to control single servo through single axis (x axis)</p><p>what codes should i use . I am using arduino atmega328p</p><p>i used this codes it showed me this error .</p><p>can you please send codes </p>
<p>this mpu6050 servo control &gt;&gt; <a href="http://www.robimek.com/ivmeolcer-ile-bluetooth-uzerinden-servo-kontrol/">http://www.robimek.com/ivmeolcer-ile-bluetooth-uze...</a> </p>
<p>Great project :)</p><p>Try <a href="http://circuito.io" rel="nofollow">circuito.io</a> to generate a schematic and code for this project.</p>
<p>Can I use the motion along the mpu6050 to control servo?</p>
<p>this code </p><p><a href="https://www.instructables.com/files/orig/FLY/GZAL/IDIN48PC/FLYGZALIDIN48PC.ino">https://www.instructables.com/files/orig/FLY/GZAL/I...</a></p>
<p>It's still for roll and pitch. Can you help me out with the yaw movement of mpu6050 to control servo?</p>
<p>this project mpu6050 contolled http://www.robimek.com/ivmeolcer-ile-bluetooth-uzerinden-servo-kontrol/</p>
<p>I am talking about this : https://www.youtube.com/watch?v=7HLnzeH4hH0</p>
<p>Hi, Do you have a circuit diagram?</p><p>Thanks</p>
Hello are you using a 90 or 180 degree servo.
<p>Hello, <a href="https://www.instructables.com/member/gandrew4/" rel="nofollow" style="">gandrew4</a> MPU6050 gyro sensor 2 axis servo control code:</p>
Hello are you using the 6050 gyro.

About This Instructable

14,277views

36favorites

License:

Bio: Mechanical engineer, Entrepreneur, Maker, robotic systems expert and Robimek founder
More by sezgingul:Arduino Mini CNC  Making Robot Hand With  Plastic Pipes Use of Microphone Module 
Add instructable to: