L298N is replaced by L298 but works differently.

I bought a two wheel drive KR 3160 robot toy kit car and all parts required from Jaycar. I had it working very well until one day I damaged and needed to replace the L298N stepper controller module board. Since replacing the L298N with the new replacement number L298, the ultrasonic sensor no longer works. I have tried rewiring, I have tried a different Arduino R3 UNO board, I have tried a different HC SR04 ultrasonic sensor. I have double checked the code, yet nothing can make the sensing work. The car hits the wall every time. The only thing that is different is the new part number on the stepper controller module red card. It is now L298 as the L298N is no longer available! The chip in the card is still numbered L298N but the actual circuitry card has the new number printed on it without the N. Why is the car not sensing an object ahead of itself with this new number and what can I do to make it sense objects?

Links to the robot and new motor driver board might help. Posting the code might too because even if you have double checked it we haven't seen it at all.

My wild guess is that there's a conflict between the pins the new motor driver board is using and the pins you have the sensor connected to. But with no real information it's just a guess.

Steve

Thank you Steve,

This my first time in forum help. I will try to attach the code I used.

Hope not to lose contact. I will try now. I am going to press post hoping to see attach.

Vince

Hi Steve,

Here is the original code that I am still using.

Vince

//Ultrasonic sensor pins
#define TRIG 9
#define ECHO 10

//motor module pins
const int channel_a_enable = 6;
const int channel_a_input_1 = 4;
const int channel_a_input_2 = 7;
const int channel_b_enable = 5;
const int channel_b_input_3 = 3;
const int channel_b_input_4 = 2;

int a=0; //motor a speed and direction
int b=0; //motor b speed and direction
int phase=0; //what stage are we up to
long timeout=0; //how long have we been doing it for

#define PROX 30
#define FORWARDSPEED 150
#define REVERSESPEED 150
//set TURNSPEED negative to turn the other way
#define TURNSPEED 150
#define REVERSETIME 1000
#define TURNTIME 1000

void setup()
{
pinMode(11,OUTPUT);
digitalWrite(11,LOW); //GND for ultrasonic
pinMode(8,OUTPUT);
digitalWrite(8,HIGH); //5V for ultrasonic

usonicsetup();

pinMode( channel_a_enable, OUTPUT ); // Channel A enable
pinMode( channel_a_input_1, OUTPUT ); // Channel A input 1
pinMode( channel_a_input_2, OUTPUT ); // Channel A input 2

pinMode( channel_b_enable, OUTPUT ); // Channel B enable
pinMode( channel_b_input_3, OUTPUT ); // Channel B input 3
pinMode( channel_b_input_4, OUTPUT ); // Channel B input 4
//everything off
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, LOW);
digitalWrite( channel_a_enable, LOW);

digitalWrite( channel_b_input_3, LOW);
digitalWrite( channel_b_input_4, LOW);
digitalWrite( channel_b_enable, LOW);

delay(1000); //wait a bit

Serial.begin( 9600 ); //serial debug
Serial.println(“Starting up”);
}

void loop()
{
//read ultrasonic sensor
int d;
d=usonic(11600)/58; // distance in cm
if(d==0){d=200;}
Serial.println(d); //print d on serial monitor

//react based on d
switch(phase){
case 0:
if(d<PROX){phase=1;timeout=millis();} //if too close, reverse
break;
case 1:
if(millis()>timeout+REVERSETIME){phase=2;timeout=millis();} //reversed for REVERSETIME, now turn
break;
case2:
if(millis()>timeout+TURNTIME){phase=0;} //turned for TURNTIME, now back to forward
break;
}

//print phase for debugging
Serial.print(“PHASE:”);
Serial.println(phase);

//set motors based on stage
switch(phase){
case 0: //normal going forward
a=FORWARDSPEED;
b=FORWARDSPEED;
break;
case 1: //backing up
a=-(REVERSESPEED);
b=-(REVERSESPEED);
break;
case 2: //turn a little
a=TURNSPEED;
b=-(TURNSPEED);
break;
default: //something wrong happened
a=0;
b=0;
break;
}
setmotor();
delay(200); //wait a bit
}

