Introduction: Arduino Self-Balancing Robot

Picture of Arduino Self-Balancing Robot

Hello, everyone!

In this instructable, I'll show you how to build a small self-balancing robot that can move around avoiding obstacles. This is a tiny robot measuring 4 inches wide and 4 inches tall and is based on the Arduino Pro Mini development board and the MPU6050 accelerometer-gyroscope module.

In the steps that follow, we will see how to interface the MPU6050 with Arduino, how to measure the angle of inclination of the robot, how to use PID to make the robot stay balanced. An ultrasonic rangefinder is also added to the robot which prevents it from banging into obstacles as it wanders around.

Parts List

I bought most of these parts from aliexpress but you can find them in any other electronics store as well.

1. Arduino Pro Mini

2. GY-521 module with MPU-6050

3. DRV8833 Pololu motor driver

4. 2, 5V boost converter

5. US-020 ultrasonic distance sensor

6. NCR18650 battery and holder

7. Pair of micro metal gear motors (N20, 6V, 200 rpm) and brackets

8. Pair of 42x19mm wheels

9. 3, Double-sided prototype PCB (4cm x 6cm)

10. 8, 25cm Nylon spacers and 4, nylon nuts

Apart from the above, you will need some cables, berg connectors and one on/off switch.

Step 1: A Bit of Theory

Let's start with some fundamentals before getting our hands dirty.

The self-balancing robot is similar to an upside down pendulum. Unlike a normal pendulum which keeps on swinging once given a nudge, this inverted pendulum cannot stay balanced on its own. It will simply fall over. Then how do we balance it? Consider balancing a broomstick on our index finger which is a classic example of balancing an inverted pendulum. We move our finger in the direction in which the stick is falling. Similar is the case with a self-balancing robot, only that the robot will fall either forward or backward. Just like how we balance a stick on our finger, we balance the robot by driving its wheels in the direction in which it is falling. What we are trying to do here is to keep the center of gravity of the robot exactly above the pivot point.

To drive the motors we need some information on the state of the robot. We need to know the direction in which the robot is falling, how much the robot has tilted and the speed with which it is falling. All these information can be deduced from the readings obtained from MPU6050. We combine all these inputs and generate a signal which drives the motors and keeps the robot balanced.

Step 2: Let's Start Building

Picture of Let's Start Building

We will first complete the circuitry and structure of the robot. The robot is built on three layers of perfboards that are spaced 25mm apart using nylon spacers. The bottom layer contains the two motors and the motor driver. The middle layer has the controller, the IMU, and the 5V boost regulator modules. The top most layer has the battery, an on/off switch and the ultrasonic distance sensor (we will install this towards the end once we get the robot to balance).

Before we begin to prototype on a perfboard we should have a clear picture about where each part should be placed. To make prototyping easy, it is always better to draw the physical layout of all the components and use this as a reference to place the components and route the jumpers on the perfboard. Once all the parts are placed and soldered, interconnect the three boards using nylon spacers.

You might have noticed that I've used two separate voltage regulator modules for driving the motors and the controller even though they both require a 5V source. This is very important. In my first design, I used a single 5V boost regulator to power up the controller as well as the motors. When I switched on the robot, the program freezes intermittently. This was due to the noise generated from the motor circuit acting upon the controller and the IMU. This was effectively eliminated by separating the voltage regulator to the controller and the motor and adding a 10uF capacitor at the motor power supply terminals.

Step 3: Measuring Angle of Inclination Using Accelerometer

Picture of Measuring Angle of Inclination Using Accelerometer

The MPU6050 has a 3-axis accelerometer and a 3-axis gyroscope. The accelerometer measures acceleration along the three axes and the gyroscope measures angular rate about the three axes. To measure the angle of inclination of the robot we need acceleration values along y and z-axes. The atan2(y,z) function gives the angle in radians between the positive z-axis of a plane and the point given by the coordinates (z,y) on that plane, with positive sign for counter-clockwise angles (right half-plane, y > 0), and negative sign for clockwise angles (left half-plane, y < 0). We use this library written by Jeff Rowberg to read the data from MPU6050. Upload the code given below and see how the angle of inclination varies.

