DC Motor runs at powerup

And it shouldn’t be.

Hardware is a mega controlling a H-bridge and running a standard DC motor.

This is being controlled wirelessly but it doesn’t matter if my transmitter is powered up or not.

Whenever I power up the receive side arduino, the motor will run full on for a good 14 seconds or so. Once the motor stops, and with my transmitter powered up, the motor does work correctly.

I have the three control pins, two power, one control, set to output and the two power pins set to low on the receive setup.

The three motor pins are in1 and 2 for power(direction) and ena for the control.

What am I missing?

This is the receive side code

int ch8;
int fspeed;           // forward speed
int bspeed;           // backward speed
const int in1 = 22;    // direction pin 1
const int in2 = 24;    // direction pin 2
const int ena = 44;    // PWM pin to change speed

void setup(){
  
  Serial.begin(38400);
  Serial3.begin(38400);

  pinMode(in1, OUTPUT);      // connection to L298n
  pinMode(in2, OUTPUT);      // connection to L298n
  pinMode(ena, OUTPUT);      // connection to L298n

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

void loop() {

  while (Serial3.available()) {
    ch8 = Serial3.read();
    Serial.println(ch8);
  }

  if (ch8 < 124)              // joystick forward
  {
    fspeed = (map(ch8, 123, 0, 0, 255));
    forward(fspeed);
  }

  if (ch8 > 126)              // joystick backward
  {
    bspeed = (map(ch8, 127, 255, 0, 255));
    backward(bspeed);
  }
  if (ch8 >= 124 && ch8 <= 126)    // joystick is centered
  {
    stop();
  }

}
void stop()

{
  //analogWrite(ena, 0);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}
void forward(int fspeed)

{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(ena, fspeed);
}
void backward(int bspeed)

{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(ena, bspeed);

}

I can post the transmit side if needed but since it happens regardless of it being on or not, I wasn’t sure if it was necessary.

Thanks

You can use pull up or down resistors, to apply the "off" state signals to the actuators, until the pins are configured as outputs.

Perhaps also try initialising ch8 to the "stopped" value when you declare it. At the moment its an undefined value until you receive the first character from the transmitter.

Hi,

After you make sure two of the outputs are OFF in setup, why haven't you done the same with enable? analogWrite(ena,0);

Tom... :)

Thanks

DrDiettrich: You can use pull up or down resistors, to apply the "off" state signals to the actuators, until the pins are configured as outputs.

I will dig out some resistors tomorrow to try.

rw950431: Perhaps also try initialising ch8 to the "stopped" value when you declare it. At the moment its an undefined value until you receive the first character from the transmitter.

I did try this, at least if I am understanding right, "int ch8 = 125" ?

TomGeorge: Hi,

After you make sure two of the outputs are OFF in setup, why haven't you done the same with enable? analogWrite(ena,0);

Tom... :)

I did actually have that originally. I tried 0 and 125(stop value). I also changed it to digitalWrite low just for kicks and neither way seemed to make a difference unfortunately. Edit, I actually don't know why it's not in the code up top. Been trying a bunch of things just messing around. must have forgot to put it back lol.

What I thought was kinda odd, but I am still learning here, it only does it on initial powerup but doesn't do it if I do a reset on the board. Reset is doing something different than powerup?

What happens if you use this setup. Leo..

void setup(){
  pinMode(ena, OUTPUT);      // connection to L298n
  digitalWrite(ena, LOW);

  Serial.begin(38400);
  Serial3.begin(38400);

  pinMode(in1, OUTPUT);      // connection to L298n
  pinMode(in2, OUTPUT);      // connection to L298n

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

Ok, I defiantly got something strange going on here.

I have the motor/h-bridge running off battery and have been powering the board itself mostly from the usb, although I have been using the battery for the board occasionally instead of the usb.

My original code was giving me problems regardless of the power.

The suggestion from Wawa seemed to work initially on battery but not on usb, but then it did work with usb power, but only once. :confused:

The strange part, my original code now actually seems to be working properly with either power option. :astonished:

The bummer part unfortunately, this code is one part of a larger code set, and in that set none of the changes made a difference. But on that end, the larger code set does have another bug I am still trying to work out as well which I am now starting to think is a bit bigger than I thought. :(

Hi,

I did actually have that originally. I tried 0 and 125(stop value). I also changed it to digitalWrite low just for kicks and neither way seemed to make a difference unfortunately.

I hope 125 was a typo, 255 would be full 100% duty.

Tom..... :)

TomGeorge: Hi,I hope 125 was a typo, 255 would be full 100% duty.

Tom..... :)

125 is actually my stop value. It would be center on my joystick controlling the motor. I had hoped it would have read as that on startup.

:)

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you also post a picture of your project so we can see you component layout.

What H-Bridge do you have?

Thanks .. Tom... :)

This is the best I could do for a diagram at the moment. It is pretty easy tho. Components are Mega, Xbee, L298N motor controller, DC Motor and 12v battery and 12v-5v converter.

12v to + - on motor controller.
Motor to M1, M2 on motor controller
Controller ena, in1, in2 to Mega 44, 22, 24
12v-5v to vin, gnd on mega
Xbee to 3v, gnd, 14, 15 on mega

This is the motor controller
Controller

12v-5v converter
Converter

!(http://<a target=)">

Hope that helps

V-in needs to be at least 6volt. It is the input of the onboard 5volt regulator. With 5volt on V-in, the MCU will likely get only 4volt.

If you trust that external 5volt, connect it directly to the 5volt pin of the Mega. Leo..

Wawa: V-in needs to be at least 6volt. It is the input of the onboard 5volt regulator. With 5volt on V-in, the MCU will likely get only 4volt.

If you trust that external 5volt, connect it directly to the 5volt pin of the Mega. Leo..

Thanks I didn't know you could actually do that. I thought they were only output. I was powering it with 12v directly through the barrel jack origionally but the regulator was getting way too hot for my liking.

I will give that a try.