Stepper Motor and Pressure Sensor

Hello,

I am new to the forum. Have built a lot of adruino projects but having great difficulties with this project.

looking to have an adruino uno or nano control a stepper motor (Nema 17 12V 2A) which will be connected to a ball valve to release high pressure when it builds up.

In conjunction with the stepper motor I am using a easydriver microchip. if this is the wrong chip I also purchased other stepper driver chips such as A4988 and H-bridge L293D. as of now I would like to get the easydriver chip to work if I can.

The pressure sensor that I am using is a 200 PSI pressure transducer that operates on a 0.5-4.5V analog signal.

also have a 12V 2A power source that is a power transformer.

so far I am able to get the pressure sensor to work fine with attached code "Pressure_Transducer_Code"

also am able to get the Stepper Motor to work with attached code "Stepper_Motor_Code"

Though I can get the sensors and code to work on their own, I am unlucky to get them to work together.

the goal is to have the stepper motor to move 90 degrees in one direction (open) if the pressure is greater then or equal to "P_top_limit" and close by moving 90 degrees in the opposite direction if the pressure is less then or equal to "P_lower_limit"

all help coding this thing would be greatly appreciated.

Thanks in advance
C/T

Stepper_Motor_Code.txt (1.74 KB)

Pressure_Transducer_Code.txt (377 Bytes)

Welcome to the arduino forum!

Could you please elaborate on your difficulties?

In your pressure transducer code, you have a function called measurePressure. If this function returns the value (by adding a "return statement" - https://www.arduino.cc/en/Reference/Return) from the pressure sensor, you could assign a variable to the statement, like this:

RETURN STATEMENT (add to function):
return psi
FUNCTION USAGE (instead on just measurePressure):
glopsi = measurePressure()

Then, using a conditional statement ("if"), you could signal the servo code to start.

All together the code will look something like this:

int dirpin = 2;
int steppin = 3;

void setup() {
  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);

  Serial.begin(9600);  //start the serial sensor
}

void measurePressure(){
  
  int raReading = analogRead(A0);  // Range: 103..968.4466019417
  float psi=((150-0)/(967.8-103))*(raReading-103);  

  Serial.println(psi);
  Serial.println(" ");
  return psi
}

void loop() {

glopsi = measurePressure();
delay (250);

if (glopsi > *A VALUE. (PRESSURE AT WHICH SERVO SHOULD START)*){
  int i;

  digitalWrite(dirpin, LOW);     // Set the direction.
  delay(1000);


  for (i = 0; i<400; i++)       // Iterate for 4000 microsteps.
  {
    digitalWrite(steppin, LOW);  // This LOW to HIGH change is what creates the
    digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
    delayMicroseconds(500);      // This delay time is close to top speed for this

     Serial.println(i);
     Serial.println(" ");
     
  }                              // particular motor. Any faster the motor stalls.

  digitalWrite(dirpin, HIGH);    // Change direction.
  delay(1000);


  for (i = 0; i<400; i++)       // Iterate for 4000 microsteps
  {
    digitalWrite(steppin, LOW);  // This LOW to HIGH change is what creates the
    digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
    delayMicroseconds(500);      // This delay time is close to top speed for this
  }                              // particular motor. Any faster the motor stalls.

     Serial.println(i);
     Serial.println(" ");
     
}
   
}

Post a link to the data sheet for your motor.

The Easydriver can't handle the rated current of the motor and the L293D is totally inappropriate.

The A4988 driver may not work well either, although if you set the current limit properly (absolutely required), the performance may be satisfactory.

I agree 100% with @jremington. We need to see the motor datasheet.

Assuming the motor does require 2 amps per coil, and assuming you need a lot of torque for your valve then I suspect an A4988 will not be suitable. The Pololu DRV8825 has a little more current capacity - but may still overheat and shut down with a 2 amp motor. Stepper drivers with 3 or 4 amp capacity will be a good deal more expensive.

Also, a stepper motor may not be appropriate at all unless you are trying to achieve very fine control of the number of degrees the valve opens.

A low-geared DC motor with suitable limit switches might be more appropriate if the valve just needs to be fully open or closed.

Or just buy a motorized ball valve.

If this is a safety-related project then you should seek approval from your liability insurer before proceeding with the project.

...R

Can you use a solenoid valve instead of the ball valve?

The motor that I am using at this point is:

STEPPER MOTOR
17HS24-2104S
2015.5.14

// I also attached a screen shot of the motor specifications as requested.

I was able to get the motor to run very smooth for a good amount of time with the code attached on the original post dated ( Posted by cterm - Jan 01, 2017, 12:07 am )
that file name was " Stepper_Motor_Code.txt"

my original plans were to upgrade the stepper if I had a working code but now that might not be possible.

