Need stepper mtr. to move according to voltage read

Hi,
I'm using a stepper motor (Nema 17), connected to a needle valve to dispense liquid.
The flow of liquid depends a dc signal from an Air Flow sensor.
The sensor puts out .6 to 1.4 volts.
At .6v the needle valve should be closed (turned all the way ccw).
At 1.4v the needle valve needs to be open 21 turns (CW).
I set up a breadboard circuit, and got the motor moving correctly on a simple test code to check the basic wiring.
Then I used Arduino Code Generator to get a code for the task above, and loaded on an Arduino Nano.
The motor only turns clockwise continuously.
I'm testing on a normal stepper, but the on the equipment it will be closed loop.
But I can't even get the test motor to follow the voltage like I need it to.
I tried to get a code that would establish a home step, and the count and keep track during operation.
On the real equipment, I will use a limit switch at the nearly closed (CCW) position.

Here's the code I used:

#include <Stepper.h> 

const int stepsPerRevolution = 200; 
int mafSensorPin = A3; 
int dirPin = 8; 
int stepPin = 9; 
int homeStep = 0; 
int mafSensorVoltage = 0; 

Stepper myStepper(stepsPerRevolution, dirPin, stepPin); 

void setup() { 
  myStepper.setSpeed(60); 
  pinMode(mafSensorPin, INPUT); 
} 

void loop() { 
  mafSensorVoltage = analogRead(mafSensorPin); 
  if (mafSensorVoltage <= 600) { 
    myStepper.step(homeStep); 
  } 
  else { 
    int stepNumber = (mafSensorVoltage - 600) * 6000; 
    myStepper.step(stepNumber); 
  } 
} 

Any help would be appreciated.

Welcome to the forum

This seems like it is yet another "the code that AI wrote for me does not work" question. As such you may not get much help here but good luck with your project

Thanks for the welcome.
I didn't know they were so often a problem. I guess it makes sense.
I'm willing to erase it all. Maybe keep pin #s and such...and go from there.
It's a little different. I'm hoping somebody knows something about step counting ,(if that's what it's called)

I smell a business opportunity...

Your favorite search engine and the search phrase "Arduino stepper controlled by pot" will turn up tutorials and code written by humans.

No guarantees of quality, though, especially with Instructables.

True. I've looked at a lot. One problem is most of them are for speed, not angle.
Maybe I'll start with simple angle then build on that.

The stepper library examples are an excellent resource, and they show you how to move to a position.

The very first example is to match the movement of knob.

1 Like

I don't think the stepper.h library is a suitable solution for this task. Use a stepper library that takes care of the positioning. With my MobaTools it's pretty easy:

#include <MobaTools.h>

const int stepsPerRevolution = 800;   // 1/4 microstepping for smoother stepper movement
const byte mafSensorPin = A3;
const byte dirPin = 8;
const byte stepPin = 9;
int homeStep = 0;
int mafSensorVoltage = 0;
long stepAngle;

MoToStepper myStepper(stepsPerRevolution, STEPDIR);

void setup() {
  myStepper.attach(stepPin, dirPin );
  myStepper.setSpeed(600);          // 60 RPM - change to your needs
  myStepper.setRampLen( 20 );       // 20 steps from standstill to full speed
  pinMode(mafSensorPin, INPUT);
}

void loop() {
  mafSensorVoltage = analogRead(mafSensorPin);
  mafSensorVoltage = constrain( mafSensorVoltage, 122, 286 ); // limit to .6V...1.4V range
  stepAngle = map ( mafSensorVoltage, 122, 286, 0, 7560 ); // Sensorvoltage to angle posiiton
                                                          // 0...21 rev
  myStepper.write(stepAngle);
  delay(100); // leave some time for the stepper to move.
}

I leave homing and smoothing af analogReading ( if needed) to you :wink:

[Edit] I adapted the mapping to your needs ( 0.6V ... 1.4V -> 0...21 turns ). But then the resolution of analogRead on UNO is fairly coarse. Maybe you need an other A/D-converter or at least use an other ref voltage.

This is immensely helpful. I owe you one.
I'm switching to a linear stepper with a jackscrew, to pinch a tube, (instead of needle valve). (Mechanically simpler, but still accurate. )
The code and library will still be applicable.
I'm getting the linear/motor on the 4th.
Thanks again! I'll report back!

