Hi,
I am learning to program with arduino.
Now I am trying to make a 24v 300w dc brushed motor turn to an input setpoint.
I have tried to use a couple of cheap aliexpres moto monster motor drivers, these however where as good as dead on arrival. I also used a couple of double bts 43A motor drivers, same problem.
But I also ordered a Dual-Motor-Driver-Module-Board-H-bridge-DC-MOSFET-IRF3205-3-36V-10A
I used his motor driver with heatsinks attached en as a single h-bridge by connecting the dir en pwm pins to 2 pins instead of 4. I also added thicker traces with tin on the wider copper leads.
my power supply is 12v 40a with diode's so no higher voltage is given back on the +.
This was all working fine yesterday with slowly but steadily increasing the pwm value and direction change.
#define ENCODER_OPTIMIZE_INTERRUPTS
#define pwmpin 10
#define dirpin 12
#include <Encoder.h>
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(2, 3);
// avoid using pins with LEDs attached
void setup()
{
Serial.begin(250000);
Serial.println("Basic Encoder Test:");
// set timer for 31.000hz pwm only pin 9 and 10.
int myEraser = 7;
TCCR2B &= ~myEraser;
int myPrescaler = 1;
TCCR2B |= myPrescaler;
}
long oldPosition = -999;
int pwm;
int target;
int error;
int minpos = 0;
int maxpos = 30000;
int maxerror = 250;
int minpwm = 60; //increasing these values for testing
int maxpwm = 240;
void loop()
{
long newPosition = myEnc.read();
if (newPosition != oldPosition)
{
oldPosition = newPosition;
}
target = map(analogRead(A0),0, 1023, minpos ,maxpos);
error = abs(newPosition - target);
pwm = map(error,minpos, maxpos, minpwm ,maxpwm);
Serial.print(" Error:");
Serial.print(error);
Serial.print(" PWM:");
Serial.print(pwm);
Serial.print(" Target:");
Serial.println(newPosition);
if(newPosition >= target)
{
if (error <= maxerror)
{
digitalWrite(dirpin, LOW);
analogWrite(pwmpin, 0);
}
else
{
digitalWrite(dirpin, LOW);
analogWrite(pwmpin, pwm);
}
}
else if (newPosition <= target)
{
if (error <= maxerror)
{
digitalWrite(dirpin, HIGH);
analogWrite(pwmpin, 0);
}
else
{
digitalWrite(dirpin, HIGH);
analogWrite(pwmpin, pwm);
}
}
}
But today i tried using PID, and i'l be honest. I'm not understanding it completely.
So my h-bridge burnt out, at least one of the irf3205 smoked and now its shorted out.
// PID motor position control.
// Thanks to Brett Beauregard for his nice PID library http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/
#include <PID_v1.h>
#include <Encoder.h> // Quadrature encoder B pin
#define M1 12 // PWM outputs to L298N H-Bridge motor driver module
#define M2 10
Encoder myEnc(2, 3);
double kp = 0.3 , ki = 0.02 , kd = 0.01; // modify for optimal performance
double input = 0, output = 0, setpoint = 0;
long temp;
PID myPID(&input, &output, &setpoint, kp, ki, kd, DIRECT); // if motor will only run at full speed try 'REVERSE' instead of 'DIRECT'
long oldPosition = -999;
void setup() { // update encoder position
int myEraser = 7;
TCCR2B &= ~myEraser;
int myPrescaler = 1;
TCCR2B |= myPrescaler;
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(1);
myPID.SetOutputLimits(-254, 254);
Serial.begin (115200); // for debugging
}
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition)
{
oldPosition = newPosition;
}
setpoint = map(analogRead(A0),0,1023,0,18414); // modify to fit motor and encoder characteristics, potmeter connected to A0
input = newPosition ; // data from encoder
// Serial.println(encoderPos); // monitor motor position
myPID.Compute(); // calculate new output
pwmOut(output); // drive L298N H-Bridge module
}
void pwmOut(int out) { // to H-Bridge board
if (out > 0) {
digitalWrite(M1,HIGH ); // drive motor CW
analogWrite(M2, out);
Serial.println(out);
}
else {
digitalWrite(M1, LOW);
analogWrite(M2, abs(out));
Serial.println(out);// drive motor CCW
}
}
But I don't understand completely why, the current wasn't all that high, it was just oscillating a bit and then it shorted out.
It was drawing a lot more current before, at least 30A because I had a 30A fuse in between the psu + leads and it popped. After i put a new one in the h-bridge begane smoking after a while. This was after I set the kp ki and kd way down.
The only reason I can think of is setting the pwm to 248~ max, because the info on the aliexpress page says a duty cycle of 0 to 98% pwm. Or maybe that the motor gave back i higher voltage than 55v wich is the threshold voltage of the mosfets.
So my question is should I try again with one of those irf3205 h-bridges or maybe there is a drop in replacement for the irf3205 mosfet. Or should I ad better cooling? Al tho the mosfets weren't realy hot to the touch before the burnout. Hopefully I gave enough information.
thanks,