Arduino With Accelerometer Servo Motor

21,955

44

15

Introduction: Arduino With Accelerometer Servo Motor

About: Mechanical engineer, Entrepreneur, Maker, robotic systems expert and founder of Robimek and RobiBot

ADXL345 accelerometer 2 servo motor control application.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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();

}

Step 2: Video

2 People Made This Project!

Recommendations

  • Backyard Contest

    Backyard Contest
  • Silly Hats Speed Challenge

    Silly Hats Speed Challenge
  • Finish It Already Speed Challenge

    Finish It Already Speed Challenge

15 Discussions

0
goldenshuttle
goldenshuttle

2 years ago

This is a very impressive . Did anyone see a code that makes it wireless ? that Gyro and arduino send signal wirelessly to servos ??

0
Ghloo
Ghloo

Reply 1 year ago

I will definitely be looking into it. Recently I have been thru controlling dual DC motors (l9110) over bluetooth from my cellphone and here the principle should be fairly similar. This instructable is a great base for a predator-style shoulder canon (so thanks a big bunch), but for a real gesture-controlled or motion-controlled car (I am sure googling that may give you a clue) one would need wireless arduino-to-arduino link.

0
colby.lewallen
colby.lewallen

3 years ago

I keep getting code crash at this

"#define TRIGGER 16 byte buff[TO_READ] ;"

Can you help, Im using and ADXL335 but its my code thats crashing.

0
MarsianaS
MarsianaS

Reply 3 years ago

you have to separate #define TRIGGER 16 and byte buff[TO_READ].

0
adirukun
adirukun

3 years ago

hi i'm adi from indonesia

i have a project to control two servo motor in accordance to yaw and roll, and i use mpu6050 as a sensor

can you plese help me ?

0
Bryuce
Bryuce

4 years ago

Hey i want to control two stepper motor in accordance to yaw n pitch of gyroscope .can u please help me?

0
yanais
yanais

4 years ago

Great project :)

Try circuito.io to generate a schematic and code for this project.

0
SiddharthK4
SiddharthK4

4 years ago

Can I use the motion along the mpu6050 to control servo?

0
SiddharthK4
SiddharthK4

Reply 4 years ago

It's still for roll and pitch. Can you help me out with the yaw movement of mpu6050 to control servo?

0
sezgingul
sezgingul

Reply 4 years ago

this project mpu6050 contolled http://www.robimek.com/ivmeolcer-ile-bluetooth-uzerinden-servo-kontrol/

0
SiddharthK4
SiddharthK4

Reply 4 years ago

I am talking about this : https://www.youtube.com/watch?v=7HLnzeH4hH0

0
ellyd2
ellyd2

4 years ago

Hi, Do you have a circuit diagram?

Thanks

0
Andrew Gyakobo
Andrew Gyakobo

4 years ago

Hello are you using a 90 or 180 degree servo.

0
gandrew4
gandrew4

4 years ago

Hello are you using the 6050 gyro.