#include "Wire.h"
#include "I2Cdev.h" #include "MPU6050.h" #include "math.h"

MPU6050 mpu;

int16_t accY, accZ; float accAngle;

void setup() { mpu.initialize(); Serial.begin(9600); }

void loop() { accZ = mpu.getAccelerationZ(); accY = mpu.getAccelerationY(); accAngle = atan2(accY, accZ)*RAD_TO_DEG; if(isnan(accAngle)); else Serial.println(accAngle); }

Try moving the robot forward and backward while keeping it tilted at some fixed angle. You will observe that the angle shown in your serial monitor suddenly changes. This is due to the horizontal component of acceleration interfering with the acceleration values of y and z-axes.

Step 4: Measuring Angle of Inclination Using Gyroscope

The 3-axis gyroscope of MPU6050 measures angular rate (rotational velocity) along the three axes. For our self-balancing robot, the angular velocity along the x-axis alone is sufficient to measure the rate of fall of the robot.

In the code given below, we read the gyro value about the x-axis, convert it to degrees per second and then multiply it with the loop time to obtain the change in angle. We add this to the previous angle to obtain the current angle.

#include "Wire.h"
#include "I2Cdev.h" #include "MPU6050.h"

MPU6050 mpu;

int16_t gyroX, gyroRate; float gyroAngle=0; unsigned long currTime, prevTime=0, loopTime;

void setup() { mpu.initialize(); Serial.begin(9600); }

void loop() { currTime = millis(); loopTime = currTime - prevTime; prevTime = currTime; gyroX = mpu.getRotationX(); gyroRate = map(gyroX, -32768, 32767, -250, 250); gyroAngle = gyroAngle + (float)gyroRate*loopTime/1000; Serial.println(gyroAngle); }

The position of the MPU6050 when the program starts running is the zero inclination point. The angle of inclination will be measured with respect to this point.

Keep the robot steady at a fixed angle and you will observe that the angle will gradually increase or decrease. It won't stay steady. This is due to the drift which is inherent to the gyroscope.

In the code given above, loop time is calculated using the millis() function which is built into the Arduino IDE. In later steps, we will be using timer interrupts to create precise sampling intervals. This sampling period will also be used in generating the output using a PID controller.

Step 5: Combining the Results With a Complementary Filter

Picture of Combining the Results With a Complementary Filter

Google defines complementary as "combining in such a way as to enhance or emphasize the qualities of each other or another".

We have two measurements of the angle from two different sources. The measurement from accelerometer gets affected by sudden horizontal movements and the measurement from gyroscope gradually drifts away from actual value. In other words, the accelerometer reading gets affected by short duration signals and the gyroscope reading by long duration signals. These readings are, in a way, complementary to each other. Combine them both using a Complementary Filter and we get a stable, accurate measurement of the angle. The complementary filter is essentially a high pass filter acting on the gyroscope and a low pass filter acting on the accelerometer to filter out the drift and noise from the measurement.

currentAngle = 0.9934 * (previousAngle + gyroAngle) + 0.0066 * (accAngle)

0.9934 and 0.0066 are filter coefficients for a filter time constant of 0.75s. The low pass filter allows any signal longer than this duration to pass through it and the high pass filter allows any signal shorter than this duration to pass through. The response of the filter can be tweaked by picking the correct time constant. Lowering the time constant will allow more horizontal acceleration to pass through.

Eliminating accelerometer and gyroscope offset errors
Download and run the code given in this page to calibrate the MPU6050's offsets. Any error due to offset can be eliminated by defining the offset values in the setup() routine as shown below.

mpu.setZAccelOffset(963); mpu.setXGyroOffset(40);

Step 6: PID Control for Generating Output

Picture of PID Control for Generating Output

PID stands for Proportional, Integral, and Derivative. Each of these terms provides a unique response to our self-balancing robot.

