Stepper motors driving unexpectedly

I have this code to move my two stepper motors using serial connection from python. That is working great, the problem is however that the motors moves unexpectedly at the first seconds when I upload the code or start the python program. It only moves when it is “uploading” not when the program starts. Might this be due to the fact that the motors shall move depending on the values given in serial, and that the uploading sends serial data?
Thanks in advance - Robert

camera_pan_tilt_V2.ino (2.08 KB)

There are a few things that would help us help you. Post a schematic, not a frizzy drawing on how you have it wired, including all power and ground connections. At this point it appears to be a hardware rather then a software problem.

More members will see your code if posted properly. Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Sorry, I will fix that

Files:

Kamera_pan_tilt.ino (1.82 KB)

Kamera_pan_tilt.ino (1.82 KB)

Hi,

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
In your circuit diagram please label pins and components and include what you power supply really is.

You have posted a Fritzy picture which does not provide all the info needed.

Thanks.. Tom... :slight_smile:

#include <AccelStepper.h>
const int vertikalDirPin = 8;
const int vertikalStepPin = 9;
String vIn = "0";
String OvIn = "0";
int vSpeed = 0;
#define motorInterfaceType 1
AccelStepper vertikal(motorInterfaceType, vertikalStepPin, vertikalDirPin);
const int horisontalDirPin = 10;
const int horisontalStepPin = 11;
String hIn = "0";
String OhIn = "0";
int hSpeed = 0;
#define motorInterfaceType 1
AccelStepper horisontal(motorInterfaceType, horisontalStepPin, horisontalDirPin);
const int maxUp = 1000;
const int maxDown = 1000;
String innkommende;
void setup() {
  vertikal.setMaxSpeed(1000);
  vertikal.setCurrentPosition(0);
  vertikal.setAcceleration(3000);
  horisontal.setMaxSpeed(1000);
  horisontal.setAcceleration(300);
  horisontal.setCurrentPosition(0);
  Serial.begin(115200);
  Serial.setTimeout(15);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
}
void loop() {
  if (Serial.available()) {
    innkommende = Serial.readStringUntil('\n');
    Serial.println(innkommende);
    if (innkommende != "h" || innkommende != "n") {
      int komma = innkommende.indexOf(',');
      String vIn = innkommende.substring(0, komma);
      String hIn = innkommende.substring(komma + 1);
      if (vIn != OvIn) {
        vSpeed = vIn.toInt();
      }
      if (hIn != OhIn) {
        hSpeed = hIn.toInt();
      }
      OhIn = hIn;
      OvIn = vIn;
    }
  }
  if (innkommende == "h") {
    vertikal.setSpeed(100);
    vertikal.moveTo(0);
    vertikal.runToPosition();
    innkommende = "0,0";
  }
  else if (innkommende == "n") {
    vertikal.setCurrentPosition(0);
  }
  else {
    vertikal.setSpeed(vSpeed);
    vertikal.run();
    horisontal.setSpeed(hSpeed);
    horisontal.run();
  }
}

gilshultz:
Post a schematic, not a frizzy drawing on how you have it wired,

What part of “not a frizzy drawing” are you having trouble understanding?

I just thought a "fizzy drawing" made things clearer, sorry.
Here is a schematic.
The components are soldered on to a perfboard, and it is quite difficult to make out the wiring on it.
However, I do not thing it has anything to do with the wiring, since it works perfectly fine. It is just when the code is being uploaded that the motors move randomly for a second.
The power supply is 12v 4a. Capasitor is 470uF

Here is a schematic.

Well that is just your opinion, it is truly horrendous. A schematic is supposed to make things clearer, not more complex. Wires should have the minimum of crossings and be horizontal or vertical never diagonal.

However, I do not thing it has anything to do with the wiring, since it works perfectly fine. It is just when the code is being uploaded that the motors move randomly for a second.

This shows you how wrong you can be.
During power up those outputs that feed into the stepping motor driver input are themselves inputs. This means that there is nothing driving the pins, the technical term for this is floating. A floating input will be prone to interference and it will look like there are pulses in the driver inputs. So you need a pull up, or pull down resistor on the inputs to the stepping motor driver. This will stop the signals floating.

Or put another way Pololu forgot to put a pull-down or pull-up resistor on the STEP input on their module,
so you're going to have to add one.

The DRV8825 chip has a builtin pulldown on STEP pin so it doesn't show this behaviour, another reason its
a better choice.

I have newer drawn a schematic before, just saw that option in fritzing, didn't understand what was wrong with the first fritzing print, so I just attached that file as well.
I personally thought this forum was to help each other, not to criticize them. You want me to post code, draw circuits and schematics.
If you were able to understand that just of my written problem description, why couldn't the answer just been given, without all the other hassle? It's nerve-racking for us starting out trying to make some new projects to ask for help, if the information isn't given in a 100% perfect way then you get so much criticism.
However, I see how the posted code and schematics could be useful, and thanks to everyone trying to help. But maybe read others code and drawings with a smile, trying to understand what that newbie needs help with.

With a pull-down (or up) resistor to STEP, do you mean that I should add like a 10k ohm resistor between that and ground (or 5v) on the arduino?

robertbm04:
With a pull-down (or up) resistor to STEP, do you mean that I should add like a 10k ohm resistor between that and ground (or 5v) on the arduino?

Yes, then it won't flap around in the breeze after power up and before the bootloader runs your sketch.
The chip powers up with all pins floating, and this only changes if you program the pinMode() for a pin.

CMOS inputs are high sensitive to nearby voltages (other signals on the PCB) through capacitive
coupling, and to nearby insulators with static charge on them - the reason is an input pin is
isolated from the rest of the chip by gate-oxide which is silica an extremely good insulator
(even in 20nm thick layers).

Once you program INPUT_PULLUP or OUTPUT for a pin its voltage will suddenly become defined again as
the pullup or output driver transistors turn on.

Thank you! Solved the problem :slight_smile:

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