Controlling DC motor using PWM

Hi everyone I'm trying to control a DC motor with this code but for some reason analog_write function does not do anything taking values from 0 to 255 the speed doesn't shift . using arduino MEGA2560

// Pins
#define ENCA 13
#define ENCB 4
#define PWM 13
#define IN1 9  
#define IN2 8
#define IN3 7
#define IN4 6

void setup() {
  Serial.begin(115200);

  pinMode(ENCA,INPUT);
  pinMode(ENCB,INPUT);
  pinMode(PWM,OUTPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);

}

void loop() {
  int pwr = 10;
  int dir = 1;
  setMotor(dir,pwr,PWM,IN1,IN2,IN3,IN4 );
}

void setMotor(int dir, int pwmVal, int pwm, int in1, int in2,int in3,int in4 ){
  analogWrite(pwm,pwmVal); // Motor speed
  if(dir == 1){ 
    // Turn one way
    digitalWrite(in1,HIGH);
    digitalWrite(in3,HIGH);
    digitalWrite(in2,LOW);
    digitalWrite(in4,LOW);
  }
  else if(dir == -1){
    // Turn the other way
    digitalWrite(in1,LOW);
    digitalWrite(in2,HIGH);
    digitalWrite(in3,LOW);
    digitalWrite(in4,HIGH);
  }
  else{
    // Or dont turn
    digitalWrite(in1,LOW);
    digitalWrite(in3,LOW);
    digitalWrite(in2,LOW);  
    digitalWrite(in4,LOW);  
  }
}

A schematic would be useful, but a PWM value of 10 is pretty low and may only cause the motor to hum.

Also, this

#define ENCA 13
...
#define PWM 13
1 Like

the 10 value is just testing doesn't matter what value I give it the speed is constant , m using a motor driver L298N and I thought ENCA pin should be same as pwm ? cuz I dunno what pin I should give pwm .

All you need to know

You don't see a problem with this

1 Like


Figure-1:

Fig-1 indicates that DPin-13 is not supported by analogWrite().function.

@GolamMostafa

Referring to the following page, I have learnt now that the ananlogWrite() function does support DPin-13 of Arduino MEGA.

Why have you used the same DPin-13 simultaneously for two devices?

I do , the thing is I need the PWM pin yet they say that ENCA is pwm so m really lost

Who is "they"?

the L298N driver datasheet

I can't see any reference to ENCA in the L298 datasheet that I have.

it's enA same thing

Hi,
Can you draw a circuit diagram of your project and post an image of it?
Include power supplies, component names and pin labels.

An image(s) of your project will also help.

This will help us see what your project consists of.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:


So I changed the pins IN1,IN2,IN3 and IN4 to the pins u see in the code , I'm not using a battery for the moment so I wired the +12V of the motor drive to the 5V arduino sharing same ground, left the +5V pin of the motor drive unwired , ENCA and ENCB ( not showing in image i'll add them later ) to pin 13, 11 respectivly , that's it the code is just to command the motor with pwm without speed encoder etc

And you're puzzled that motor doesn't move?

Your schematic shows a Uno, but you said you're using a Mega2560.

(And no, ENCA and ENA are not the same thing)

I am using a mega this is the old schematic where I used a uno , the pins are still the same

We've already established that that is not the case.

Hi,

Why have you got SDA and SCL of the I2C connected to pins 11 and 12 of the UNO?

How about you redraw your circuit to what you really have, and not post a circuit that then has amendments?

Post your code for Mega and code for UNO please.
Post schematic for Mega and schematic for UNO please.
That way there will be no confusion.

Thanks.. Tom... :smiley: :+1: :coffee: :australia: