Arduino and motor L298n code

Hello guys,

well... I'm trying to connect to a motor L298n an Arduino UNO. I made a code, but when I finish uploading it, I power my Motor and Arduino, but nothing happens!
I tried to modify my code, but always in vain... I don't know, for sure, if the problem is withing my code.

here is my code:
int speedMAX = 255;
int speedMIN = 50;
int A_En = 13;
int int1 = 11; // int1
int int2 = 10; // int2

int B_En = 12;
int int4 = 8; // int4
int int3 = 9; // int3

void setup() {
Serial.begin(9600);
Serial.println("ENTER AT Commands:");

pinMode(A_En, OUTPUT);
pinMode(int1, OUTPUT);
pinMode(int2, OUTPUT);

pinMode(B_En, OUTPUT);
pinMode(int4, OUTPUT);
pinMode(int3, OUTPUT);

digitalWrite(A_En,LOW);
digitalWrite(B_En,LOW);//stop driving
digitalWrite(int1,LOW);
digitalWrite(int2,LOW);//setting motorA's direction
digitalWrite(int3,LOW);
digitalWrite(int4,LOW);//setting motorB's direction

}

void loop() {
char data;
data = Serial.read();
Serial.write(Serial.read());

switch (data)
{
case '1': // FORWARD
analogWrite(A_En, speedMAX);
analogWrite(B_En, speedMAX);
digitalWrite(int1, HIGH);
digitalWrite(int2, LOW);
digitalWrite(int3, HIGH);
digitalWrite(int4, LOW);
break;
case '2': // BACKWARD
analogWrite(A_En, speedMAX);
analogWrite(B_En, speedMAX);
digitalWrite(int1, LOW);
digitalWrite(int2, HIGH);
digitalWrite(int3, LOW);
digitalWrite(int4, HIGH);
break;
case '3': // FORWARD LEFT
analogWrite(A_En, speedMIN);
analogWrite(B_En, speedMAX);
digitalWrite(int1, LOW);
digitalWrite(int2, LOW);
digitalWrite(int3, HIGH);
digitalWrite(int4, LOW);
break;
case '4': //FORWARD RIGHT
analogWrite(A_En, speedMAX);
analogWrite(B_En, speedMIN);
digitalWrite(int1, HIGH);
digitalWrite(int2, LOW);
digitalWrite(int3, LOW);
digitalWrite(int4, LOW);
break;
case '5': //BACKWARD LEFT
analogWrite(A_En, speedMIN);
analogWrite(B_En, speedMAX);
digitalWrite(int1, LOW);
digitalWrite(int2, HIGH);
digitalWrite(int3, LOW);
digitalWrite(int4, LOW);
break;
case '6': // BACKWARD RIGHT
analogWrite(A_En, speedMAX);
analogWrite(B_En, speedMIN);
digitalWrite(int1, LOW);
digitalWrite(int2, LOW);
digitalWrite(int3, LOW);
digitalWrite(int4, HIGH);
break;
default:
analogWrite(A_En, LOW);
analogWrite(B_En, LOW);
digitalWrite(int1, LOW);
digitalWrite(int2, LOW);
digitalWrite(int3, LOW);
digitalWrite(int4, LOW);
}
data = 1;
delay(2000);
data = 6;
delay(2000);
data = 5;
delay(2000);
data = 3;
delay(2000);
data = 2;
delay(2000);
data = 4;
delay(2000);
}

Hi,
The L298 is not a very good motor driver as you lose power in the output stage (2-3volts). Also we need to know what driver shield you are using, also what power, seperate for motors and arduino!!

With out more info, we don’t know how your working and what it should do? your code which should be in code<> brackets, as no comments so we can’t follow what’s doing what!!

And I’m having a bad day! My chrystal balls not working either…

Hope it helps.

Mel.

well, I'm working on an arduino car, like this one in this tutorial: Smartphone Controlled Arduino 4WD Robot Car ( Part - II ) - YouTube
but I'm doing this without a bluetooth module.

I've modified my code lately, this is the new one:

int speedMAX = 255;
int speedMIN = 50;
//Motor A;
int Motor_A_Enable = 13;
int Motor_A_Forward = 11; // int1
int Motor_A_Backward = 10; // int2

//Motor B;
int Motor_B_Enable = 12;
int Motor_B_Forward = 8; // int4
int Motor_B_Backward = 9; // int3

char data;

void setup() {

pinMode(Motor_A_Enable, OUTPUT);
pinMode(Motor_A_Forward, OUTPUT);
pinMode(Motor_A_Backward, OUTPUT);

pinMode(Motor_B_Enable, OUTPUT);
pinMode(Motor_B_Forward, OUTPUT);
pinMode(Motor_B_Backward, OUTPUT);

digitalWrite(Motor_A_Enable,LOW);
digitalWrite(Motor_B_Enable,LOW);//stop driving
digitalWrite(Motor_A_Forward,LOW);
digitalWrite(Motor_A_Backward,LOW);//setting motorA's direction
digitalWrite(Motor_B_Backward,LOW);
digitalWrite(Motor_B_Forward,LOW);//setting motorB's direction
}

