Hello ,
I want to control the speed and direction of a 10W dc Motor.
I already have this driver:
but it doesn't turn in low values and the motor does one squeaking noise. ( maybe it's my code )
int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int sensorPin = A0;
int sensorValue = 0;
void setup(){
pinMode(ENA,OUTPUT);//output
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
digitalWrite(ENA,LOW);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's directon
Serial.begin(9600);
}
void loop(){
sensorValue = analogRead(sensorPin);
analogWrite(ENA,sensorValue);//start driving motorA
Serial.println(sensorValue);
delay(500);
}
Can someone suggest me a proper driver or identify the problem in my code ?
Code looks right. Presumably you're reading a pot as the "sensor" on A0.
What voltage does the motor need, and what voltage are you providing to the L298? Those things lose a minimum of 2V through the chip, so with low PWM average voltage, it might just simply be that there's not enough voltage to turn the motor.
Actually I just noted a fault in your code. The sensor value can be 0-1023, but the analogWrite can only be 0-255, so you don't output the sensor value in the analogWrite, it must be /4. Not sure what happens if you analogWrite a value over 255.
analogWrite(ENA,sensorValue/4);//start driving motorA
But at the low values, you're probably not giving the motor enough volts to turn. If the motor needs 14V and you have a maximum of 12V that's 2 missing already, then lose 2 in the L298 that's 10. Then cut that by PWM-ing a low average value and maybe the 14V motor only gets 5 or so....
Me?- of course. But it's quite common to divide an analogRead value of 0-1023 by 4 to get an analogWrite of 0-255, rather than using map, it's quite a bit simpler.
Alex_Tr:
The noise starts before the motor start spinning ...
The motor start spinning when the value( in analogWrite) is 50 to 60,
the noise stop when the value is greater than 140.
This is perfectly normal behavior. The motor needs a certain amount of power to overcome internal friction etc before starting to rotate. The noise you are hearing is the pwm switching frequency.
If you want the motor to start earlier you can use the map function with a toLow value of 50 or 60 and use an additional if statement to turn it all off