PLS HELP : error in the project of Control speed of stepper motor with buttons

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
    
  }


I'm sorry, but I'm unclear on what problem you're having with the 1.x IDE (that being the category you posted in). Could you please expand upon that?

Do not use the Arduino TX pin 1.

EDIT: That code looks fresh outta chatGPT.

If INPUT_PULLUP then NOT pressed is HIGH, button pressed will be LOW.

  • Pin 11 is used in both . . .
  • BTW in real life, do not use Arduino to power motors.

Can the BYJ-48 do 90 RPM?

Maybe you should be using a DC motor.

Your sketch (also) confused speed with RPM. RPM you can set and run. Speed you must measure.

Here is a sketch with DIFFERENT WIRING but in the spirit of your sketch that translates your "speed" button into an "RPM" and "SPEED" (not a real speed or RPM, those are up to you to find).

#include <LiquidCrystal.h>
#include <Stepper.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

#define STEPS_PER_REV 200
Stepper stepperMotor(STEPS_PER_REV, 8, 9, 10, 11);

const int button1Pin = 14; // A0
const int button2Pin = 15; // A1
const int button3Pin = 16; // A2

const int speed30 = 1;
const int speed60 = 2;
const int speed90 = 3;

int speed;

void setup() {
  Serial.begin(115200);
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);
  pinMode(button3Pin, INPUT_PULLUP);

  lcd.begin(16, 2);
  lcd.setCursor(3, 0);
  lcd.print("Motor Speed");
  lcd.setCursor(1, 1);
  lcd.print("Speed   RPM");
}

void loop() {
  if (digitalRead(button1Pin) == LOW) {
    setMotorSpeed(speed30);
  }
  if (digitalRead(button2Pin) == LOW) {
    setMotorSpeed(speed60);
  }
  if (digitalRead(button3Pin) == LOW) {
    setMotorSpeed(speed90);
  }
}

void setMotorSpeed(int speed) {
  updateSpeedDisplay(speed);
  stepperMotor.setSpeed(speed * 100);
  stepperMotor.step(STEPS_PER_REV);
}

void updateSpeedDisplay(int speed) {
  lcd.setCursor(7, 1);
  lcd.print(speed);
  lcd.setCursor(13, 1);
  lcd.print(speed * 30);
}
diagram.json for wokwi.com
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
    { "type": "wokwi-lcd1602", "id": "lcd1", "top": -178.97, "left": 6.4, "attrs": {} },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -44.6,
      "left": 230.6,
      "rotate": 180,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn2",
      "top": 3.4,
      "left": 230.6,
      "rotate": 180,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn3",
      "top": 51.4,
      "left": 230.6,
      "rotate": 180,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-stepper-motor",
      "id": "stepper1",
      "top": -72.72,
      "left": -69.31,
      "rotate": 270,
      "attrs": { "size": "8" }
    }
  ],
  "connections": [
    [ "btn3:1.r", "btn2:1.r", "black", [ "h-9.8", "v-48" ] ],
    [ "btn2:1.r", "btn1:1.r", "black", [ "h-9.8", "v-48" ] ],
    [ "nano:GND.3", "lcd1:K", "black", [ "v0", "h19.2" ] ],
    [ "nano:5V.2", "lcd1:A", "red", [ "v0", "h38.4" ] ],
    [ "nano:A0", "btn3:2.r", "green", [ "v28.8", "h163.2", "v-19" ] ],
    [ "nano:A1", "btn2:2.r", "green", [ "v19.2", "h144", "v-38.2" ] ],
    [ "nano:A2", "btn1:2.r", "green", [ "v9.6", "h124.8", "v-76.6" ] ],
    [ "nano:8", "stepper1:B-", "green", [ "v0" ] ],
    [ "nano:9", "stepper1:B+", "green", [ "v0" ] ],
    [ "nano:10", "stepper1:A+", "green", [ "v0" ] ],
    [ "nano:11", "stepper1:A-", "green", [ "v0" ] ],
    [ "nano:GND.3", "btn1:1.r", "black", [ "h28.8", "v-9.6" ] ],
    [ "nano:7", "lcd1:RS", "green", [ "v0" ] ],
    [ "nano:2", "lcd1:D7", "green", [ "v-9.6", "h28.8" ] ],
    [ "lcd1:D6", "nano:3", "green", [ "v28.8", "h-47.8" ] ],
    [ "nano:4", "lcd1:D5", "green", [ "v-28.8", "h28.8" ] ],
    [ "lcd1:D4", "nano:5", "green", [ "v9.6", "h-19.2" ] ],
    [ "nano:6", "lcd1:E", "green", [ "v-38.4", "h9.6" ] ]
  ],
  "dependencies": {}
}

Max speed of 28BYJ-48 without acceleration control is about 12 ~14 RPM.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.