PROBLEM: the major problem I am having is that the ball valve gets hotter then 125C and most servo or solenoid valves that I have found can not operate near those temperatures.

  • My thoughts to solve this was to mount a motor a good distance below the valve and drive a chain gear. moving it 90 degrees one way or the other to open or close it. how ever I used a grinder and removed the 90 degree stops so I could use the DC motor with 90 degree stops (I kind of like that idea.)

NOTE: I also have a 24v p40-250 DC motor (AmpFlow Pancake Motors) that I am not using at this moment. I can also buy one as well.
I have a few limit switches as well.

  • if anyone has any simple, quick and easy code for the DC motors along with any clues on how the switches should be connected, I would be very interested in that if it is a quick solution to my problem.

however I do have the motor and sensors already connected to a bench test arduino so if it is easier to get the code to work that would be good before I dismantle the current set up and switch gears (/motors)

the following code seems to be more accurate for the pressure transducer.

float s0 = analogRead(A0);    // read the sensor voltage
int psi = ((s0-95)/204)*50;   // converts RawData From INPUT pin to PSI.

if it can be done I think I am getting close.

I have been working on the following code and having some interesting results.

int Stepping = false;   // hope that this sets the initial Stepping integer to false.

int DirPin = 8;     // Sets Digital Pin 8 on Arduino as the StepperMotor "Direction".

int StepPin = 9;    // Sets Digital Pin 9 on Arduino as the StepperMotor "Step" otherwise known as how 
                         // far it moves in that direction.

void setup() 
{

  Serial.begin(9600);           // Opens Serial Port at specific baud rate.

  pinMode(DirPin, OUTPUT);      // Sets DirPin as OUTPUT. For calculations that occur within the 
                                          // loop below.
  
  pinMode(StepPin, OUTPUT);     // Sets StepPin as OUTPUT. For calculations that occur within 
                                            // the loop below.

  pinMode(A0, INPUT);           // Sets PressureTransducer as an INPUT. For calculations that 
                                         //occur within the loop below.
}

