controlling DC motor via L298N with arduino

Hello!

Can someone tell me how to wire and control the DC motor with L298N DC motor controlling board?
The DC motor I am trying to control is 23KM-K246-01V

This is the code that i have so far:

var pin0 = new OutputPort(Pins.GPIO_PIN_D11, false);
var pin1 = new OutputPort(Pins.GPIO_PIN_D10, false);
var pin2 = new OutputPort(Pins.GPIO_PIN_D9, false);
var pin3 = new OutputPort(Pins.GPIO_PIN_D8, false);

while (true)
{
pin0.Write(true);
pin1.Write(false);
pin2.Write(true);
pin3.Write(false);

Thread.Sleep(5);

pin0.Write(true);
pin1.Write(false);
pin2.Write(false);
pin3.Write(true);

Thread.Sleep(5);

pin0.Write(false);
pin1.Write(true);
pin2.Write(false);
pin3.Write(true);

Thread.Sleep(5);

pin0.Write(false);
pin1.Write(true);
pin2.Write(true);
pin3.Write(false);

Thread.Sleep(5);
}

Written for NetDuino but I have tried and interesting to do it for Arduino. I cannot make it work for both duinos… :frowning:

Thank you!

$T2eC16FHJGIFFo(5Me!rBR4PMwmVSw~~60_12.JPG

I have no idea what a netduino is, but that code is nothing like Arduino code which has to have this structure:

// declarations and stuff right at the top

void setup()
{
// stuff that only runs once like pin configuration etc
}

void loop()
{
//stuff here that runs again and again like reading sensors and switching motors.
}

There are loads of 298 examples in this forum and the playground, and on instructables too.

You need to ask that question on the Netduino forum: http://forums.netduino.com/

Netduino is not an Arduino.

But I found some old test code.

I didn’t put any comments in it which is bad of me, but it seems I used Arduino pin 11 as ENA and 12 and 13 as motor A inputs, and pin 10 as ENB with 8 and 9 as that motor inputs.

Sorry for the total lack of comments… I assume it worked but it’s a couple of years ago.

void setup()
{
  pinMode(11, OUTPUT);   // sets the pin as output
  pinMode(12, OUTPUT);   // sets the pin as output
  pinMode(13, OUTPUT);   // sets the pin as output
  pinMode(8, OUTPUT);   // sets the pin as output
  pinMode(9, OUTPUT);   // sets the pin as output
  pinMode(10, OUTPUT);   // sets the pin as output
}

void loop()
{
  int i;
  digitalWrite(12,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(13,LOW);
  digitalWrite(9,LOW);
  for(i = 0;i<255;i++)
  { 
    analogWrite(11, i);  
    analogWrite(10, i);
    delay(100);
  }
  digitalWrite(13,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(12,LOW);
  digitalWrite(8,LOW);
  for(i = 0;i<255;i++)
  { 
    analogWrite(11, i);  
    analogWrite(10, i);
    delay(100);
  }
}

MarkT: Netduino is not an Arduino.

But I think the OP also wants to run it on an Arduino.

OK, does anyone know if the wiring is correct?
I am attaching the specification for this motor.

Thank you!

23KM-K255U.pdf (582 KB)

red+yellow = one winding blue+orange = other winding (bipolar mode, centre-taps unconnected), so yes your photo shows it wired right. Now you've just got to sequence it right :)

MarkT:
Now you’ve just got to sequence it right :slight_smile:

I hadn’t noticed it’s a stepper: I went with “DC motor” in the headline. You may well find that the 298 is not the best chip to run that stepper, maybe look at a Pololu A4988.

Here’s some code I wrote a year ago to run a stepper off a 293 (not a 298), and explained here.

Your Mileage May Vary

// to run a stepper from a 293
// no libraries involved ;)
// Jim Brown March 2013
// E&OE, YMMV

  int ledpin = 13;
  
  //arduino pin names match motor wires
  
  int red = 8;
  int black = 9;
  int blue= 10;
  int yellow = 11;
  
    //speed control.... pause between steps
  int stepPause = 100;
  
void setup()

{
  // switch led on 13 off
  pinMode(ledpin, OUTPUT);
  digitalWrite(ledpin, LOW);
    
  pinMode(red, OUTPUT);
  pinMode(black, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(yellow, OUTPUT);
 
}

void loop()
{
  
  //step.... RED
  digitalWrite(red, HIGH);
  digitalWrite(black, LOW);
  digitalWrite(blue, LOW);
  digitalWrite(yellow, LOW);
  delay(stepPause);
  
    //step.... BLUE
  digitalWrite(red, LOW);
  digitalWrite(black, LOW);
  digitalWrite(blue, HIGH);
  digitalWrite(yellow, LOW);
  delay(stepPause);
  
    //step....BLACK
  digitalWrite(red, LOW);
  digitalWrite(black, HIGH);
  digitalWrite(blue, LOW);
  digitalWrite(yellow, LOW);
  delay(stepPause);
  
    //step....YELLOW
  digitalWrite(red, LOW);
  digitalWrite(black, LOW);
  digitalWrite(blue, LOW);
  digitalWrite(yellow, HIGH);
  delay(stepPause);
  
}

JimboZA:

MarkT:
Now you’ve just got to sequence it right :slight_smile:

I hadn’t noticed it’s a stepper: I went with “DC motor” in the headline. You may well find that the 298 is not the best chip to run that stepper, maybe look at a Pololu A4988.

Here’s some code I wrote a year ago to run a stepper off a 293 (not a 298), and explained here.

Your Mileage May Vary

// to run a stepper from a 293

// no libraries involved :wink:
// Jim Brown March 2013
// E&OE, YMMV

int ledpin = 13;
 
 //arduino pin names match motor wires
 
 int red = 8;
 int black = 9;
 int blue= 10;
 int yellow = 11;
 
   //speed control… pause between steps
 int stepPause = 100;
 
void setup()

{
 // switch led on 13 off
 pinMode(ledpin, OUTPUT);
 digitalWrite(ledpin, LOW);
   
 pinMode(red, OUTPUT);
 pinMode(black, OUTPUT);
 pinMode(blue, OUTPUT);
 pinMode(yellow, OUTPUT);

}

void loop()
{
 
 //step… RED
 digitalWrite(red, HIGH);
 digitalWrite(black, LOW);
 digitalWrite(blue, LOW);
 digitalWrite(yellow, LOW);
 delay(stepPause);
 
   //step… BLUE
 digitalWrite(red, LOW);
 digitalWrite(black, LOW);
 digitalWrite(blue, HIGH);
 digitalWrite(yellow, LOW);
 delay(stepPause);
 
   //step…BLACK
 digitalWrite(red, LOW);
 digitalWrite(black, HIGH);
 digitalWrite(blue, LOW);
 digitalWrite(yellow, LOW);
 delay(stepPause);
 
   //step…YELLOW
 digitalWrite(red, LOW);
 digitalWrite(black, LOW);
 digitalWrite(blue, LOW);
 digitalWrite(yellow, HIGH);
 delay(stepPause);
 
}

Mine 298 getting really hot when I use it. It is so hot that I cannot touch it.
Can I use Toshiba ULN2803APG chip for this stepper?

How can I know what chip is good for this stepper or any other NEMA DC motor?

Thank you!

Yeah 298s usually have a heatsink as big as an elephant.

I can’t say what other chips would work, sorry.

BTW, the “NEMA x” in a motor’s name has nothing at all to do with the motor’s electrical characteristics: it refers solely to the motor’s physical size and placement of the mounting holes.

JimboZA: Yeah 298s usually have a heatsink as big as an elephant.

I can't say what other chips would work, sorry.

BTW, the "NEMA x" in a motor's name has nothing at all to do with the motor's electrical characteristics: it refers solely to the motor's physical size and placement of the mounting holes.

I believe you are on something. I have bought a small stepper motor with the drive board from eBay just to try if I can move at least any DC motor. If I can, probably my issue was in a hardware and not a software.

I have ordered the chip you have sudgested. I will keep you posted if I can move the small DC motor with supplied driver.

Thanks again!

does anyone know how to move the DC motor is there is a 4.0A per phase? Looks like all the chip discussed here are for small DC motors. Can I use the MOSFETs? is it the only solution?

Getting into DC Motors... A lot of questions...

Thank you!

viva_unix: Mine 298 getting really hot when I use it. It is so hot that I cannot touch it. Can I use Toshiba ULN2803APG chip for this stepper?

How can I know what chip is good for this stepper or any other NEMA DC motor?

Thank you!

What supply voltage are you using? That datasheet says 5V 1.1A, so something like a 7.2V supply is needed (the L298 drops 2.5V or so). Too much voltage and you'll cook the motor and L298.

MarkT: What supply voltage are you using? That datasheet says 5V 1.1A, so something like a 7.2V supply is needed (the L298 drops 2.5V or so). Too much voltage and you'll cook the motor and L298.

Strikes me that one of the hassles with the 298 is that it can drop almost 5V if my memory serves, at high current. So you may need an input voltage that allows for that, but then when it's not drawing high current and so not incurring that loss, you might be passing too many volts to the motor.

I have just recently bought an Ardiuno + stepper + L298N controller. I will share with you what (I think) I have learned. If I get anything wrong, I expect someone will be able to correct me.

Your wiring looks fine. Also, I think the L298N controller you have should be fine with your stepper as far as simple controllers go.

Have you tried any of the Arduino examples? (File -> Examples -> Stepper)? They worked fine for me. I added to them as I needed (e.g., the ENable functionality on the driver board).

What are you using for a supply? The stepper motor can draw lots of current and a weak supply will collapse when it does (more on this later).

I used the Stepper motor to drive the rear wheels of a remote control car I was building. The stepper/L298N combination didn't work well for various speeds. I am waiting on delivery of a common DC motor to replace it. The problem is this: When voltage is applied to a winding in the stepper motor, not much current is drawn initially due to the inductor 'back-EMF'. If the voltage remains (when the pulse is long.. if the motor is spinning slowly), the back-EMF collapses and a lot of current flows (limited only by the winding resistance). I think pulses are generally kept short and not much current flows, so the supply voltage is often higher than spec to push more current through, to get the necessary torque (that's why a 4 or 5V motor will be used in a 12V system). The problem is, when the pulses are long (like when my car drives slowly), the back-EMF collapses and a lot of current is drawn, 3 time more with 12V on a 4V motor, then double again since there are two windings (10A in my case)! I then used the ENable input on the driver to turn it off at very low speeds. I bought a 12V stepper so that these current would be lower, but now I find that it doesn't run very well at all (I think due to quite low torque at normal speeds, due to the low current). I think a more sophisticated driver will have current limiting, so that long pulses aren't such a problem, and the driver can continue working when stopped to keep the stepper from moving.

Not sure if any of this helps, and corrections are welcome. But there you have it: my two cents.

Please share what you get. i have "destructed" at my work and cannot try a lot now. I will continue on weekend. I will try sample and a small stepper to see how it is working. I do have an external power supply for the logic and the separate supply for my motor. Separate DC motor supply is 5A. Logic is 1.5A. I do have a common ground between them. Thank you!

JimboZA:
Strikes me that one of the hassles with the 298 is that it can drop almost 5V if my memory serves, at high current. So you may need an input voltage that allows for that, but then when it’s not drawing high current and so not incurring that loss, you might be passing too many volts to the motor.

According to the datasheet it drops about 1.6V to 3.7V, depending on load upto 2A
max (at which point its probably going to melt anyway)

All darlington H-bridges have the same behaviour because each output darlington has
a Vsat of 0.9 to 1.5V or so, and there are two of them.

This is why darlington drivers are a poor choice for low voltage motors (< 24V),
as the maximum effciciency achievable is poor to abyssmal Compare with a
MOSFET H-bridge like the LMD18200T, which Vsat of 0.3V at 1A, so 0.6V lost.
However that device has a minimum supply voltage of 12V so can’t be used for
6V motors. Its also expensive and is only a single H-bridge, not dual.

The VNH2SP30 and related devices are several MOSFETs and a driver chip in
one package and pololi have some nice breakout board for these devices which
work from 5.5V up

MarkT:

JimboZA:
298 is that it can drop almost 5V if my memory serves, at high current.

According to the datasheet it drops about 1.6V to 3.7V, depending on load upto 2A
max (at which point its probably going to melt anyway)

I’m sticking with 5

L298 drop v2.png

Hello!

I was able to control the DC motor. I got some junky Chinese 28BYJ-48 that I connected to the Due and with the help of Stepper example rotated clockwise. By some reason it failed to rotate counterclockwise... I am not sure why...

monkey-man: What are you using for a supply? The stepper motor can draw lots of current and a weak supply will collapse when it does (more on this later).

I used the Stepper motor to drive the rear wheels of a remote control car I was building. The stepper/L298N combination didn't work well for various speeds. I am waiting on delivery of a common DC motor to replace it. The problem is this: When voltage is applied to a winding in the stepper motor, not much current is drawn initially due to the inductor 'back-EMF'. If the voltage remains (when the pulse is long.. if the motor is spinning slowly), the back-EMF collapses and a lot of current flows (limited only by the winding resistance). I think pulses are generally kept short and not much current flows, so the supply voltage is often higher than spec to push more current through, to get the necessary torque (that's why a 4 or 5V motor will be used in a 12V system). The problem is, when the pulses are long (like when my car drives slowly), the back-EMF collapses and a lot of current is drawn, 3 time more with 12V on a 4V motor, then double again since there are two windings (10A in my case)! I then used the ENable input on the driver to turn it off at very low speeds. I bought a 12V stepper so that these current would be lower, but now I find that it doesn't run very well at all (I think due to quite low torque at normal speeds, due to the low current). I think a more sophisticated driver will have current limiting, so that long pulses aren't such a problem, and the driver can continue working when stopped to keep the stepper from moving.

Not sure if any of this helps, and corrections are welcome. But there you have it: my two cents.

I started experimenting with some voltage and current. It appears you are right! I was able to rotate the NEMA23 DC motor with 12V and 1A+ current. The voltage dropped to 4V and current was staying around 1.35A I am kind of confused on what should I do now. If this motor is using 8A of current, how to drive it on a remote car for example? What DC motor are you buying as a replacement? Anyone can add/comment on DC motor and current usage? Any educational links?

Thank you!

Hello again,
I have to warn you, I am pretty new to Arduino and will have many failures before I get success. So don’t assume anything I do or say to be accurate, or to be the best way to tackle a problem, or to even work!
Having said that, this is what I did to get my stepper motor spinning in both directions, it was a modification of the stepper_speedControl example provided in the IDE:

#include <Stepper.h>

const int stepsPerRevolution = 200;  
Stepper myStepper(stepsPerRevolution, 8,9,10,11);            
int MotorDriver = 12;  // Use this pin to disable motor driver at low speeds (to save power)
int led = 13;          // There is a LED on the Arduino board connected at pin 13.
int stepCount = 0;  // number of steps the motor has taken
int SpeedPin = A1;  // Analog input for speed control
int SpeedReading;
int MotorSpeed;

void setup() {
  pinMode(MotorDriver, OUTPUT);
  pinMode(led, OUTPUT);
  digitalWrite(MotorDriver, LOW);  // initialise Motor Driver to be off
  digitalWrite(led, LOW);          // debug, to see what MotorDriver is doing
}

void loop() {
  // read the sensor value:
  SpeedReading = analogRead(SpeedPin);
  // map it to a range from ...
  MotorSpeed = map(SpeedReading, 0, 1023, -200, 200);
 
  if (MotorSpeed > 10) {
    digitalWrite(MotorDriver, HIGH);  // Enable Motor Driver
    digitalWrite(led, HIGH);          // debug
    myStepper.setSpeed(MotorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution/100);
  } 
  else if (MotorSpeed < (-10)) {
    digitalWrite(MotorDriver, HIGH);
    digitalWrite(led, HIGH);  
    myStepper.setSpeed(-MotorSpeed);
    myStepper.step(-stepsPerRevolution/100);
  }
  else {
 //  Turn off the Motor Driver so as not to waste power. Use pin 12 to drive Motor Controller Enable
   digitalWrite(MotorDriver, LOW);
   digitalWrite(led, LOW);
  }
}

I deleted some comments, but I think it’s all there.
So I mapped the analog input to a range from -X to +X, not just 0 to +X. I had a look at Stepper.ccp to try to figure out how to reverse the motor. From memory I think the myStepper.setSpeed function requires the number passed to it to be positive (because it determined the pulse delay… and a negative delay doesn’t make sense), so when MotorSpeed is negative, it must be made positive. The reversal of direction is then achieved by making the value passed to the myStepper.step function negative. Whatever the case, the above code worked.

But, as I have mentioned, I am ditching the stepper motor for a DC motor. I plan to use the same L298N to drive it. I have ordered this motor:
http://www.ebay.com.au/itm/130580506675?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649
but I expect it will still be a couple of weeks away.
A friend of mine bought one to drive a 16mm camera mechanism. At 12V it gives the 300rpm or so. A quick check (not precise) showed it drew around about 100mA with no load, but when holding onto the shaft it rose to about 400mA (depending how hard you hold it). And it turns both ways. Should be ok I think. For $10 it’s worth a shot (it’s just a shame I have to wait so long to get it).