Hi I'm using an Arduino UNO try to do a speed controller 28byj-48 5V connected with motor driver.The speed is control by three buttons with 3 different speed which is 30,60 and 90 rpm respectively. The rpm value will shown in LCD when the button pressed. However, the stepper motor doesn't work when I pressed the button. I do the simulation in Tinkercad, can someone help me solve the problem?TT
#include <LiquidCrystal.h>
#include <Stepper.h>
//Stepper Motor Setup
#define STEPS_PER_REV 2048 //Adjust for your stepper motor
Stepper stepperMotor(STEPS_PER_REV,8,9,10,11);
// Pin configuration
const int button1Pin = 13; // Button 1
const int button2Pin = 12; // Button 2
const int button3Pin = 11; // Button 3
// LCD pin configuration (RS, Enable, D4, D5, D6, D7)
LiquidCrystal lcd(6, 5, 4, 3, 2, 1);
// Motor speeds in RPM
const int speed0 = 0;
const int speed30 = 30; // New lower speed
const int speed60 = 60;
const int speed90 = 90;
// PWM values corresponding to speeds (adjust based on motor specs)
const int pwm0 = 0;
const int pwm30 = 70; // Minimum PWM to start the motor
const int pwm60 = 140; // Adjusted PWM for 60 RPM
const int pwm90 = 200; // Adjusted PWM for 90 RPM
int currentSpeed = 0; // Variable to store current speed
void setup() {
// Configure buttons as input with pullup resistors
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
// Initialize stepper motor speed
stepperMotor.setSpeed(0);
// Initialize the LCD
lcd.begin(16, 2);
lcd.setCursor(1, 0);
lcd.print("Motor Speed:");
// Display initial speed
updateSpeedDisplay();
}
void loop() {
// Check if button 1 is pressed
if (digitalRead(button1Pin) == HIGH) {
setMotorSpeed(speed30, pwm30);
delay(10000); // Add delay of 5 seconds
} else {
setMotorSpeed(speed0, pwm0);
}
// Check if button 2 is pressed
if (digitalRead(button2Pin) == HIGH) {
setMotorSpeed(speed60, pwm60);
delay(10000); // Add delay of 5 seconds
}
// Check if button 3 is pressed
if (digitalRead(button3Pin) == HIGH) {
setMotorSpeed(speed90, pwm90);
delay(10000); // Add delay of 5 seconds
}
}
// Function to set motor speed and update display
void setMotorSpeed(int speed, int pwmValue) {
if (currentSpeed != speed) {
currentSpeed = speed;
stepperMotor.setSpeed(currentSpeed); // Set stepper motor speed
updateSpeedDisplay();
}
}
// Function to update the speed on the LCD
void updateSpeedDisplay() {
lcd.setCursor(1, 0);
lcd.print("Speed: ");
lcd.print(currentSpeed);
lcd.print(" RPM "); // Ensure to clear extra characters
}
