0% found this document useful (0 votes)
76 views2 pages

DC Motor Control with MPU6050 Code

The document contains Arduino code for controlling two DC motors using an MPU6050 sensor. It initializes the sensor, reads motion data, and adjusts motor speeds based on the accelerometer's x-axis values. The code includes two separate implementations for motor control, each using different pin configurations.

Uploaded by

Emanuel Zalazar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views2 pages

DC Motor Control with MPU6050 Code

The document contains Arduino code for controlling two DC motors using an MPU6050 sensor. It initializes the sensor, reads motion data, and adjusts motor speeds based on the accelerometer's x-axis values. The code includes two separate implementations for motor control, each using different pin configurations.

Uploaded by

Emanuel Zalazar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Codigo para Motor DC y MPU6050

#include <Wire.h>
#include<I2Cdev.h>
#include<MPU6050.h>

MPU6050 mpu;

int16_t ax, ay, az;


int16_t gx, gy, gz;

#define pin1 3
#define pin2 5

void setup(){
[Link](9600);
[Link]("Initialize MPU");
[Link]();
//[Link]([Link]() ? "Connected" : "Connection failed");
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);

void loop(){
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

ax = map(ax, -17000, 17000, -1500, 1500);

//[Link](ax);
if(ax > 0){
if(ax<255){
[Link](ax);
analogWrite(pin2,ax);
}
else{
[Link]("+255");
analogWrite(pin2,255);
}

}
if(ax<0){
if(ax>-255){
[Link](ax);
analogWrite(pin1, ax-ax-ax);
}
else{
[Link]("-255");
analogWrite(pin1, 255);
}
}
delay(1000);
}
#include <Wire.h>
#include <MPU6050.h>

#define first_motor_pin1 5
#define first_motor_pin2 6
#define second_motor_pin1 9
#define second_motor_pin2 10

MPU6050 sensor;
int16_t ax, ay, az;
int16_t gx, gy, gz;

int first_motor_speed;
int second_motor_speed;

void setup ( )
{
[Link]( );
[Link] (9600);
[Link] ("Initializing the sensor");
[Link] ( );
[Link]([Link]( ) ? "Successfully Connected" :
"Connection failed");
delay(1000);
[Link]("Taking Values from the sensor");
delay(1000);
}

void loop ( )
{
sensor.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
ax = map(ax, -17000, 17000, -125, 125);
first_motor_speed = 125+ax;
second_motor_speed = 125-ax;

[Link] ("Motor1 Speed = ");


[Link] (first_motor_speed, DEC);
[Link] (" && ");
[Link] ("Motor2 Speed = ");
[Link] (second_motor_speed, DEC);

analogWrite (first_motor_pin2, first_motor_speed);


analogWrite (second_motor_pin2, second_motor_speed);
delay (200);
}

You might also like