void setmotor(){
int s;

if(a>0){
digitalWrite( channel_a_input_1, HIGH);
digitalWrite( channel_a_input_2, LOW);
}
if(a<0){
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, HIGH);
}
if(b>0){
digitalWrite( channel_b_input_3, HIGH);
digitalWrite( channel_b_input_4, LOW);
}
if(b<0){
digitalWrite( channel_b_input_3, LOW);
digitalWrite( channel_b_input_4, HIGH);
}

s=abs(a);
if(s>255){s=255;}
if(s<0){s=0;}
analogWrite( channel_a_enable, s);

s=abs(b);
if(s>255){s=255;}
if(s<0){s=0;}
analogWrite( channel_b_enable, s);

}

void allInputsOff()
{
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, LOW);
digitalWrite( channel_a_enable, LOW);

digitalWrite( channel_b_input_3, LOW);
digitalWrite( channel_b_input_4, LOW);
digitalWrite( channel_b_enable, LOW);
}

void usonicsetup(void){
pinMode(ECHO, INPUT);
pinMode(TRIG, OUTPUT);
digitalWrite(TRIG, LOW);
}

long usonic(long utimeout){ //utimeout is maximum time to wait for return in us
long b;
if(digitalRead(ECHO)==HIGH){return 0;} //if echo line is still low from last result, return 0;
digitalWrite(TRIG, HIGH); //send trigger pulse
delay(1);
digitalWrite(TRIG, LOW);
long utimer=micros();
while((digitalRead(ECHO)==LOW)&&((micros()-utimer)<1000)){} //wait for pin state to change- return starts after 460us typically
utimer=micros();
while((digitalRead(ECHO)==HIGH)&&((micros()-utimer)<utimeout)){} //wait for pin state to change
b=micros()-utimer;
return b;
}

robot_2.ino (3.83 KB)

This isn't going to help

case2:
      if (millis() > timeout + TURNTIME) {
        phase = 0; //turned for TURNTIME, now back to forward
      }
      break;

But the obvious question is what distances are you seeing in your Serial prints of "d"? It may be worth adding extra prints to check the actual value returned from usonic (I can't make any sense of the code in that function particularly since the comments are rubbish...you check if ECHO is HIGH and the comment says return zero if ECHO is still LOW?).

Steve

Thank you for trying to help me,Steve,

But I am new at coding and have a long way to go. I was hoping for someone to suggest a different driver module or something like that. Or at least explain why the boards are different in what they do. I appreciate your help. I did look up some links as you suggested and will continue to look further. I will also mention the usonics to someone I know that might help me in that area.

Thanks again,

vince

If you won't answer questions about what exactly is happening and you won't give any details of the two boards are that are different then it is difficult to help.

Steve

Hi

I'm building a similar project and i'm also using the l298n board and an ultrasonic sensor, i dont think the sensor is not working because of the l298 board, my guess is some wrong connection or some error on the code

Edit: i see on your code that you use pin 9 and 10 to connect the sensor, i'm not sure but i thought the ultrasonic sensor must be connected to the analog pins

//Ultrasonic sensor pins
#define TRIG 9
#define ECHO 10

It would help if you can recall where you read the information about connecting to the analog part of the Arduino uno board.The analog on my Aldruino uno shows ao-a1-a2-a3-a4-a5 but I have no ultrasonic info to that. It's a little hard without.

Vince

On my project i use a sensor shield board, and that board have a set of 4 pins to connect the ultrasonic sensor, and those pins connect to A0 and A1, of course you can connect it without the sensor shield, before i start to build i search on google and on every schematic and code uses the analog pins

Here the first working version of my code you see i use A0 and A1

#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 3;
const int LeftMotorBackward = 5;
const int RightMotorForward = 6;
const int RightMotorBackward = 11;

//sensor pins
#define trig_pin A0 //analog input 1
#define echo_pin A1 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  
  servo_motor.attach(10); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){

  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    analogWrite(LeftMotorForward, 90);
    analogWrite(RightMotorForward, 95);
  
    analogWrite(LeftMotorBackward, 0);
    analogWrite(RightMotorBackward, 0); 
  }
}