The proportional term, as its name suggests, generates a response that is proportional to the error. For our system, the error is the angle of inclination of the robot.

The integral term generates a response based on the accumulated error. This is essentially the sum of all the errors multiplied by the sampling period. This is a response based on the behavior of the system in past.

The derivative term is proportional to the derivative of the error. This is the difference between the current error and the previous error divided by the sampling period. This acts as a predictive term that responds to how the robot might behave in the next sampling loop.

Multiplying each of these terms by their corresponding constants (i.e, Kp, Ki and Kd) and summing the result, we generate the output which is then sent as command to drive the motor.

Step 7: Tuning the PID Constants

1. Set Ki and Kd to zero and gradually increase Kp so that the robot starts to oscillate about the zero position.

2. Increase Ki so that the response of the robot is faster when it is out of balance. Ki should be large enough so that the angle of inclination does not increase. The robot should come back to zero position if it is inclined.

3. Increase Kd so as to reduce the oscillations. The overshoots should also be reduced by now.

4. Repeat the above steps by fine tuning each parameter to achieve the best result.

Step 8: Adding the Distance Sensor

The ultrasonic distance sensor that I've used is the US-020. It has four pins namely Vcc, Trig, Echo, and Gnd. It is powered by a 5V source. The trigger and echo pins are respectively connected to digital pins 9 and 8 of Arduino. We will be using the NewPing library to get the distance value from the sensor. We will read the distance once every 100 milliseconds and if the value is between 0 and 20cm, we will command the robot to perform a rotation. This should be sufficient to steer the robot away from the obstacle.

Step 9: The Complete Code

#include "Wire.h"

#include "I2Cdev.h" #include "MPU6050.h" #include "math.h" #include <NewPing.h>

#define leftMotorPWMPin 6 #define leftMotorDirPin 7 #define rightMotorPWMPin 5 #define rightMotorDirPin 4

#define TRIGGER_PIN 9 #define ECHO_PIN 8 #define MAX_DISTANCE 75

#define Kp 40 #define Kd 0.05 #define Ki 40 #define sampleTime 0.005 #define targetAngle -2.5


int16_t accY, accZ, gyroX; volatile int motorPower, gyroRate; volatile float accAngle, gyroAngle, currentAngle, prevAngle=0, error, prevError=0, errorSum=0; volatile byte count=0; int distanceCm;

void setMotors(int leftMotorSpeed, int rightMotorSpeed) { if(leftMotorSpeed >= 0) { analogWrite(leftMotorPWMPin, leftMotorSpeed); digitalWrite(leftMotorDirPin, LOW); } else { analogWrite(leftMotorPWMPin, 255 + leftMotorSpeed); digitalWrite(leftMotorDirPin, HIGH); } if(rightMotorSpeed >= 0) { analogWrite(rightMotorPWMPin, rightMotorSpeed); digitalWrite(rightMotorDirPin, LOW); } else { analogWrite(rightMotorPWMPin, 255 + rightMotorSpeed); digitalWrite(rightMotorDirPin, HIGH); } }

void init_PID() { // initialize Timer1 cli(); // disable global interrupts TCCR1A = 0; // set entire TCCR1A register to 0 TCCR1B = 0; // same for TCCR1B // set compare match register to set sample time 5ms OCR1A = 9999; // turn on CTC mode TCCR1B |= (1 << WGM12); // Set CS11 bit for prescaling by 8 TCCR1B |= (1 << CS11); // enable timer compare interrupt TIMSK1 |= (1 << OCIE1A); sei(); // enable global interrupts }

void setup() { // set the motor control and PWM pins to output mode pinMode(leftMotorPWMPin, OUTPUT); pinMode(leftMotorDirPin, OUTPUT); pinMode(rightMotorPWMPin, OUTPUT); pinMode(rightMotorDirPin, OUTPUT); // set the status LED to output mode pinMode(13, OUTPUT); // initialize the MPU6050 and set offset values mpu.initialize(); mpu.setYAccelOffset(1593); mpu.setZAccelOffset(963); mpu.setXGyroOffset(40); // initialize PID sampling loop init_PID(); }

void loop() { // read acceleration and gyroscope values accY = mpu.getAccelerationY(); accZ = mpu.getAccelerationZ(); gyroX = mpu.getRotationX(); // set motor power after constraining it motorPower = constrain(motorPower, -255, 255); setMotors(motorPower, motorPower); // measure distance every 100 milliseconds if((count%20) == 0){ distanceCm = sonar.ping_cm(); } if((distanceCm < 20) && (distanceCm != 0)) { setMotors(-motorPower, motorPower); } } // The ISR will be called every 5 milliseconds ISR(TIMER1_COMPA_vect) { // calculate the angle of inclination accAngle = atan2(accY, accZ)*RAD_TO_DEG; gyroRate = map(gyroX, -32768, 32767, -250, 250); gyroAngle = (float)gyroRate*sampleTime; currentAngle = 0.9934*(prevAngle + gyroAngle) + 0.0066*(accAngle); error = currentAngle - targetAngle; errorSum = errorSum + error; errorSum = constrain(errorSum, -300, 300); //calculate output from P, I and D values motorPower = Kp*(error) + Ki*(errorSum)*sampleTime - Kd*(currentAngle-prevAngle)/sampleTime; prevAngle = currentAngle; // toggle the led on pin13 every second count++; if(count == 200) { count = 0; digitalWrite(13, !digitalRead(13)); } }

Step 10: Final Thoughts

Picture of Final Thoughts

Spending a bit more time on tweaking the PID constants would give us a better result. The size of our robot also limits the level of stability we can achieve. It is easier to build a full-sized balancing robot than it is to build a small one like ours. Still, I guess, our robot does a pretty decent job in balancing on various surfaces as shown in the video.

That's it for now.

Thanks for your time. Don't forget to leave your thoughts in the comments section.


shashis26 (author)2017-11-29


Are you sure your code will work without the above line?

Coz I checked your Angle code and it is working only if I write Wire.begin() in setup.

SergeD13 (author)2017-09-19

Hi midhun, i feel sorry to reply late to a past
message, but weirdly i didn't seen appeared on the instructable page but
in my Gmail mail box, something i seldome check i admit :) .. Anyway,
thank you for ur answer and ur explains. I understood well about why the
"255" in backward way ;) it was evident for sure.Since last
message i worked on it, and honnestly it works reallllly not bad. I
finaly found the best settings for the walue of Kp, Ki and Kp. and now i
can drive it better with the tablette. I will do a little video next
day i will send you on Gmail ;) next step, adding some sound :) I have had put again the big wheel, and thats better than with the small one. About RemoteXY, for sure u should have a look on it ! I'm sure you will find a good used to this app.
mfdonmez (author)SergeD132017-11-02

Please email me the control system code with bluetooth.
Thank you

SergeD13 (author)mfdonmez2017-11-14

sorry, i just saw ur message there.

It is not needed i send u any blutooth code because it will depends on ur own

The only thing i can tell you is to have a look on net about "Remotexy"

the best application, for my own. I'm sure u will find a way to apply to ur own robot. Thats really simple.



midhun_s (author)mfdonmez2017-11-03

I'm sorry I haven't interfaced a Bluetooth module yet. I thought I would keep my robot simple and concentrate just on the balancing part.

mfdonmez (author)midhun_s2017-11-05

Thank you midhun_s

mfdonmez made it! (author)2017-11-02

Thank you for your training. but I want help with how to control it with bluetooth.
İ did it with sg90 servo.

midhun_s (author)mfdonmez2017-11-03

Congrats! Please do share a video as well.

mfdonmez (author)midhun_s2017-11-05

LeidyV (author)2017-10-31

Hello midhun, and colleagues who follow this page.

I am making a similar model of your design, but I have had some difficulties, and I would like to know if you can solve some concerns:

1. I used a 18650 battery, but it is not enough to run the robot. In that case, what other battery could you use?

2. Both the DRV8833 Pololu motor driver and 5V boost converter are generic. This can influence the operation?

3.Is it necessary to use the 5V boost converter?

midhun_s (author)LeidyV2017-11-02


1. The battery I am using is a Panasonic NCR18650b rated for 3400mah. This will be at 4.2V when fully charged. This is then boosted to a steady 5V level. The motors that I am using is rated for 6V, 200rpm. Make sure you are using a motor rated for 6V. And also make sure you are using an original 18650 battery. There are many fake ones in the market.

2. You can use any other motor driver or boost converter as well. As long as they can carry the rated current of the motor, they do not affect the operation of the robot.

3. A boost converter ensures that a steady 5V power supply reaches the arduino and the motor. If we do not use a boost converter, as we run the robot, the battery voltage reduces gradually from 4.2V. As the voltage reduces, the motor will get slowed down and the arduino and the sensors may stop working.

novaland297 (author)2017-10-12

Very exellent man! Btw, How can I make this with DMP and FIFO buffer? I've been looking for some documents about this but couldn't understand so much. I think it'd be better if we let the IMU calculates the values instead of the arduino. Would you mind gave me some ideas or some documents if you can? Thank you for reading this!

midhun_s (author)novaland2972017-10-15

Thank you. I haven't used the DMP and FIFO buffer either. The DMP can be used to combine the data from accelerometer and gyroscsope. This should give a better angle approximation than the complimentary filter used here. The same library that I have used for getting raw sensor values have functions for getting DMP values as well.

SergeD13 made it! (author)2017-08-30

Hi midhun, I made it... it is a bite taller than Urs, but i used ur informations and ur sketch

I added bluetooth to be able to control and do some setting on a second page. It is RemoteXY app. Thats great !

I added too too 3 colours led fixed under the sonic sensor, it light inside through a little hole i did :)

It runs well, but still have some "little" problems. On a carpet it is 90% perfect. On

floor tile it is too much reactive, do yaws, runs too fast and finaly falls down . I try to modify the Kp, Ki, Kd... but it seems it is not the efficient way to act.

May U suggest me how to deal with ?

Thank you in advance and have a nice day

midhun_s (author)SergeD132017-09-01

Hello Serge, Great to see your robot with Bluetooth control.

From the description of your problem, I suppose your robot cannot recover once it gets titled and falls over in the same direction of tilting. If that is the case, increase Kp in steps from zero while keeping Ki and Kd zero. You should reach a value of Kp at which your robot recovers from the tilt and falls back in the opposite direction. Once you get your robot to do that, set Ki and Kd with a value of your choice at first and observe your robot's behavior. If it falls over while trying to recover from the tilt, try increasing Ki to correct the error which gets added up (here, the error is the angle of tilt). If your robot doesn't fall over trying to recover but oscillates rapidly about its center, increase Kd to reduce the oscillations.

If your robot is still not stable, try changing the filter time constant (see Step 5). Increase the time constant to reduce oscillations and decrease it if you need a quick response.

Do share your results. Have a nice day.

SergeD13 (author)midhun_s2017-09-04

hello, :)

I have an other little question, it is about the interrupt pin.
I'm using a arduino micro for my own and because it is not exactly the same than urs pro mini, i have to use an other interrupt pin than 2 the "original" pin. I declared it in my sketch, for sure, but should i modify something else in the skecth to be sure it is rightly used ?

Thank again

Wish u a nice day

midhun_s (author)SergeD132017-09-10

I was unsure whether the INT pin of MPU6050 is to be connected or not. After a bit of searching, it seems that the interrupt pin is required only if you are reading DMP values from MPU6050. So I think, the code should work well in our case even with the INT pin left unconnected.

SergeD13 (author)midhun_s2017-09-11

Hi, u surely right because i didn't noticed any changment, declared or not.

I still working on it, it is running... not badly. But, i still have some problems. The biggest is when suddenly the robot start to run (forward of backward), it seems on constant angle, speed increase, then finaly unable to compensate it falls down.

