Project tutorial
Simple MPU6050 IMU + Arduino bot UPDATE

Simple MPU6050 IMU + Arduino bot UPDATE

Gyro control in robots to hold direction

  • 2,893 views
  • 8 comments
  • 24 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)
26w6260 40
Multitool, Screwdriver
5165950
Tape, Foam

Apps and online services

About this project

Using a Gyro to control my projects was a big thing on my bucket list but apart from acquiring the IMU the rest was hard as nails. The lack of effective content on extracting yaw pitch and roll values bugged me for over a month. After numerous websites, countless libraries and problems I learned to get data from the gyro and use it in a simple project that beginners can easily do and save themselves a lot of trouble.

"It's gonna be LEGENDARY" - Swarley

(Check out our blog - virginrobotics.blogspot.com for amazing content.)

Follow these steps to make use of the gyro angles from the MPU6050 sensor.

1. After getting yourself the 6-axis accelerometer-gyroscope sensor, there's a library to install.

Here's the link :

MPU6050 library

https://github.com/jarzebski/Arduino-MPU6050

2. On installing the library go ahead and wire up the system as follows

The connections to the motor driver are given in the code itself.

ena = 5;

enb = 6;

in1 = 7;

in2 = 4;

in3 = 9;

in4 = 8;

here anyway :)

The connections to the sensors are :

VCC - +5V

GND - GND

SDA - A4

SCL - A5

Note - From here on we call the robot Barney.

3. Upload the code and place your Barney on the floor to let the sensor calibrate

and set the orientation you placed it in as 0 degrees.

Time for Fun kids.....

Once it's done that, Barney may twitch side to side but it's fine. Now try and shove the bot in different directions but it will return to its original orientation.

Here's Barney demonstrating.

UPDATE--

As you can see the bot starts twitching once it returns to facing the wall. The problem has been fixed and to see how follow this link - VirginRobotics

Here's a video showing the difference the fix makes . Notice how the bot doesn't twitch after returning to face the wall.

the fix

Code

Gyro_controlled_Barney.inoC/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);
  }
    
  }
}

Schematics

Connections
you can use a Breadboard too
connections_HZG7aJfqk3.fzz
final_breadboard_e3H2cdpFRt.jpg
Final breadboard e3h2cdpfrt

Comments

Similar projects you might like

Trash Built BT Line Drawing Bot - My Bot

Project tutorial by jegatheesan

  • 748 views
  • 0 comments
  • 7 respects

Bluetooth Controlled Bot (Using Adafruit L293D Motor Driver)

Project showcase by Chandran N

  • 2,618 views
  • 0 comments
  • 14 respects

Simple Arduino Digital Clock Without RTC

Project in progress by Annlee Fores

  • 120,270 views
  • 32 comments
  • 107 respects

Visualising 3D Motion of IMU sensor

Project tutorial by Aritro Mukherjee

  • 34,730 views
  • 7 comments
  • 44 respects

Home Safety Bot

Project showcase by Team HomeSafety

  • 3,411 views
  • 3 comments
  • 18 respects

Obstacle Avoidance Bot Using IR Sensors

Project tutorial by Chandran N

  • 3,337 views
  • 0 comments
  • 11 respects
Add projectSign up / Login