Reading senor vs just entering a number

I’ve recently started using stepper motors with A4988 drivers and I ran into a strange issue that I can’t figure out myself.

I can control the speed that the stepper turns easily enough if I just enter a number in the appropriate spot, but when I try controlling the speed with a potentiometer, it doesn’t even come close to working. Instead of speeding up and slowing down smoothly as I turn the knob, it will just change speeds however it feels like it seems.

I can’t find any errors in the code, which may be due to inexperience, but if anyone can help, please do. I tried to use enough comments to make it fairly clear what the intent is.

Powder_Dispenser.ino (2.42 KB)

What values are showing on the serial monitor when you turn the potentiometer?

Paul

Image from Reply #NN Original Post so we don’t have to download it. See this Simple Image Guide

Stepper Control_bb.jpg

…R

I see no pull down resistor on the switch (button) input. Are you really applying motor supply voltage to the pot?

Fritzing diagrams are too easy to misunderstand. A photo of a simple pencil drawing with all the connections clearly labeled will be much more useful.

OP's program

int buttonStart = 8;             //this starts the whole process
int stepPin = 4;                  //this pin pulse to make the stepper step
int dirPin = 2;                  // this pin sets step direction.
String off ;                //this is for shutting off the motor
int pot = A0;                    // this is the pin that the pot is connected to
unsigned int  rotSpeed;         //stores the rotation speed info. is unsigned int because of possibly very high numbers

void setup() {
  Serial.begin(9600);                 //activate serial
  Serial.println("Ready");            //gives me visual confirmation that the arduino is booted up and readt to roll
  pinMode (buttonStart, INPUT);       // next for set modes for pins
  pinMode (stepPin, OUTPUT);
  pinMode (dirPin, OUTPUT);
  pinMode (pot, INPUT);
}

void loop() {


  while (digitalRead(buttonStart) == LOW) { }  //makes the arduino wait for the button to be pushed to start things up
  delay(10);                                    //not sure why I put this in.
  Serial.println("pressed");                   //this is purely here for dubugging purposes
  high();                                      //this calls the function that runs the motor. I separate things out in functions because
                                               //I learned programming on ABB welding robots, and that's the way things were done.

}






void high() {
  Serial.println("Should be turning");            // just to give me visual notification that something is happening.

  while (Serial.available()  == 0) {               //gives me a way to shut of the motor

    int val = analogRead(A0);                        //read pot and place value in int
    Serial.println(val);                              //print the value so that I know what it is
    rotSpeed = map(val, 0, 1023, 100, 45000);            //map the value from val to the valuse that I want for the stepper speeds
    //
    Serial.println(rotSpeed);                           //prints the value for debugging purposes
    digitalWrite(dirPin, HIGH);                         //sets the rotation direction

    digitalWrite(stepPin, HIGH);                        //next three lines pulse at a given speed to make the stepper turn
    delayMicroseconds(rotSpeed);                        // 
    digitalWrite(stepPin, LOW);                         //

  }
  Serial.println("Motor should have stopped turning");         //print for debugging purposes

}

Try making the motor run very much more slowly for your initial tests - perhaps start with a low speed of 5 steps per second.

Also have a look at the second example in this Simple Stepper Code. I think the whole business will be a lot easier to follow if you use your potentiometer input to change the variable millisBetweenSteps. There is no need to change the step pulse width.

...R

Lets be sure we are talking about the pot on the bread board and not the pot on the stepper control board.

Paul

Paul_KD7HB:
What values are showing on the serial monitor when you turn the potentiometer?

Paul

I am seeing the values that I think I should be seeing. From 100 to 50000.

Paul_KD7HB:
What values are showing on the serial monitor when you turn the potentiometer?

Paul

I am seeing the values that I think I should be seeing. From 100 to 50000.

You have a 16 bit ADC? Which board are you using? (I can’t see your code)

Robin2:
Image from Reply #NN Original Post so we don’t have to download it. See this Simple Image Guide

Stepper Control_bb.jpg

…R

nope. thats a mistake. the leads from the pot should be going to the other rail.

17asleep:
I am seeing the values that I think I should be seeing. From 100 to 50000.

The documentation has this warning:

Currently, the largest value that will produce an accurate delay is 16383.

Paul

Paul_KD7HB:
Lets be sure we are talking about the pot on the bread board and not the pot on the stepper control board.

Paul

The pot on the bread board is the one I am adjusting.

GrooveFlotilla:
You have a 16 bit ADC? Which board are you using? (I can't see your code)
I'm not certain I understand the question. Arduino Uno?

Paul_KD7HB:
The documentation has this warning:

Currently, the largest value that will produce an accurate delay is 16383.

Paul

Why is that?

Robin2:
Try making the motor run very much more slowly for your initial tests - perhaps start with a low speed of 5 steps per second.

...R

I can make the stepper run exactly like I want it to by manually changing the value in rotSpeed instead of taking the value generated by the pot. What I can't figure out is why the speed is erratic with the pot.

Why? Because the parameter is an integer and that is the largest positive value it can have. Perhaps you are supplying a negative number to the function.

Paul

17asleep:
The pot on the bread board is the one I am adjusting.

A couple of things spring to mind:-

That pot and the switch should be supplied from the 5V logic supply from the Arduino not the 8-35V motor supply rail.

Currently the pot is providing a voltage between 0 and the motor supply. Which could well be significantly higher than the 0-5V the arduino expects.

Change the switch wiring to one side to arduino pin 8 and the other pin to GND then change the void setup() code to read:-

 pinMode (buttonStart, INPUT_PULLUP);

This way pin 8 will definitely go low when the switch is pushed.

Hope this helps.

Ian

Why? Because the parameter is an integer and that is the largest positive value it can have.

What?

IanCrowe:
A couple of things spring to mind:-

That pot and the switch should be supplied from the 5V logic supply from the Arduino not the 8-35V motor supply rail.

Currently the pot is providing a voltage between 0 and the motor supply. Which could well be significantly higher than the 0-5V the arduino expects.

Change the switch wiring to one side to arduino pin 8 and the other pin to GND then change the void setup() code to read:-

 pinMode (buttonStart, INPUT_PULLUP);

This way pin 8 will definitely go low when the switch is pushed.

Hope this helps.

Ian

You will also have to change your switch detect sense code to read

          while (digitalRead(buttonStart) == HIGH) { }

Ian

val is an int, largest positive value is 32767, make it unsigned int then it will hold 65535.

unsigned int val;

I’m replying from mobile here so I’m having a tough time quoting, but for the people talking about the push button, that’s not an issue. It works exactly as it should. My problem is with rotation speed only.

17asleep:
I’m replying from mobile here so I’m having a tough time quoting, but for the people talking about the push button, that’s not an issue. It works exactly as it should. My problem is with rotation speed only.

True, but with the switch you could well be applying voltages to Atmega chip outside it’s acceptable range:-

From the Atmega 328P datasheet

Table 32.1 Absolute Maximum ratings

Voltage on any pin except Reset -0.5V to VCC+0.5V

Note: Stresses beyond those listed under “Absolute Maximum Ratings” may cause permanent damage
to the device. This is a stress rating only and functional operation of the device at these or other
conditions beyond those indicated in the operational sections of this specification is not implied. Exposure to absolute maximum rating conditions for extended periods may affect device reliability.

Hence the suggestion to rewire the pot and switch to the +5V from the Arduino.

Ian

I have previously stated that the schematic is wrong. I’m not actually taking power from the 12 volt rail to power the pot. I simply drew the lines in wrong.