void loop() {
char data;
// data = Serial.read();
// Serial.write(Serial.read());

/* Run CAR FORWARD for 2 sec */
data = 1;
delay(2000);

/* Run motor BACKWARD RIGHT for 2 sec */
data = 6;
delay(2000);

/* Run motor BACKWARD LEFT for 2 sec */
data = 5;
delay(2000);

/* Run motor FORWARD LEFT for 2 sec */
data = 3;
delay(2000);

/* Stop motor FOREVER :slight_smile: */
data = 10;

switch (data)
{
case '1':
// FORWARD
analogWrite(Motor_A_Enable, speedMAX);
analogWrite(Motor_B_Enable, speedMAX);
digitalWrite(Motor_A_Forward, HIGH);
digitalWrite(Motor_A_Backward, LOW);
digitalWrite(Motor_B_Backward, LOW);
digitalWrite(Motor_B_Forward, HIGH);
break;
case '2': // BACKWARD
analogWrite(Motor_A_Enable, speedMAX);
analogWrite(Motor_B_Enable, speedMAX);
digitalWrite(Motor_A_Forward, LOW);
digitalWrite(Motor_A_Backward, HIGH);
digitalWrite(Motor_B_Backward, HIGH);
digitalWrite(Motor_B_Forward, LOW);
break;
case '3': // FORWARD LEFT
analogWrite(Motor_A_Enable, speedMIN);
analogWrite(Motor_B_Enable, speedMAX);
digitalWrite(Motor_A_Forward, LOW);
digitalWrite(Motor_A_Backward, LOW);
digitalWrite(Motor_B_Backward, LOW);
digitalWrite(Motor_B_Forward, HIGH);
break;
case '4':// FORWARD RIGHT
analogWrite(Motor_A_Enable, speedMAX);
analogWrite(Motor_B_Enable, speedMIN);
digitalWrite(Motor_A_Forward, HIGH);
digitalWrite(Motor_A_Backward, LOW);
digitalWrite(Motor_B_Backward, LOW);
digitalWrite(Motor_B_Forward, LOW);
break;
case '5': //BACKWARD LEFT
analogWrite(Motor_A_Enable, speedMIN);
analogWrite(Motor_B_Enable, speedMAX);
digitalWrite(Motor_A_Forward, LOW);
digitalWrite(Motor_A_Backward, HIGH);
digitalWrite(Motor_B_Backward, LOW);
digitalWrite(Motor_B_Forward, LOW);
break;
case '6': // BACKWARD RIGHT
analogWrite(Motor_A_Enable, speedMAX);
analogWrite(Motor_B_Enable, speedMIN);
digitalWrite(Motor_A_Forward, LOW);
digitalWrite(Motor_A_Backward, LOW);
digitalWrite(Motor_B_Backward, HIGH);
digitalWrite(Motor_B_Forward, LOW);
break;
}
}

Nice to know that you keep changing your code but:

  1. Please post your code in </> code tags. It’s much easier for us to read.

  2. Please tell us what motors you are using, exactly what you are using to power the motors and how you have it all connected.

Otherwise my best guess is either you’re using the wrong components or you have them wired up wrong or the code is wrong. We can’t tell.

Steve

rachida:
Hello guys,
well... I'm trying to connect to a motor L298n an Arduino UNO. I made a code, but when I finish uploading it, I power my Motor and Arduino, but nothing happens!

What you've done is the 'holy grail' of 'what not to do'.

Usually, the first thing to do is to start with the simplest piece of code for testing the L298N outputs.

This means..... don't even have the motors connected to the L298N. And just write the shortest piece of program you can that will produce a voltage across one of the output terminals of the L298N. Then....change the code so that it manually removes that voltage across those terminals. If both of these pieces of code works reliably..... then proceed to use more code to test the other pair of terminals. Then you can be sure that you have full control of those terminal voltages (with software).

You can then focus on driving the robot's motors when you're sure that you can reliably control the L298N's output voltages.

Also, if your L298N has a ground terminal..... then connect that ground terminal to the arduino's ground terminal.

Also........ even though the 'delay' function is part of the toolset...... avoid using this function..... the reason is because when this function is activated ..... the whole program will basically become disabled (hanging here) until the amount of delay time has been completed. It is as if I am tell you to count to peel potatoes, and then somebody tells you to do nothing (ie. delay) for 10 seconds.....and you're not allowed to do anything else except wait for 10 seconds before being able to get back to what you were supposed to be going before.

And...what other important things could the program need to do (without getting held up)? .... One of those things is receiving instructions..... or receiving data coming in through a cable, or coming in via wireless transmissions. And you don't want to hold up other tasks such as sending time-critical commands for driving other things....like motors etc.

Using delay() is a very manly thing! When we men put a sandwich in the microwave oven, we stair at the micro for 30 seconds doing nothing. I've heard that women can do a lot of things during those 30 seconds.

This is why code tags are preferred.

arduino forum 3.PNG

Well, your code does nothing that would activate the motors. You just set data to different values, then you delay. After each line thst sets data to a value, you need the code that actually sets the pins high or low accordingly.