Need help with incremental encoder

Mod edit:
This is a continuation from Stepper not turning
@ardnewbie03, @marwannnnn are apparently room mates working together on the same project.

Hey guys,,,,ive been trying to work the error in my code using chatgpt and some friends and something still seems to be off. ive hooked up a stepper motor to an H bridge and its been working fine,,,so has the encoder theyre both fine on their own,,,when hooked up togther though the stepper seems to be microsteping back and forth aand vibrating in place.

i need to make a code that when the encoder is turned the stepper turns proportionally....

#include <AccelStepper.h>
#include <Encoder.h>

#define IN1 1  // Stepper driver IN1 pin connected to Arduino pin 1
#define IN2 2  // Stepper driver IN2 pin connected to Arduino pin 2
#define encA 8 // Encoder A pin connected to Arduino pin 8
#define encB 9 // Encoder B pin connected to Arduino pin 9

// Define steps per revolution based on your motor's specification
int stepsPerRevolution = 200;
long encoderPos = 0;
long lastEncoderPos = 0;
long stepperPos = 0;

AccelStepper stepper(AccelStepper::DRIVER, IN1, IN2);  // Stepper motor setup
Encoder encoder(encA, encB);  // Encoder setup

void setup() {
  stepper.setMaxSpeed(1000);     // Set max speed of stepper motor
  stepper.setAcceleration(500);  // Set acceleration
  Serial.begin(9600);            // Start Serial Monitor for debugging
}

void loop() {
  encoderPos = encoder.read(); // Get the current position of the encoder

  // If the encoder position changes, move the stepper motor accordingly
  if (encoderPos != lastEncoderPos) {
    long deltaPos = encoderPos - lastEncoderPos; // Calculate how much the encoder moved
    stepperPos += deltaPos;  // Increment the stepper motor's position

    // Move the stepper motor incrementally
    stepper.moveTo(stepperPos);

    // Move the stepper motor towards the target position
    stepper.run();
  
    lastEncoderPos = encoderPos;  // Update the last encoder position
  }

  // Debugging: Print encoder position and stepper position to serial monitor
  Serial.print("Encoder Pos: ");
  Serial.print(encoderPos);
  Serial.print(" | Stepper Pos: ");
  Serial.println(stepperPos);
}

here is the chat gpt code

NOTE: the stepper is a nema 17 and the encoder is e38s6g5 600b
using a 12v 30amp power supply,,,,,,,,,,,,,thanks in adavnce guys,

I think it may have the same problem we have, we cannot see what you have. Please post an annotated schematic that clearly shows how everything is wired, including all power and ground connections, power sources, and any external hardware such as resistors. Additionally, include a clear picture of your setup and provide links to the technical documentation for each hardware device.

Avoid linking to marketplaces like Amazon, as they typically don’t provide the necessary technical details.

Looking at your code declarations, assuming you are using a UNO, it is apparent it will not work.

ill try my best to create a schematic...in the meantime why would an uno not work?

Not the way you have it connected per your sketch. If properly connected it will work just fine. I think the problem will become apparent when you draw the schematic.

Pin 1 of UNO is serial output (Tx), you can't use it for motor control

Which motor (part number)?
Which motor driver?
How is the driver connected to the Arduino and to the motor?
Which power supply?

Which encoder?
How many counts does the encoder output for 1 shaft revolution?

17HS8401, 200 steps/revolution, 1.8° step angle
L298N H-Bridge--driver
E38S6G5-600B-G24N --- encoder
600 counts per revolution

as for the schematic im still a noobie in the arduino world and im learning kicad as we speak....any help until i can provide a schematic will be helpful

hm,,it still moved the stepper when i was testing it on its own,,will that not be the case when tested with an encoder?

The encoder has NPN open collector outputs. How do you have it wired?

5v to arduino
black wire to ground
Green Wire (Pin A): Connected to Arduino Pin 8.
White Wire (Pin B): Connected to Arduino Pin 9.

