Motordriver with PWM


#1

Hi, i’m trying to run a motor with variable speeds via a motor driver. http://www.robotpark.com/Mini-L298N-Motor-Driver by sending a PWM signal via pins A4 and A5.

Wiring: i have connected A4 to IN1 on the driver and A5 tot IN2. Vin is connected tot the + on the driver and Gnd to the -. The motor is connected to both connections of Motor-A on the drive.

This is the code i am using.

int IN1 = A4;
int IN2 = A5;

void setup()
{
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT); 
}

void Loop() {
 analogWrite(IN1,255);
 analogWrite(IN2,0);
}

Any idea’s what is wrong?

Greetings,
Just


#2

What device are you using?
How are you powering the device?
What motors are you using?
Are you sure Vin can supply enough current for the motor to turn?


#3

I’m using a Photon powered via a relay shield. The motor is a small fan which I succesfully got turning on full speed by controling it with the same shield via digital outputs (HIGH/LOW).


#4

Try not to rewrite the same value over and over again.

Although that issue was addressed a long time ago, there were device OS versions that always kept reinitialising the PWM counter whenever analogWrite() was called messing up the actual duty cycle.
What device OS version are you targeting?

You can try something like this instead

int IN1 = A4;
int IN2 = A5;
int speed = 255;

void setup()
{
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT); 

  analogWrite(IN1, speed);
  digitalWrite(IN2, LOW);

  Particle.function("setSpeed", setSpeed);
}

void loop() {
}

int setSpeed(const char* arg) {
  speed = constrain(atoi(arg), -255, 255);
  if (speed > 0) {
    digitalWrite(IN2, LOW);
    analogWrite(IN1, speed);
  }
  else if (speed < 0) {
    digitalWrite(IN1, LOW);
    analogWrite(IN2, -speed);
  }
  else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
  }
  return speed;
}

But the main point I guess is that you have a capital L in Loop() which is not loop() and hence will not be called by the main() function :wink:


#5

A capital L. Really? And this i don’t get an error message for from the compiler? Thx a million! Problem solved. Everything works now.


#6

Nope, why should it?

You can declare any function name you like (as long it’s not already taken) and you can have a completely “empty” or non-existent loop() since main() checks if(loop) loop(); (does it exist, then call, otherwise don’t :wink: )

You can flash code that doesn’t implemente setup() either.
Try opening Web IDE, delete all contents and build - you’ll get no errors.
That’s robust programming an the side of the device OS :wink: