Motorshield AnalogWrite functioning badly

Hi,
Using arduino Uno, arduino motorshield, and a simple dc motor (not important because I change the motor I test on constantly), and a bench power system 10v.

I run simple code which I will attach, and I have the problem where with one call of the AnalogWrite(), the motor moves in two increments no matter what. When I call for the motor to go 'up', it goes up briefly, stops then continues going up for the specified delay time. Same happens when i call it to go down.

I have tried putting a PWM of 0 at the start of the code to initialise it, and it still does the same thing. When i put a multimeter over the motorshield, and a 50 percent duty cycle, whenever I run the code i can see the motor channel A driving pins briefly go up to 5V, drop to 0 then go back to 5v.

Any idea why or how I can prevent it as this 'jitter' is not something I can account for during my project.



const int directionPin = 12;
const int pwmPin = 3;
const int brakePin = 9;


void setup() {

  pinMode(directionPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
  pinMode(brakePin, OUTPUT);
  analogWrite(pwmPin, 0);

  // Perform initial actions (for example, go down)
  digitalWrite(directionPin, HIGH); // Set direction to go up
  analogWrite(pwmPin, 125);
  delay(1000);
  analogWrite(pwmPin, 0);

}

void loop() {
}

void goUp() {
  digitalWrite(directionPin, LOW); // Set direction to go up
  analogWrite(pwmPin, 255); 
}

void goDown() {
  digitalWrite(directionPin, HIGH); // Set direction to go down
  analogWrite(pwmPin, 255);          // Start the motor
}

void brake() {
  analogWrite(pwmPin, 0);
      // Stop the motor
}

Your sketch does not call any of the functions that control the motor. Please post one that does

It would also help if you posted a schematic of your project

How are the motors powered ?

Try to call the functions (goUP and goDown) inside the loop, currently it is not doing that, that's why the motors stop after a while

I decided to avoid the functions for debugging purposes, that is why i didn't include it in this snippet of code. There is no need to use the function as I am directly calling AnalogWrite.

Motor is connected directly to the arduino motorshield.
Arduino motorshield powered by bench power supply.

That is not the problem, thank you for your response though. The problem is that the first time I run the code and call analogwrite, I notice there is a 'bounce'. As in I call analogwrite 255 dutycycle once and then set the analogwrite PWM to 0, but on the oscilloscope I can see 2 changes like as below
Capture

This bounce happens only on the first movement of the motor as I run the code, however I have tried manually entering PWM values into the register as well and it still does the same thing.

Try using digitalWrite (pwmPin, LOW) instead of analogWrite(pwmPin, 0)
Sometimes PWM 0 doesn't go all the way to zero.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.