Help with controlling 2 DC motors with L298N motor driver and MPU6050 Gyro with Arduino Uno

Hello,

I am building a robot with 2 DC motors and ran into the problem of the robot not driving straight, so I am trying to use the MPU6050 gyro to fix this issue. I wired everything and coded a program to test the gyro and motors. However, after uploading the code, I found the motors are not running at all. I attached the code and a picture of my wiring so far, any help would be appreciated. Thank you!

Here is my code, following the tutorial from https://www.youtube.com/watch?v=Xd1_NMVx2l0&list=PLTeUncoDx9z2oY6xSqprzZB2KVQogWywt&index=1.

#include <Wire.h>
#include <MPU6050.h>
#define motor1_pin1 5 //PWM - Digital Pins
#define motor1_pin2 6 //PWM - Digital Pins
#define motor2_pin1 9 //PWM - Digital Pins
#define motor2_pin2 10 //PWM - Digital Pins

MPU6050 gy_521;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int motor1_speed1;
int motor2_speed1;
int motor1_speed2;
int motor2_speed2;

void setup ( ){
Wire.begin( );
Serial.begin (9600);
Serial.println ("Initializing MPU and testing connections");
gy_521.initialize ( );
Serial.println(gy_521.testConnection( ) ? "Successfully Connected" : "Connection failed");
delay(1000);
Serial.println("Reading Values");
delay(1000);
}

void loop ( ){
gy_521.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
ax = map(ax, -17000, 17000, -125, 125);
ay = map(ay, -17000, 17000, -125, 125);
motor1_speed1 = 125+ax;    //To move first motor
motor2_speed1 = 125-ax;    //To move second motor
motor1_speed2 = 125+ay;    //To move first motor
motor2_speed2 = 125-ay;    //To move second motor
Serial.print ("Motor1 Speed1 = ");
Serial.print (motor1_speed1, DEC);
Serial.print (" && ");
Serial.print ("Motor2 Speed1 = ");
Serial.println (motor2_speed1, DEC);
Serial.print ("Motor1 Speed2 = ");
Serial.print (motor1_speed2, DEC);
Serial.print (" && ");
Serial.print ("Motor2 Speed2 = ");
Serial.println (motor2_speed2, DEC);
analogWrite (motor1_pin1, motor1_speed1);
analogWrite (motor1_pin2, motor2_speed1);
analogWrite (motor2_pin1, motor1_speed2);
analogWrite (motor2_pin2, motor2_speed2);
delay(200);
}

Arduino wiring

With the L298 you normally use the IN pins for direction control and the EN pins for speed.
That video has nothing to do with keeping a robot going straight

However, I don't think that using an accelerometer to make small corrections in direction is a practical solution.

Connecting a 3.3 volt device to th 5 volt UNO sticks out.
What do the Seriial.prints tell?

I did find this: https://www.instructables.com/How-to-Make-a-Robot-Car-Drive-Straight-and-Turn-Ex/ on using a gyro specifically to make a robot drive straight, but I am unsure how to adapt it to the L298N motor driver I am using.

Would using an encoder work better? If so, what kind of encoder should I use? Thank you, again.

Thank you for your response. The serial prints gave 125 for the speeds of both motors, though neither of the motors were actually spinning.

Check that your codeing/sketch has maintained the following Truth Table:

ENA  IN1  IN2      Function
5V   5V   0V       Forward Rotation
5V   0V   5V       Reverse Rotation
5V   PWM  0V       Variable speed at Forward Rotation

Do not connect 5V from UNO to L298 Diver as the driver has its own source of 5V (assuming that the 5V jumper is here).

Connect the left motor to OUT1,2
Connect the right motor to OUT4,3

Connect ENA to pin 9
Connect ENB to pin 5

Connect IN1 to pin 3
Connect IN2 to pin 2
Connect IN3 to pin 8
Connect IN4 to pin 4

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.