neverofftheinternet
Published

Simple MPU6050 IMU + Arduino bot UPDATE

Gyro control in robots to hold direction

IntermediateFull instructions provided12,659
Simple MPU6050 IMU + Arduino bot UPDATE

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Inertial Measurement Unit (IMU) (6 deg of freedom)
Inertial Measurement Unit (IMU) (6 deg of freedom)
×1
L298N motor driver
×1
Breadboard (generic)
Breadboard (generic)
×1
DC Motor, 12 V
DC Motor, 12 V
×2
Smart car chassis
×1
11.1V Lipo battery
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Fritzing

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Multitool, Screwdriver
Multitool, Screwdriver
Tape, Foam
Tape, Foam

Story

Read more

Schematics

Connections

you can use a Breadboard too

final_breadboard_e3H2cdpFRt.jpg

Code

Gyro_controlled_Barney.ino

C/C++
#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

// Timers
unsigned long timer = 0;
float timeStep = 0.01;

// Pitch, Roll and Yaw values
float pitch = 0;
float roll = 0;
float yaw = 0;


int led = 13;
int ena = 5;
int enb = 6;
int ina = 7;
int inb = 4;
int inc = 9;
int ind = 8;




void setup() 
{
  Serial.begin(115200);

  // Initialize MPU6050
  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(500);
  }
  
  // Calibrate gyroscope. The calibration must be at rest.
  // If you don't want calibration, comment this line.
  mpu.calibrateGyro();

  // Set threshold sensivty. Default 3.
  // If you don't want use threshold, comment this line or set 0.
  mpu.setThreshold(3);
pinMode(led, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(enb, OUTPUT);
pinMode(ina, OUTPUT);
pinMode(inb, OUTPUT);
pinMode(inc, OUTPUT);
pinMode(ind, OUTPUT);

delay(5000); // use the delay to place barney on the floor
}

void loop()
{
  timer = millis();

  // Read normalized values
  Vector norm = mpu.readNormalizeGyro();

  // Calculate Pitch, Roll and Yaw
  pitch = pitch + norm.YAxis * timeStep;
  roll = roll + norm.XAxis * timeStep;
  yaw = yaw + norm.ZAxis * timeStep;

  // Output raw
  Serial.print(" Pitch = ");
  Serial.print(pitch);
  Serial.print(" Roll = ");
  Serial.print(roll);  
  Serial.print(" Yaw = ");
  Serial.println(yaw);

  // Wait to full timeStep period
  delay((timeStep*1000) - (millis() - timer));

  if ( yaw == 0 ) {
        digitalWrite(ina, HIGH);
        digitalWrite(inb,HIGH);
        analogWrite(ena, 0);
        digitalWrite(inc,HIGH);
        digitalWrite(ind,HIGH);
        analogWrite(enb, 0); 
        digitalWrite(led, HIGH);
  }
  else {
    if ( yaw < 0 ) {
        digitalWrite(ina, HIGH);
        digitalWrite(inb,LOW);
        analogWrite(ena, 80);
        digitalWrite(inc,LOW);
        digitalWrite(ind,HIGH);
        analogWrite(enb, 80); 
        digitalWrite(led, LOW);
  }

  else {
        digitalWrite(ina, LOW);
        digitalWrite(inb,HIGH);
        analogWrite(ena, 80);
        digitalWrite(inc,HIGH);
        digitalWrite(ind,LOW);
        analogWrite(enb, 80); 
         digitalWrite(led, LOW);
  }
    
  }
}

Credits

neverofftheinternet

neverofftheinternet

0 projects • 21 followers

Comments