Hi, I received the linear step motor.
I loaded the program from MicroBahner and I'm getting no output to the 4 stepper motor wires.
I've tried all three a4988 I have. One was brand new and had voltage .5v across the adjustment screw.
There is some sort of activity at the dir and step pins coming in from the Nano.
All other pins seem OK according to my VOM.
I don’t know what to do next.
Here's a picture of what I have.
(ANY help would be appreciated)


I don't see any connection to the Vmot of the driver. So your stepper is not powered.
Please provide a link to the tecnical specs of your linear stepper.

Of, course, why didn't I think of that!
Gonna wait before I try.
I would like to jump 5v from the Arduino, but not sure if it's a good idea.
It will be used a lot in real application, but the travel range will be about 1/4".
It's going on a car, so the Arduino 5v would be handy. (Or another line from USB.)
THANKS!
Specs page;
https://www.amazon.com/gp/aw/d/B08JCHXXFB?psc=1&ref=ppx_pop_mob_b_asin_image

Hi, @mojave_dean

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

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

You cannot power the stepper from the arduino 5V - the arduino is no power source.
And the the Vmot of A4988 needs at least 8V to work properly - and it must be at least 2x higher than the rated voltage of your stepper - and that is 5-9V. So you need more than 12V at Vmot - als depending on the curent you want to drive your stepper with ( 200mA...600mA).

I think the A4988 isn't really a good choice for this stepper. The A4988 works best with low impedance steppers. The coil resistance of your stepper is 21 Ohm - that's not really low impedance...but you can try if the Vmot voltage is high enough.

Interesting.
If I run a max of 200mA, do you think I would I be OK running 4 wires off the Arduino, instead of dir and step?
In other words, eliminate the a4988.
BTW, while you were posting, I actually did try the Arduino 5V, and got no motor response, (just like you predicted).

In any case you need a driver. You cannot connect the stepper directly to the Arduino. It would be possible to use a MOSFET H-bridge like the DRV8833. You can power this H-bridge with 5V - if the torque of the stepper is sufficent in that case.

Ahh,I see.
That makes the most sense to me right now.
Thank you so much.
I'm going to work on setting that up.

OK I edited my whole post, due to recent findings.
I got the new driver (drv8833). Looks like I need a+a-b+b- coming out of the Arduino.
That should be no problem, I going to try to modify MicroBahn's sketch.
Some people say "run the stepper motor to a stop" to count steps from there. Thats what I want to do.
So I'll try to test that soon, and see if it works as anticipated.
Just diving into this again, so any insights would help.
Thanks again to MicroBahner, for helping me get to this point.
This is going to meter fuel on fuel vapor system for a car. Very exciting experiment, imo.


I need help. I dont know how to convert MicroBahner's previous code to 4 pin a+a-b+b-.

Maybe this would work;

#include <MobaTools.h>

const int stepsPerRevolution = 800;   // 1/4 microstepping for smoother stepper movement
const byte mafSensorPin = A3;
int a_plus = 2;
int a_minus = 3;
int b_plus = 4;
int b_minus = 5;
int homeStep = 0;
int mafSensorVoltage = 0;
long stepAngle;

MoToStepper myStepper(stepsPerRevolution, STEPDIR);

void setup() {
  //set pins as outputs
  pinMode(a_plus, OUTPUT);
  pinMode(a_minus, OUTPUT);
  pinMode(b_plus, OUTPUT);
  pinMode(b_minus, OUTPUT);
  myStepper.setSpeed(600);          // 60 RPM - change to your needs
  myStepper.setRampLen( 20 );       // 20 steps from standstill to full speed
  pinMode(mafSensorPin, INPUT);
}

void loop() {
  mafSensorVoltage = analogRead(mafSensorPin);
  mafSensorVoltage = constrain( mafSensorVoltage, 122, 286 ); // limit to .6V...1.4V range
  stepAngle = map ( mafSensorVoltage, 122, 286, 0, 7560 ); // Sensorvoltage to angle posiiton
                                                          // 0...21 rev
  myStepper.write(stepAngle);
  delay(100); // leave some time for the stepper to move.
}