Hello,
Im using a L298n to drive a gearbox motor. I can control the speed of the motor using the ENA OUTPUT. when i set it to 255 (highest speed) the motor sounds "normal" but when i try to lower the speed. lets just say to 150 the motor starts making a high pitch sound. Is it normal for a motor to make that sound or is it a indication there is something wrong?
Thanks for your help.
const int buttonPin = 2;
const int limit1Pin = 5;
const int limit2Pin = 4;
const int ENA = 9;
const int IN1 = 7;
const int IN2 = 6;
int buttonState = 0;
int buttonPushCounter = 0;
int lastButtonState = 0;
int limit1State = 0;
int limit2State = 0;
void setup()
{
// put your setup code here, to run once:
pinMode (buttonPin, INPUT);
pinMode (limit1Pin, INPUT);
pinMode (limit2Pin, INPUT);
pinMode (ENA, OUTPUT);
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);
Serial.begin(9600);
}
void runClockwise()
{
Serial.println("Clockwise");
analogWrite (ENA, 255);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
void runCounterclockwise()
{
Serial.println("Counterclockwise");
analogWrite (ENA, 255);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
void stopMotor()
{
Serial.println("Stop");
analogWrite (ENA, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
void loop()
{
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
limit1State = digitalRead(limit1Pin);
limit2State = digitalRead(limit2Pin);
if (buttonState != lastButtonState)
{
lastButtonState = buttonState;
if (buttonState == HIGH)
{
runClockwise();
}
}
if (limit1State == HIGH)
{
// Limit1 Switch Pressed
runCounterclockwise();
}
if (limit2State == HIGH)
{
// Limit2 Switch Pressed
runClockwise();
delay (1500);
stopMotor();
}
}