1

I am making a pid line follower robot. So I was tinkering with code to optimize my line follower when i removed two lines of code unknowingly. As soon as I uploaded my code and started the line follower, 4 mosfets on my motor driver smoked and burst into blue flames. Bought a new one and same outcome. Interestingly, The ones corresponding to the left motor alone had this outcome as I removed " return Motorspeed1" and " return MotorSpeed2" from the function corresponding to the left motor. Heres the code :

signed int MotorSpeed = 70;
int MotorSpeed1, MotorSpeed2;
int MotorFunc(signed int a, signed int b){
   if(a >= -10 && a < 0)
   {
      b = -b;
      digitalWrite(_brk_m1, LOW);
      digitalWrite(_brk_m1, LOW);
      digitalWrite(_dir_m1, HIGH);
      digitalWrite(_dir_m2, LOW);
      MotorSpeed1 = MotorSpeed + b;
      MotorSpeed2 = MotorSpeed + b;
      MotorSpeed1 = constrain(MotorSpeed1, 0, 255);
      MotorSpeed2 = constrain(MotorSpeed2, 0, 255);
      analogWrite(_pwm_m1, MotorSpeed1);
      analogWrite(_pwm_m1, MotorSpeed2);
      return MotorSpeed1;
      return MotorSpeed2;
   }
   if (a < 10 && a > 0)
   {
      digitalWrite(_brk_m1, LOW);
      digitalWrite(_brk_m2, LOW);
      digitalWrite(_dir_m1, LOW);
      digitalWrite(_dir_m2, HIGH);
      MotorSpeed1 = MotorSpeed+ b;
      MotorSpeed2= MotorSpeed+ b;
      MotorSpeed1 = constrain( MotorSpeed1, 0, 255);
      MotorSpeed2= constrain( MotorSpeed2, 0, 255);
      analogWrite( _pwm_m1,  MotorSpeed1);
      analogWrite( _pwm_m2, MotorSpeed2);
      return MotorSpeed1;
      return MotorSpeed2;
   }

   else
   {
      digitalWrite(_brk_m1, LOW);
      digitalWrite(_brk_m2, LOW);
      digitalWrite(_dir_m1, HIGH);
      digitalWrite(_dir_m2, HIGH);
      MotorSpeed1 = MotorSpeed+ b;
      MotorSpeed2 = MotorSpeed - b;
      MotorSpeed1 = constrain(MotorSpeed1, 0, 255);
      MotorSpeed1 = constrain(MotorSpeed2, 0, 255);
      analogWrite( _pwm_m1, MotorSpeed1);
      analogWrite( _pwm_m2, MotorSpeed2);
      return MotorSpeed1;
      return MotorSpeed2;
   }

}

And i called this function in the code using :

    MotorFunc(a,b)

This code ran just fine, But when i removed the return functions from the first if condition, I destroyed 2 motordrivers, Is this possible? Edit: I am updating the whole code:

The main code tab :

     int _dir_m1 = 2;
     int _pwm_m1 = 3;
     int _brk_m1 = 4;
     int _brk_m2 = 5;
     int _pwm_m2 = 6;
     int _dir_m2 = 7;

     int sen1 = A4;
     int sen2 = A3;
     int sen3 = A2;
     int sen4 = A1;
     int sen5 = A0;
     signed int Error = 0;


     int Kp = 20;
     int Ki = 0;
     int Kd = 0;


     void setup() {
     pinMode(_dir_m1, OUTPUT);
     pinMode(_pwm_m1, OUTPUT);
     pinMode(_brk_m1, OUTPUT);
     pinMode(_dir_m2, OUTPUT);
     pinMode(_pwm_m2, OUTPUT);
     pinMode(_brk_m2, OUTPUT);

     pinMode(sen1, INPUT);
     pinMode(sen2, INPUT);
     pinMode(sen3, INPUT);
     pinMode(sen4, INPUT);
     pinMode(sen5, INPUT);

     } 

     void loop(){

     signed int a = Sensors();
     signed int b = PID(a);
     MotorFunc(a,b);
       }

And pretty much it. I am leaving out PID as Its not required along with error selection.

The setup is - Arduino mega connected to 5 ir sensors 12v 9ah battery 500rpm 12v motors johnsons and the motor driver 20 amp 20v-30v Motor Driver which burst into flames

Motor driver link with specs

6
  • 1
    You can't have two return statements after each other. So there is already something very wrong here.
    – Gerben
    May 11, 2017 at 16:52
  • @Gerben I suspect he isn't using those returned values, since they are defined as global variables anyway.
    – Majenko
    May 11, 2017 at 17:07
  • @Gerben I dont know why but the program refuses to work without all those return statements. Any idea how it can burn a mosfet by its absence? May 13, 2017 at 14:49
  • Is it possible that i shorted two motor inputs by not calling return in on all the statements? I didnt call the return statement in all the functions once but that didnt make the driver burn, just that the program wasnt behaving as needed. @Majenko May 13, 2017 at 14:51
  • 1
    Without seeing your entire program and knowing all your wiring it's impossible for us to know that kind of thing. Yes, it's possible. Is it what happened? Who knows?
    – Majenko
    May 13, 2017 at 14:53

2 Answers 2

3

It all depends on how the circuit works and how it is controlled. A basic H-bridge looks like this:

enter image description here

Normally you have one MOSFET from each side turned on and one turned off - so for example Q1 and Q4 on to make the motor turn one way, or Q2 and Q3 to make it turn the other. Turning on Q2 and Q4 for example would make the motor "brake".

However, if it is possible for your driver to turn on both Q1 and Q2 at the same time, or Q3 and Q4, then they will form an almost dead short circuit and massive amounts of current will try to flow. At that point things will start to get hot and the Magic Smoke™ will try and escape.

0

It isn't possible to say arbitrarily that removing the statements could or could not cause the drivers to burn out. What does the caller do with the value returned by your function? With the return statement removed, the value returned to the caller is unknown. Is there any value which, if returned to the caller, could set the conditions for driver burn-out?

2
  • Is it possible that i shorted two motor inputs by not calling return in on all the statements? I didnt call the return statement in all the functions once but that didnt make the driver burn, just that the program wasnt behaving as needed May 13, 2017 at 14:51
  • A function will return if control reaches the end the final '}', even without a return statement.
    – JRobert
    May 13, 2017 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.