void moveBackward(){

  goesForward=false;

  analogWrite(LeftMotorBackward, 90);
  analogWrite(RightMotorBackward, 95);
  
  analogWrite(LeftMotorForward, 0);
  analogWrite(RightMotorForward, 0);
  
}

void turnRight(){

  analogWrite(LeftMotorForward, 90);
  analogWrite(RightMotorBackward, 95);
  
  analogWrite(LeftMotorBackward, 0);
  analogWrite(RightMotorForward, 0);
  
  delay(450);
  
  analogWrite(LeftMotorForward, 90);
  analogWrite(RightMotorForward, 95);
  
  analogWrite(LeftMotorBackward, 0);
  analogWrite(RightMotorBackward, 0);
 
  
  
}

void turnLeft(){

  analogWrite(LeftMotorBackward, 90);
  analogWrite(RightMotorForward, 95);
  
  analogWrite(LeftMotorForward, 0);
  analogWrite(RightMotorBackward, 0);

  delay(450);
  
  digitalWrite(LeftMotorForward, 90);
  digitalWrite(RightMotorForward, 95);
  
  digitalWrite(LeftMotorBackward, 0);
  digitalWrite(RightMotorBackward, 0);
}

vincescerri: The only thing that is different is the new part number on the stepper controller module red card. It is now L298 as the L298N is no longer available!

Surely it's possible to obtain a compatible replacement L298N module that has the same features as your original one.

Should post photos of your failed module. And a photo of the new module.

This is so that people can get an idea about what the particular modules (you have) look like. They probably need to be able to see something in order to help. People here are usually not clairvoyant.

Thank you for your suggestion, but I was too quick and threw the L298n module away.

Thanks again

Then better show photos of your new one, and photos of how you connected it up to the sensors etc.

Also, if your system is anything like this .....

...click here....

Then the ultrasonic sensors don't even connect to the L298N motor driver module. The sensors connect to the arduino.

jorpec: On my project i use a sensor shield board, and that board have a set of 4 pins to connect the ultrasonic sensor, and those pins connect to A0 and A1, of course you can connect it without the sensor shield, before i start to build i search on google and on every schematic and code uses the analog pins

I believe you're right jorpec. The sensors are associated with analog pins in some cases. Eg.

int inputPin = A0; // define ultrasonic receive pin (Echo) int outputPin =A1; // define ultrasonic send pin(Trig)

But....after checking things up on the internet, it appears that they're using analog pins to do 'digital' things.

So digital pins could be used for this case.

Best bet is to just forget about the L298 for the moment, and just use the arduino to test the sensors.

Thanks,

I have tried the sensors on something else and are working. I have also tried a different Arduino Uno.

Although I haven't effected the wiring in any way, I still went ahead and doubled checked. All good with the wiring. It's wierd. All that has been changed is the L298n to a L298. We tried.

Thanks again

vincescerri: Although I haven't effected the wiring in any way, I still went ahead and doubled checked. All good with the wiring. It's wierd. All that has been changed is the L298n to a L298. We tried.

You might need to check. But I think "L298N" is the same as "L298". The IC itself within the module is the "L298N". People just call the module a "L298N" .... but it's really a motor driver module based on the L298N IC...... featuring input and output pins and connectors/sockets etc for interfacing.

Could also look around on ebay and search for "Arduino Stepper Motor Controller Module". Maybe that's where you got your replacement from already.