Gyroscope Controlled Robot (Wired)
Gyroscopes are used in mostly all devices. The most common example is the smartphones. The Gyro sensors are used to sense the angular velocity and using that data we can control the movement of the robot. Unlike accelerometers which measure linear acceleration, the Gyroscope sensors adds an additional dimension to the accelerometer thus allowing us to measure the angular velocity.
Today we will learn to make a Gyroscope controlled robot using Arduino and GY521 Gyroscope sensor.
Working :
The gyroscope sensor is also known as the tilt sensor. It senses its movements and provides the values to the arduino. The gyroscope module works in all the three axis namely the x,y and z. When the arduino receives these values it uses them with the if else statements to drive the motors accordingly.
This project is a wired project. i.e. The Arduino and Gyro sensor is connected to each other using wires. This project can be used for surveillance and the only disadvantage with this project is its wired connections.
Components used in this project :
1. Arduino Uno.
2. GY-521 MPU-6050 Gyroscope sensor.
3. Motor Driver Module.
4. Metal chassis and wheels.
5. Connecting wires.
6. Double sided tape.
7. Batteries or adapter to power the board.
6. Double sided tape.
7. Batteries or adapter to power the board.
Circuit Diagram :
The circuit diagram for this project is shown below. Do all the connections accordingly.
You can also download the circuit diagram by clicking HERE .
Place all the components on metal chassis and fix them with the help of some double sided tape.
Here are some screen shots of the project.
Now connect the Arduino to your PC and upload the code.
CODE :
#include<Wire.h>
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
const int trigPin=13;
//Motor A
const int motorPin1 = 5;
const int motorPin2 = 6;
//Motor B
const int motorPin3 = 10;
const int motorPin4 = 9;
void setup()
{
pinMode(trigPin,OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
Serial.print("AcX = "); Serial.print(AcX);
Serial.print(" | AcY = "); Serial.print(AcY);
Serial.print(" | AcZ = "); Serial.print(AcZ);
Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);
Serial.print(" | GyX = "); Serial.print(GyX);
Serial.print(" | GyY = "); Serial.print(GyY);
Serial.print(" | GyZ = "); Serial.println(GyZ);
if(AcX<-6000&&-11000)
{
digitalWrite(trigPin,HIGH);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);//FORWARD
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
else if(AcX>4000&&6000)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);//REVERSE
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
else if(AcY<-7000&&-5000)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);//LEFT
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
else if(AcY>5000&&12000)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);//RIGHT
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
else
{
digitalWrite(trigPin,LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);//STOP
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
delay(333);
}
You can also download the code from HERE .
After this power the Arduino and Motor driver and that's it. Your project is ready to be tested.
You can also download the circuit diagram by clicking HERE .
Place all the components on metal chassis and fix them with the help of some double sided tape.
Here are some screen shots of the project.
Now connect the Arduino to your PC and upload the code.
CODE :
#include<Wire.h>
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
const int trigPin=13;
//Motor A
const int motorPin1 = 5;
const int motorPin2 = 6;
//Motor B
const int motorPin3 = 10;
const int motorPin4 = 9;
void setup()
{
pinMode(trigPin,OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
Serial.print("AcX = "); Serial.print(AcX);
Serial.print(" | AcY = "); Serial.print(AcY);
Serial.print(" | AcZ = "); Serial.print(AcZ);
Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);
Serial.print(" | GyX = "); Serial.print(GyX);
Serial.print(" | GyY = "); Serial.print(GyY);
Serial.print(" | GyZ = "); Serial.println(GyZ);
if(AcX<-6000&&-11000)
{
digitalWrite(trigPin,HIGH);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);//FORWARD
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
else if(AcX>4000&&6000)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);//REVERSE
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
else if(AcY<-7000&&-5000)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);//LEFT
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
else if(AcY>5000&&12000)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);//RIGHT
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
else
{
digitalWrite(trigPin,LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);//STOP
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
delay(333);
}
You can also download the code from HERE .
After this power the Arduino and Motor driver and that's it. Your project is ready to be tested.
No comments:
Post a Comment