H-bridge LMD18200

Hi all,

I’m using an h-bridge chip (LMD18200) in my senior project (rotary inverted pendulum) and an arduino mega 2560 board. As shown in logic truth table of datasheet, combining logic states of dir,brake and pwm pins you can get motor to spins clockwise, counterclockwise or just stop it. I set pins just using digitalWrite function.

void motor_CW ()/* motor spins clockwise direction*/
{
  digitalWrite(dir,HIGH);
  digitalWrite(pwm,HIGH);
  digitalWrite(brake,LOW);
}
void motor_CCW ()/*motor spins counter clockwise direction*/
{
  digitalWrite(dir,LOW);
  digitalWrite(pwm,HIGH);
  digitalWrite(brake,LOW);
}
void motorStop ()/*motor brakes*/
{
  digitalWrite (dir, LOW);
  digitalWrite (pwm, LOW);
  digitalWrite (brake, HIGH);
}

In that way you get motor spining full load (100% pwm) at one side or another, but what if I want a pwm variable (duty-cycle variable) ? to solve this should I just change this ‘digitalWrite(pwm, HIGH)’ to this ‘analogWrite(pwm,value)’ ?? or instead of just add the line analogWrite like this

void motor_CW ()/* motor spins clockwise direction*/
{
  digitalWrite(dir,HIGH);
  digitalWrite(pwm,HIGH);
  digitalWrite(brake,LOW);
  analogWrite(pwm,value); /*right here*/
}
void motor_CCW ()/*motor spins counter clockwise direction*/
{
  digitalWrite(dir,LOW);
  digitalWrite(pwm,HIGH);
  digitalWrite(brake,LOW);
  analogWrite(pwm,value); /*right here*/ 
}
void motorStop ()/*motor brakes*/
{
  digitalWrite (dir, LOW);
  digitalWrite (pwm, LOW);
  digitalWrite (brake, HIGH);
}

LMD18200.pdf (338 KB)

Yes - but you can also remove the digitalWrite(pwm, HIGH) call from those functions if you are doing analogWrite().

Any call to analogWrite or digitalWrite will supersede any previous call of either for that pin.

You might want to add a function called "setSpeed()" too, to set value and call analogWrite().

And lastly don't do what I did with an LMD18200 and accidentally touch the 12V supply to one of the inputs(!)