Arduino, steppermotor, uStepper challenges

Hi all,

I am starting with Arduino and I am stuck with a - probably simple - thing.

Situation: I try to control the a stepper motor via a Python Script on the Raspi. The Raspi communicates with a uStepper (Arduino Board) to control the forward and backward spinning of the stepper motor.

Problem: As soon as I connect the stepper motor to the power source, it starts executing the loop and moving.

However, it should only initiate the loop when being triggered by the Python script, via the GPIO. Once I start the python script, the stepper motor stops moving, and the function works well. I guess this has to do with high/low signals, but cant figure it out.

Further, what is the best way to let the Raspi & Arduino board communicate? (trigger the stepper motor to move). Currently I use the PWN pins of the Arduino Board. (https://ustepper.com/store/index.php?controller=attachment&id_attachment=9)

The code which I currently have:

The code which I currently have:

#include <uStepperS.h>
#define MAXACCELERATION 50000 //Max acceleration = 50000 Steps/s^2

int buttonPin1 = 2; // the pin that the pushbutton is attached to
int buttonPin2 = 3; // the pin that the pushbutton is attached to
int buttonPin3 = 4; // the pin that the pushbutton is attached to
int buttonState1 = digitalRead(buttonPin1); // current state of the button
int buttonState3 = digitalRead(buttonPin3); // current state of the button

uStepperS stepper;

void setup()
{
  Serial.begin(9600);
  stepper.setup();
  stepper.setMaxVelocity(5000);

  // initialize the button pin as a input:
  pinMode(buttonPin1, INPUT_PULLUP);
  //pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
}

void loop()
{
  if (buttonState1 = digitalRead(buttonPin1))
  {
    if (buttonState1 == HIGH){
    stepper.moveSteps (-400000);
    delay( 4000);
  }
else {
stepper.stop();}

}
  if (buttonState3 = digitalRead(buttonPin3))
  {
    if (buttonState3 == HIGH){
    stepper.moveSteps (400000);
   delay( 4000);
  }
  else{
  stepper.stop();}}
}

Please take a look at the first topic telling how to get the best from this forum.
Use autoformat in the IDE and code tags, </>, when pasting.
A wiring diagram would also be appreciated.

1 Like

Thanks, updated based on your tips

You forgot the autoformat before pasting. Try it and note the difference!

You're reading pin 2 which has nothing attached to it, but since you activated the internal pull-up it will be high. That triggers the move.

From the if structure reference.

Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

Please format your code.

Hi Wildbill,

Thanks for your advice. I put the "buttonState1 == LOW" and then the movement stops.

The issue I face then is that when the python script runs, and triggers then communication with the Arduino board, nothing happens. (in the python script I put the GPIO to HIGH to trigger the Arduino)

thanks for the advice, let me have a look at that and update

So I tried but the issue is not solved. It seems to be the wiring and/or the communication with the Raspi.

When the Raspi GPIO is set to "LOW" the steppermotor stops. During the execution of the python script on the Raspi everything works well.

Once Raspi is turned off, the arduino get the "HIGH" signal again and starts moving the stepper motor.

Same thing. If the Pi has pulled the pin low, movement stops. When the Pi is off, the pullup brings the pin high and the stepper moves.

Hi Wildbill,

Agree, and I understand that concept now after playing around with it today and based on your feedback. I am just thinking how to get it to stop moving. My thinking now is to change the wiring.

When I put the "buttonState = 0 " it stops moving, but its also not activated when I run the python script

How would I get this in a pull down configuration? That might be the solution, not?

Use an external physical resistor instead of the internal pullup.

thank you, let me try that. recommend a 10K ohm?

I just came up with a "hack" as well. I just start the power to the Arduino only when the python script runs. This way the pin gets the right input from the Raspi. This is controlled with a relais via the python script. Maybe not pretty, but definitely "plan B"

Have you got a common ground?

@MarkT yes the raspi and arduino have a common ground. (via a logic level converter)

@wildbill, @MarkT Ok, my plan B didn't work: it works when activating the script, but once the script is done, the same problem arises as before...

On to a physical resistor...

Can you recommend where to place it in the circuit and what amount of ohm? Please find my updated schematics below

Ah - the level shifter is news to us! That's likely your problem, you can't use this level shifter with one end powered down. Well that's my guess.

If you want to independently power down the two ends, use opto-isolators to communicate as they have no power supply requirements.

@MarkT I didn't know the levelshifter made a difference in the setup, thats why i didn't draw it in the beginning.

Even when I have both ends powered up, the stepper motor is moving. Only when I start the python script on the raspberry, (and change the GPIO to LOW), the motor stops. Based on that I wonder if the two power sources make a difference.

It's still the same issue, you need a pulldown instead of the internal pullup. Try 10K, IIRC, that's what the Arduino uses for the pullup.