I have a L298N Dual H Bridge DC Stepper Motor Board and an unknown motor but I’m having troubles getting good speed control. I have an optical switch accurately tracking the RPM. Controlling the speed (originally using analogWrite), putting the Enable pin on 127 from 255 doesn't change the RPM at all, and only putting the enable pin at 8 causes any change, decreasing the RPM from 140 to 137. I tried using the LEDC system to get a higher resolution, but I am still having the same problems. Does anyone know what's going on? I need an accurate control of the RPM from 40 to 140. Here's my code:
int limitswitch1 = 27;
int enable1Pin = 26;
const int freq = 16000;
const int pwmChannel = 0;
const int resolution = 12;
long revolutions = 0;
long lastTime = millis();
bool lastState = false;
void setup() {
Serial.begin(9600); // initialize serial
// put your setup code here, to run once:
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(18, OUTPUT);
pinMode(limitswitch1, INPUT_PULLUP);
digitalWrite(limitswitch1, HIGH);
analogWrite(18, 1023); //ENA pin
pinMode(33, OUTPUT);
pinMode(25, OUTPUT);
pinMode(enable1Pin, OUTPUT);
ledcAttachChannel(enable1Pin, freq, resolution, pwmChannel);
ledcWrite(enable1Pin, 1023);
digitalWrite(33, LOW);
digitalWrite(25, HIGH);
}
void loop() {
if (Serial.available() > 0) {
int receivedValue = Serial.parseInt(); // Read an integer
if(receivedValue > 0) {
ledcWrite(enable1Pin, receivedValue);
}
}
// put your main code here, to run repeatedly:
if(digitalRead(limitswitch1) == HIGH) {
if(lastState) {
revolutions++;
if(revolutions % 2 == 0) {
Serial.println(1000.0 / (millis() - lastTime) * 60);
lastTime = millis();
}
}
lastState = false;
} else {
lastState = true;
}
delay(1);
}
I get it's probably not the greatest, so tips are recommended (just try not to be condescending, I'm new to this). Regardless, help is appreciated!
I'm stepping up the 3.3V to 5V to power the optical sensor as well as the logic on the L289N, and I'm using an external power supply to give the extra power for the motor, ~14.5 volts