The world of Robotics is progressing by the minute. It's touching our lives in different ways every now and then. The ease of learning this skill has enabled people to be innovative and create new things daily. One such bot constructed is the self-balancing bot which works on the principle of the inverted pendulum. It balances itself by counterbalancing the fall of the bot using motors, sensors, and Arduino primarily.
To keep the structure in an upright position a feedback and a correcting element is essential. The feedback element is the gyroscope and accelerometer also known as the Inertial Sensor Unit collectively, gives both acceleration and rotation in all three axes which is used by the Arduino to know the current orientation of the robot. The actuator block which drives the correcting element i.e. the motors and wheels has two drivers that receive the analog voltage signal from a controller and produce the current signal to drive the motors at the desired torque. As the robot tilts, a restoring force is applied by rotating the wheels in the direction opposite to the tilt to return the robot to the vertical position.
Arduino Uno, accelerometer+gyroscope module MPU6050, a mini breadboard and driver module L293D all rest on the top of the chassis while the motors are connected to the wheels. It draws power from a 9 Volt battery.
![](https://static.wixstatic.com/media/a27d24_f2457280a231430abe2c5e8689c4ab1e~mv2.jpg/v1/fill/w_980,h_690,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/a27d24_f2457280a231430abe2c5e8689c4ab1e~mv2.jpg)
Coding
#include<Wire.h>
//#include<Stepper.h>
long ax,ay,az;
float afx,afy,afz;
long gyx,gyy,gyz;
float rx,ry,rz;
//const int stepsperrev=200;
//Stepper mystepper(stepsperrev,4,5,6,7);
void setup() {
Serial.begin(9600);
Wire.begin();
setMPU();
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(13,OUTPUT);
//mystepper.setSpeed(60);
}
int ch;
void loop() {
ch=Serial.read();
if(ch==1)
{ digitalWrite(13,HIGH);
Serial.print("1");
}
rar();
//rgr();
//pd();
check();
}
void setMPU()
{ Wire.beginTransmission(0b1101000);
Wire.write(0x6B);
Wire.write(0b00000000);
Wire.endTransmission();
Wire.beginTransmission(0b1101000);
Wire.write(0x1B);
Wire.write(0x00000000);
Wire.endTransmission();
Wire.beginTransmission(0b1101000);
Wire.write(0x1C);
Wire.write(0b00000000);
Wire.endTransmission();
}
void rar()
{ Wire.beginTransmission(0b1101000);
Wire.write(0x3B);
Wire.endTransmission();
Wire.requestFrom(0b1101000,6);
while(Wire.available()<6);
ax=Wire.read()<<8|Wire.read();
ay=Wire.read()<<8|Wire.read();
az=Wire.read()<<8|Wire.read();
pad();
}
void pad()
{
afx=ax/16384.0;
afy=ay/16384.0;
afz=az/16384.0;
}
void rgr()
{ Wire.beginTransmission(0b1101000);
Wire.write(0x43);
Wire.endTransmission();
Wire.requestFrom(0b1101000,6);
while(Wire.available()<6);
gyx=Wire.read()<<8|Wire.read();
gyy=Wire.read()<<8|Wire.read();
gyz=Wire.read()<<8|Wire.read();
pgd();
}
void pgd()
{ rx=gyx/131.0;
ry=gyy/131.0;
rz=gyz/131.0;
}
void fwstep()
{ Serial.println("Forword");
digitalWrite(6,0);
digitalWrite(7,1);
digitalWrite(8,0);
digitalWrite(9,1);
}
void revstep()
{ Serial.println("Backward");
digitalWrite(6,1);
digitalWrite(7,0);
digitalWrite(8,1);
digitalWrite(9,0);
}
void stops()
{
digitalWrite(6,0);
digitalWrite(7,0);
digitalWrite(8,0);
digitalWrite(9,0);
}
void pd()
{
Serial.print("Y = ");
Serial.println(afy);
delay(1000);
}
void check()
{int ton;
if(afy<0)
ton=afy*22*-1;
else
ton=afy*22;
int tonc=ton*2;
int toff=12-ton;
if(toff<0)
toff=0;
if(afy<=-0.07)
{ revstep();
delay(tonc-tonc/5);
}
else if(afy>=0.07)
{ fwstep();
delay(tonc);
}
stops();
// delay(toff);
}
![](https://static.wixstatic.com/media/a27d24_ef5ec57a02824dbe85d5bd597856946b~mv2.jpg/v1/fill/w_900,h_1600,al_c,q_85,enc_auto/a27d24_ef5ec57a02824dbe85d5bd597856946b~mv2.jpg)