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);
}
}
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 .
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