Hi.
I'm trying to understand and develop code that will operate my 12v DC motor in both clock-wise and counter clock-wise directions. Additionally, I would like to control the speed of the motor (in both directions) using a single potentiometer.
I need to have the Arduino control the whole process so, 3rd party - off the shelf modules - will not do.
I have a stock of L298N DC motor drivers...2 of which I have hosed already, attempting to use a 24v PSU. Lesson learned the whole thing runs fine off a 12v unit.
I'm only using Motor 2 side of the L298N (leaving Motor 1 side disabled) but I'm not sure I have the pin assignments on the bridge or the UNO connections correct? I have the pot controlling speed but I can't figure how to drive the motor in reverse using the current code.
I'm happy to post my sketch thus far, once I've figured out how to
Update...sketch posted below. Noob so apologies for disjointed post
What is the stall current spec of your motor? You must select the motor driver for the motor based on the motor supply voltage and stall current. The L298 may not be appropriate for your motor. Actually, in my opinion the L298 is not a very good motor driver. The L298 is ancient and inefficient technololgy. There are far better modern MOSFET output drivers available.
Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
[code]
// Controlling a 12v DC Motor Speed Using a Potentiometer via L298N H Bridge
int pot1pin1 = 0; // select A0 as the input pin for potentiometer 1
int val = 0; // variable to store the value coming from the sensor
char receivedCommand; // character for controlling the serial program
// Motor Pin Assignments
const int enB = 3; //Enable (PWM) '~' --- Swapped order and changed pin assignment from 5.
const int in4 = 2; //Enable motor direction (Power) --- Swapped order and changed pin assignment from 8.
const int in3 = 8; //Enable opposite motor direction (Power) --- Assignment pin 8.
bool enableRun = false;
bool newData = false;
// Setup function (Run Once)
void setup()
{
pinMode(enB, OUTPUT); // Motor 2, motor 1 is disabled
pinMode(in4, OUTPUT); // direction
pinMode(in3, OUTPUT); // opposite direction
Serial.begin(9600);
Serial.println("CONTROLING MOTOR SPEED VIA ARDUINO PIN A0 ASSIGNED TO POT 1");
}
void loop()
{
readSerialPort();
readPot1pin1();
}
void readSerialPort()
{
if (Serial.available() > 0) //is there a data on the serial?
{
receivedCommand = Serial.read();
newData = true; //if we received a new command we set this bool to true
}
if (newData == true)
{
if (receivedCommand == 'r') // --- RUN ---
{
enableRun = true; //start_run
//Problems here...
//For Clock-wise motion: in3 = HIGH; in4 = LOW
digitalWrite(in3, HIGH) ;
digitalWrite(in4, LOW) ;
analogWrite(enB, 75) ;
delay(3000) ;
//brake
digitalWrite(in3, HIGH) ;
digitalWrite(in4, HIGH) ;
delay(1000) ;
//For Counter Clock-wise motion: in3 = LOW; in4 = HIGH
digitalWrite(in3, LOW) ;
digitalWrite(in4, HIGH) ;
delay(3000) ;
//brake
digitalWrite(in3, HIGH) ;
digitalWrite(in4, HIGH) ;
delay(1000) ;
//...Problems end
}
if (receivedCommand == 's') // --- STOP ---
{
enableRun = false; //shut down everything
digitalWrite(in4, LOW);
}
}
newData = false; //reset newData to false so we can receive new command
}
void readPot1pin1()
{
if (enableRun == true) //is running enabled?
{
val = analogRead(pot1pin1); // read the value from A0
val = map(val, 0, 1023, 0, 255); //map 0-1023 to 0-255 ; |-----------| ---> |------|
analogWrite(enB, val); //write PWM (pin ~3)
digitalWrite(in4, HIGH); //enable power
Serial.println(val); //output to serial monitor
}
else
{ //do nothing, disable power if it was enabled previously
enableRun = false;
digitalWrite(in4, LOW);
}
}
[/code]
Hi. I began this project a couple of years or so ago. I have to admit being completely green behind the ears when I did so. I had no experience with Arduino, electrical engineering, specifications etc. However, I had the foresight not to splash cash on expensive, cutting edge technology. I opted for cheap parts knowing full well that I was highly likely to damage stuff. I take your point re. the L298N but I'm happy to use it until I get better at this kind of project.
The project (a pickup coil winder btw) has remained dormant until recently due to various circumstances.
The motor itself is Chinese, a fairly generic Machifit/Hanpose 775 or similar with no stall current information available (as far as I can see). Sorry I can't give more info on the hardware. Thanks for your input.
You really don't say exactly what you need help with. Running the motor in the reverse direction should be as simple as inverting the path through the "H"
That said, before changing direction you should stop the PWM output and if you're going to use the "brake" function, don't exceed a 2A motor current as per the spec.
Have you tried a higher number there? That is the speed control. Try 200 or so.
If I Google Machifit/Hanpose 775 I see several different power level of maximum current all of which are far beyond what a L298 will handle (that is, perhaps, why you have killed the other 2). I recommend that you find the stall current for the motor that you have and procure an appropriate driver if the stall current is over about 1.5A.
In the absence of a data sheet, the stall current can be estimated. To estimate the stall current, measure the motor winding resistance. Take several measurements rotating the motor a bit between readings. Use the lowest reading in the calculation. The estimated stall current is the motor supply voltage divided by the measured resistance. What is the calculated current for your motor?
If you will look at the specification table on this page you can see that there is only one motor that has a stall current under the 2A maximum of the L298. It is at 1.8A which is uncomfortably close to the max.
Another strike against the L298 is the over 4V drop across the driver when delivering full current. That drop is turned into heat. Hence the huge heat sinks. So the 12V to the driver means 8V to the motor. Wasteful
Hi and thanks for the information. I was looking at the possibility of using a relay instead. I wasn't aware of the need for it to be DPDT though. I'll be looking into it
The L298 is specified for a maximum motor supply voltage of 50Vdc. Current killed them, not voltage.
Without knowing the stall current (and motor supply voltage) there is no way to make an intelligent choice. Armed with the motor supply voltage and stall current have a look at Pololu's selection of brushed DC motor drivers.
You need a DPDT to be able to reverse the motor plus a SPST (or SPDT) relay to control power. You will not be able to control speed, though, just on and off. You could use a MOSFET to control speed and the DPDT relay to control direction.
Hi and thanks for your input. Would it be safer to remove the 'brake' function altogether? It should run Ok with out it I'm presuming?
You are right. Inverting in3 and in4 from high to low and visa-versa reverses motor direction as expected but not in this sketch and this is what I need help with.
I came across this tutorial a few moments ago which has cleared the matter up a little bit. I'll compile and upload it to my Uno tomorrow and see if the introduction of the push button (and related code) helps overcome my problems with this particular sketch.
No, the stall current is also referred as the locked rotor current. It is the current drawn if you stop the rotor from turning while the motor is energized. Not good for the motor. The stall current is drawn, briefly, every time the motor starts. That is why the driver must withstand stall current.
I do think that you should confirm that by measuring the winding resistance of your motor.
There is one other parameter you will need to take care of sooner or later. That is the heat generated by that motor. The only way it will stay cool and continue to operate is if your mounting design can operate as a heat sink and move the heat away from the motor.
If the motor only operates now and then and for short times, then it will be ok without heat consideration.
Paul
Most 775 sized motors will have much higher stall currents than that, 10's of amps, I frankly don't believe that "datasheet", it sounds more like the max efficiency point current.
Data for motors is often incorrect/incomplete/missing from websites selling motors. Finding the manufacturer's datasheet is the best bet usually, which means a known brand... Or just measure the stall current or winding resistance.
This will be the case. Hopefully, I'll be using it in a coil winding machine for winding electric guitar pickups. Maybe 15-30 minutes at a time and by the look of things, only a small percentage of it's maximum rpm. Thanks
I'm working on it. I did see a datasheet for a motor like this with a stall current of 5.5A so yes, datasheets can lie. Measuring the stall current...I'm working on that too but I need a dummies guide