I tried to write some lines comparing speed, angle, acceleration, but not succed :(((

if U have some ideas, i will be happy to have your help.

thank you in advance

SergeD13 (author)midhun_s2017-09-03

Hi midhun, i worked on it this weekend so i can do some return already.

About the stability i think i will not do better :) Thats really not bad on any ground.

But the capability to keep awake thats an other problem :(

I noticed that when the robot start to run with a stable speed... it "lost control" then finaly fall donw.

i don't know how may i control the "increase speed" and be and it standing up.

if u have some good idea i will like listen to u and follow ur advise.

Thank you in advance.

PS maybe motor encoder will be the only solution to be able to control the moving.

Is there a way to send U a video to show you what happen ?


SergeD13 (author)midhun_s2017-09-03

Thank you for ur advise and ur replys :)

first i have change the wheel with smaller one, i noticed it is better
for it stabilty. Anyway for now it does the same thing and sometime,
when it has found equilibrium, his constant speed increase, look like a
larsen, then finaly fall down on forwarding, more than backwarding.

is a "weird" added in ur line, you add 255 to the motor control when
motorspeed is < 0 .. but 255 is the limite of power ! so why do u
add, i tried to delete and seems it is not changing anything.

i will follow ur method and will set kp at first,honnestly thats what did already :).

I already change the filter time constant and i notice it is better a few when i increase it. around 0.08

control it and move forward or Backward, i modify the targetAngle. it's
work, but i'm not really satisfy, do u think it is the best way for it ?

Thank again for Ur help, be sure i will share the result.

Have same nice day

Best regards from France

PS: for controling with tablette i'm using RemoteXY it is really a great soft ! do u know it ?

OctaR (author)2017-09-08

Hello Midhun, im trying to build self balancing robot like you, i have following your intruction. so far so good, but when i use library that written by Jeff Rowberg. arduino IDE send error compiling for me.

E:\<myDirectories>\Arduino\libraries\I2Cdev\I2Cdev.c:40:16: fatal error: os.h: No such file or directory

#include "os.h"


compilation terminated.

im try to find os.h library on google but i couldnt get. how to repair this error ? thanks for great tutorial.

Kul_Guyz (author)2017-07-22

I have a problem, I'm using all the materials above but the only difference is that I'm using an L298N driver. So how do I change the code to fit in the driver?
Secondly, the codes​ I downloaded from your​Instructables have to be debugged a lot in my laptop, and I can't fully debug the whole code especially in the calibration part of the code, Can you please help me in this because I'm still a newbie and I've just got a gist on how the code works. Thank you and have a nice day ???

midhun_s (author)Kul_Guyz2017-07-22

You need not change your code to fit in L298N. From the last picture of Step 2, you can see that Arduino digital pins 4, 5, 6 and 7 are connected to AIN1, AIN2, BIN2 and BIN1 pins of DRV8833 respectively. Connect these four Arduino pins to your L298N motor control pins. Here, pins 4 & 5 are for controlling the right motor and 6 & 7 for the left. Make sure to set any enable pins on your driver module. Upload the code and check whether both the motors turn in the right direction. Swap the connections to the motor if it rotates in the opposite direction.

Regarding the code, I hope you have installed necessary libraries (I2Cdev.h and MPU6050.h). Also, make sure that the MPU6050 is oriented exactly as shown in the photos in Step 2. You will have to do a slight modification in the code if you have arranged the MPU6050 in a different way. I hope this solves most of the issues. Feel free to ask if your problem is not solved.

Have a nice day :)

Kul_Guyz (author)midhun_s2017-07-23

Thanks for the quick and helpful response however my problem still lingers
The calibration part found on the void set up had an error saying:
Class MPU6050 has no member called initialize
Other than that there was no other difficulties I faced

midhun_s (author)Kul_Guyz2017-07-28

Sorry for my delayed response. Did you download the MPU6050 from the link that I've given in Step 3?

Kul_Guyz (author)Kul_Guyz2017-07-23