void loop()
{
float s0 = analogRead(A0);    // read the sensor voltage

int psi = ((s0-95)/204)*50;   // converts RawData From INPUT pin to PSI.


  delay (1500);   // Delays the reading above. 
  
  int i;    // Establishing "i" as a integer as for the below code. 
  int Stepping;   // Establishes Stepping as a integer for the following code below.


  if (analogRead(A0)>=108.00 && Stepping == false)  // 1st "if" statement, having the stepper motor 
                                                                       // move if pressure increase past a certain point 
                                                                       // only if the variable "Stepping" is satisfied.
                                                                       // also 108.00 is upper limit of 3PSI (for bench 
                                                                       // testing purposes)
    {
      digitalWrite(DirPin, LOW);     // Set the direction.

      Stepping = true;  // Changes Stepping variable from 'false' to 'true'

      delay(100);         // I think there is a delay issue (possible??)
  
      for (i = 0; i<400; i++)       // Iterate for 400 microsteps. (this is a 90 turn for the stepper used)
  
      digitalWrite(StepPin, LOW);  // This LOW to HIGH change is what creates the
      digitalWrite(StepPin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
      delayMicroseconds(400);      // This delay time is close to top speed for this
                                             // particular motor. Any faster the motor stalls. 
                                             // *** Those notes above were from the working code originally.
                                             // this is a possible issue.

      Serial.print(psi);       // the following is me testing this on a serial port to see what is showing up.
      Serial.print(" PSI ");
      Serial.print("   =   ");
      Serial.print(s0);
      Serial.print("   =   ");
      Serial.print("Valve is Open");
      Serial.println (" ");
      Serial.println(" ");
  
      delay(analogRead(A0)<=94.00);  // attempting to use a delay that is dependent on the signal from 
                                                  //  PressureTransducer
                                                  // also 94.00 is ~ 0PSI (for bench 
                                                  // testing purposes) I hoped this would reset it all but the.
                                                  // stepping does NOT seem to be recording or is being overwritten
    }


  if (analogRead(A0) <= 80.00 && Stepping == true)    // 2nd "if" statement, having the stepper motor
                                                                         //  move if pressure increase past a certain
                                                                         // point only if the variable "Stepping" 
                                                                        // is satisfied. 80.00 ~ -3PSI
    {
      digitalWrite(DirPin, HIGH);     // Set the direction.
      Stepping = false;
      delay(100);

      for (i = 0; i<400; i++)       // Iterate for 4000 microsteps
  
      digitalWrite(StepPin, LOW);    // This LOW to HIGH change is what creates the
      digitalWrite(StepPin, HIGH);   // "Rising Edge" so the easydriver knows to when to step.
      delayMicroseconds(400);       // This delay time is close to top speed for this
                                              // particular motor. Any faster the motor stalls. 
                                              // *** Those notes above were from the working code originally.
                                              // this is a possible issue.

      Serial.print(psi);         // the following is me testing this on a serial port to see what is showing up.
      Serial.print(" PSI ");
      Serial.print("   =   ");
      Serial.print(s0);
      Serial.print("   =   ");
      Serial.print("Valve is Closed");
      Serial.println (" ");
      Serial.println(" ");
  
      delay(analogRead(A0)>=94.00); // attempting to use a delay that is dependent on the signal from 
                                                  //  PressureTransducer
                                                  // also 94.00 is ~ 0PSI (for bench 
                                                  // testing purposes) I hoped this would reset it all but the.
                                                  // stepping does NOT seem to be recording or is being overwritten
  
    }
}

observed problems and thoughts:

  • one problem I am having is that the motor is jerking all over the place. I have a feeling this is an issue with the StepPin = 9 High and Low settings, delays/microdelays and possibly the DirPin.

  • The "int Stepping" = true/false is not holding or is being overwritten faster then expected.

  • the "if" statements might be off as well.

  • I think there is a issue with the initial motor start up position. as the motor seems to twitch all over the place when starting up.

after a few seconds of twitching from the motor during initial start up, the motor does not move that much or as often. above all the motor does not seem to be moving in the desired motion in what I had hoped to code for.

any help would be greatly appreciated.

once again if someone has any code, and diagrams for a dc motor and ball valve, that would be very helpful.

CT

stepper motor spec.JPG

Image from Reply #5 so we don't have to download it. See this Image Guide

7246ccbe511e99dae4ebd3bc4c97b9a7fb8c7b3a.jpg

...R

One thing I forgot to mention earlier is that a stepper motor will consume almost the full current (2 x 2.1 amps) all the time even when it is not moving. If you disable the motor to save power you risk losing position.

And, with a stepper motor you will also need at least one limit switch so the Arduino can figure out where the ZERO or HOME position is.

...R

I have limit switches. Need help with how to set them up if possible a diagram and wiring instructions would be very helpful.

Also the 24v D.C. Motor I said I have is most likely over kill.

The valve is 3/4" copper steam with 1/4" carbon fiber heat insulation plus added strength. That two is over kill as well but there is max of 100psi saturated steam that this valve holds back.

A 12v (? 2A ? ) DC motor might be enough torque??? It's doesn't require that much torque to close or open.

If I go with a D.C. Motor I am under the impression that I will need a driver for it if I am going to use a arduino and greater then 5V.

Prototype specs only require valve to be open or closed. All open or all closed with no in between.

Thanks again.

CT

For safety there is a spring loaded pressure pop off valve set at 100psi to release pressure for peace of mind.

I also see that the motor requires 2.1A per pole.

I was able to get this motor to work well with the original code.

This is the issue. The easy driver motor operates between 250mA and 750mA.

Heat is building on the driver and the motors are wanting more power.

Any thoughts in what I should use?

I also see that the motor requires 2.1A per pole.

That is the maximum allowed steady state current, not the "required" current.

You can set a modern motor driver to provide less current, but you get less torque from the motor. The DRV8825 stepper driver from Pololu will handle 1.5 amperes/winding without extra cooling and this may be enough for your application.

If not, you will need an industrial stepper driver, like GeckoDrive.

I agree with Reply #10.

You should expect both the stepper motor and the stepper driver to be hot in normal use - uncomfortable to touch.

You could set the limit switches so that they are triggered by the valve at its permitted extremes of movement. The Arduino will regularly check the state of both switches and (for example) will stop the valve moving clockwise if the clockwise limit has been triggered. Try writing a simple program that shows the switch states on the Serial Monitor and press the switches with a finger to change the state.

...R
Stepper Motor Basics
Simple Stepper Code

Thank you all for your help so far.

I just ordered the DRV8825 Stepper Motor Driver for the current stepper motor.

attached to this response is my proposed setup so far.

if someone can please give it a second look and help with the code to make this run properly.

Thanks
CT

Thank you all for your help so far.

I just ordered the DRV8825 Stepper Motor Driver for the current stepper motor.

attached to this response is my proposed setup so far.

if someone can please give it a second look and help with the code to make this run properly.

Thanks
CT

I don't think you need to connect RESET and SLEEP to the Arduino - only to +5v.

And, of course you must connect the motor properly - don't rely on wire colours, check with your multimeter to determine which pairs belong to each coil.

It is generally better to arrange switches so they pull the Arduino pin to GND when closed. Use pinMode(pin, INPUT_PULLUP); to use the Arduino's internal pullup resistors.

Arrange the switches so that if something goes wrong and the motor turns the valve too far it won't break the switches.

And, of course, ensure that no matter what goes wrong no person can be injured.

...R