Then pins 8 & 9 should be set to:

pinMode(encA,INPUT_PULLUP);
pinMode(encB,INPUT_PULLUP);

In setup().
Also, when the motor turns 1 step, the encoder will count 3 or 6 increments. If you turn the encoder 1 increment, the motor cannot turn 1/3 of a step. You could divide the encoder count by 3 (or 6).

Im still getting no movment...just slight noise from the stepper.

Post your latest code and a wiring diagram.

That driver is not suitable for your motor, you need a current controlling driver like DRV8825.

http://handsontec.com/dataspecs/17HS8401B.pdf

https://www.pololu.com/category/120/stepper-motor-drivers

#include <AccelStepper.h>
#include <Encoder.h>

#define IN1 1  // Stepper driver IN1 pin connected to Arduino pin 1
#define IN2 2  // Stepper driver IN2 pin connected to Arduino pin 2
#define encA 8 // Encoder A pin connected to Arduino pin 8
#define encB 9 // Encoder B pin connected to Arduino pin 9

// Define steps per revolution based on your motor's specification
int stepsPerRevolution = 200;
long encoderPos = 0;
long lastEncoderPos = 0;

long stepperPos = 0;

AccelStepper stepper(AccelStepper::DRIVER, IN1, IN2);  // Stepper motor setup
Encoder encoder(encA, encB);  // Encoder setup

void setup() {
  stepper.setMaxSpeed(1000);     // Set max speed of stepper motor
  stepper.setAcceleration(500);  // Set acceleration
  Serial.begin(9600);            // Start Serial Monitor for debugging

  // Set encoder pins to INPUT_PULLUP
  pinMode(encA, INPUT_PULLUP);
  pinMode(encB, INPUT_PULLUP);
}

void loop() {
  encoderPos = encoder.read(); // Get the current position of the encoder

  // If the encoder position changes, move the stepper motor accordingly
  if (encoderPos != lastEncoderPos) {
    long deltaPos = encoderPos - lastEncoderPos; // Calculate how much the encoder moved

    // Adjust by dividing the encoder count by 3 (or 6 depending on your encoder's resolution)
    deltaPos /= 3;  // Use 6 if your encoder has higher resolution

    stepperPos += deltaPos;  // Increment the stepper motor's position

    // Move the stepper motor incrementally
    stepper.moveTo(stepperPos);

    // Move the stepper motor towards the target position
    stepper.run();
  
    lastEncoderPos = encoderPos;  // Update the last encoder position
  }

  // Debugging: Print encoder position and stepper position to serial monitor
  Serial.print("Encoder Pos: ");
  Serial.print(encoderPos);
  Serial.print(" | Stepper Pos: ");
  Serial.println(stepperPos);
}

are you sure?
i used a potentiometer to turn the stepper this morning and it was working fine
,,,,sorry for the bad drawing i cant upload from the actual software because im still learinging it

Well, if it works, it works. Good luck.

1 Like


best i could do...

Good start on the schematic! When you have some time, I recommend downloading KiCad and experimenting with it. KiCad is a powerful, free schematic capture and PCB design program. While a donation is optional, it’s appreciated to support its development.

Here’s a link to a useful guide you should read: Arduino Uno Pins: A Complete Practical Guide. Pay special attention to the Tx and Rx pins, as they are used for serial communication with your monitor.

i want to build an inverted pendlum (i made a post like a day or two ago)..i wired everything like it should but whenever i upload the code something always seems to be wrong.. i hvae a rotary encoder and i need it to spin to keep a pendlum standing upwards using either a stepper motor nema 17 or a dc geared JGA25-370...the most prominnet problem with both motor is that it jitters alot and when i spin the shaft cw adn then ccw the shaft doesnt move to the ccw it just keeps spinning,

if you'll write the code the pins hardly matter to me right now just assign them to whatever (ill be using an arduino uno)... thanks in advance