Stepper motor control

Hey all. I am brand new to Arduino and can't seem to figure out how to get my Uno R3 to control a Nema 23 stepper motor with a DM542T controller. I have checked and rechecked my wiring and believe it to be correct so I am left with thinking my sketch is the problem. I am trying to use two momentary switches and a potentiometer to control the direction and speed of the motor. Any advice would be greatly appreciated.

#include <AccelStepper.h>

// Define the stepper motor connections
#define STEP_PIN 2
#define DIR_PIN 5

// Define the switch pins
#define CLOCKWISE_SWITCH 7
#define COUNTER_CLOCKWISE_SWITCH 8

// Define the potentiometer pin
#define POTENTIOMETER_PIN A0

// Create an AccelStepper object
AccelStepper stepper(1, STEP_PIN, DIR_PIN);

// Variables to store switch state
int clockwiseSwitchState = 0;
int counterClockwiseSwitchState = 0;

void setup() {
  // Set the switch pins as INPUT_PULLUP
  pinMode(CLOCKWISE_SWITCH, INPUT_PULLUP);
  pinMode(COUNTER_CLOCKWISE_SWITCH, INPUT_PULLUP);

  // Set the potentiometer pin as INPUT
  pinMode(POTENTIOMETER_PIN, INPUT);

  // Set stepper motor speed and acceleration
  stepper.setMaxSpeed(1000.0);
  stepper.setAcceleration(500.0);
}

void loop() {
  // Read switch states
  clockwiseSwitchState = digitalRead(CLOCKWISE_SWITCH);
  counterClockwiseSwitchState = digitalRead(COUNTER_CLOCKWISE_SWITCH);

  // If clockwise switch is pressed, move the motor clockwise
  if (clockwiseSwitchState == LOW) {
    stepper.setSpeed(AnalogReadMapped(POTENTIOMETER_PIN, 0, 1000));
    stepper.move(100); // Adjust the steps per revolution as needed
    stepper.runSpeed();
  }

  // If counter-clockwise switch is pressed, move the motor counter-clockwise
  else if (counterClockwiseSwitchState == LOW) {
    stepper.setSpeed(-AnalogReadMapped(POTENTIOMETER_PIN, 0, 1000));
    stepper.move(-100); // Adjust the steps per revolution as needed
    stepper.runSpeed();
  }

  // If no switch is pressed, stop the motor
  else {
    stepper.setCurrentPosition(0);
  }
}

// Function to map analog input to a specific range
int AnalogReadMapped(int pin, int fromLow, int fromHigh, int toLow, int toHigh) {
  return map(analogRead(pin), fromLow, fromHigh, toLow, toHigh);
}

// Overloaded function with default mapping range (0-1023 to 0-255)
int AnalogReadMapped(int pin, int toLow, int toHigh) {
  return AnalogReadMapped(pin, 0, 1023, toLow, toHigh);
}

Welcome to the forum.

A problem is fixed by taking a step back (or many steps back) until you have something that works, then you can go forward.

Reduce the complexity of the sketch to the bare minimum.
Have you seen this example: https://github.com/waspinator/AccelStepper/blob/master/examples/ConstantSpeed/ConstantSpeed.pde

Background: The AccelStepper generates the signals for the stepper motor in the loop(). It needs to run "stepper.runSpeed()" as often as possible, preferably hundreds or thousands times per second. Each time the "stepper.runSpeed()" runs, the library decides if a new step should be taken.

I had no hand in creating the code I posted. I used chatGPT to generate it and have a very basic (and I mean basic) understanding of how sketches work. I’m more so just trying to see if anyone who is experienced with sketches can see anything obviously wrong with the sketch chatGPT created. Or if anyone else has done the same thing I’m trying to do and would be kind enough to share their sketch with me. That’s about my limit until I become more familiar with how all this stuff works.

it generate garbage and you must be experienced enough to repair all errors. but if you experienced you write your sketch self. negative profit!

so, what is your problem? compiling error?

1 Like

The sketch does not trigger any errors in the IDE. I have used chatGPT to generate sketches before and they have worked fine.

well, good luck.

Have you tried asking ChatGPT what is wrong with the sketch?

AccelStepper stepper(1, STEP_PIN, DIR_PIN);

I think the first parameter should be steps per revolution.

    stepper.setSpeed(-AnalogReadMapped(POTENTIOMETER_PIN, 0, 1000));
    stepper.move(-100); // Adjust the steps per revolution as needed

I think setSpeed () should be a positive number.
1000 may be too high for the speed.
The comment is wrong, it does not apply to move(), I think it should apply to the stepper() constructor.

I would look at https://docs.arduino.cc/learn/electronics/stepper-motors#stepperspeedcontrol and try those examples first.

I’ll check it out. Thank you for the info.

Hi, @patriotj1776
Welcome to the forum.

Can I suggest you give Google a try.

Google;

dm542t arduino

This may help with the basics.
https://vslot-poland.com/how-to-connet-arduino-to-nema
It uses simple code and unlike chatGPT, EXPLAINS each of the statement.

Tom... :grinning: :+1: :coffee: :australia:

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