I also have downloaded the libraries required for the project

LeoO15 made it! (author)2017-07-13

Hi friend, thank you very much for the information

I am doing a similar project for university but I need to apply PID control on frequency would change the program? Or could you use the same?

midhun_s (author)LeoO152017-07-16

Hi, Leo. Glad to see your project. Try setting the constants as given in Step 7. The value that you will end up with may be different from what I have used in my code.

houmedim (author)2017-06-19

Excuse me again to disturb you. When you talk about "serial monitor", I do not understand which component is it!

midhun_s (author)houmedim2017-06-20

Serial Monitor is a terminal you can access from the Arduino programming environment. Look for the right top corner after opening the Arduino IDE. In Steps 3 and 4, you will need to first connect your Arduino to your computer using a USB-TTL converter. Now upload the sketch and open the Serial Monitor to see the angle of inclination.

houmedim (author)midhun_s2017-06-20

OK thank you very much my friends

DrAure (author)2017-06-20

I want to have the acceleration of the there axes (whitout counting gravity) and the inclinaison of the three axes. How can I do this using a MPU6050 and an Arduino?

houmedim (author)2017-06-16

please, Do I need to use a component to connect the arduino so that it can be programmed? for example a FTDI?

midhun_s (author)houmedim2017-06-17

Yup. You will need a USB-TTL converter for programming the arduino pro mini.

houmedim (author)midhun_s2017-06-17

perfect! thank you very much dear friend

houmedim (author)2017-06-16

Do I need to use a component to connect the arduino so that it can be programmed?

Kul_Guyz (author)2017-06-11

Can this robot balance on a ball (with little modifications, of course)?

midhun_s (author)Kul_Guyz2017-06-13

The principle might be the same but you might need some major modifications in the structure.

malonu (author)2017-06-04

What is the newping.h library?

midhun_s (author)malonu2017-06-07

It is a library used to interface the ultrasonic distance sensor with the arduino.

BrownDogGadgets (author)2017-05-08

What about using two continuous rotation servos instead of geared motors? Would that work?

midhun_s (author)BrownDogGadgets2017-05-09

Continuous rotation servos should also work well. Since the rpm of these servos are generally low, small wheels like the one used here might have to be replaced with large diameter ones.

JoãoM134 (author)2017-05-04

Hi, i am using the arduino leonardo but without se sonar. i cleared the sonar part from the code and changed the mpu pins to the right ones ,but when i upload the code the wheels dont stop sping .Can you help me?

Thanks ,great progect.

midhun_s (author)JoãoM1342017-05-04

I hope you have placed the MPU6050 exactly as shown in the picture. You can also upload the codes in steps 3 & 4 and check whether the angle of inclination is correct (+ve while tilting forward and -ve while tiliting backward)

JoãoM134 (author)midhun_s2017-05-08

i did what you said but phe problem stays the same

midhun_s (author)JoãoM1342017-05-09

I hope you are getting +ve angles when tilting the robot forward and -ve when tilting backward. Now you need to check whether the connections to the motor are done correctly. You can use the setMotors() in your loop and comment out all the other lines of code in your loop. The function works this way: setMotors(leftMotorSpeed, rightMotorSpeed). +ve value of motorSpeed gives forward rotation and -ve gives reverse rotation. Check both motors for speed and direction by giving different values for leftMotorSpeed and rightMotorSpeed ranging from -255 to +255.

AdnanH25 (author)2017-05-08

Hey Would you please share the schematics for the wiring?

midhun_s (author)AdnanH252017-05-08

I haven't made a schematic yet. But I hope I have made the circuit clear using the perfboard layouts and the block diagram given in Step 2. Do let me know if there is any doubt.

santu.sanucupid19 (author)2017-05-05

its really a nice tutorial. I am planning build one with this as my reference notes. Keep up the good work.

Thank you. Once you've finished yours please share it here. All the best.

About This Instructable




More by midhun_s:Arduino Self-Balancing RobotRobot - Line Follower
Add instructable to: