Notifications
Clear all

Adding a sensor input to run a stepper motor (arduino)  

  RSS

Anthonydkay
(@anthonydkay)
Active Member
Joined: 2 months ago
Posts: 7
2020-05-21 8:55 am  

I would appreciate help to add the code to activate the following sketch 

The signal voltage would be a 12v signal from another component so when it starts the stepper motor will run the following sequence , reset and wait for the next signal 

 

// Include the Arduino Stepper Library
#include <Stepper.h>

// Number of steps per output rotation
const int stepsPerRevolution = 200;

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup()
{
// set the speed at 300 rpm:
myStepper.setSpeed(300);
// initialize the serial port:
Serial.begin(9600);
}

void loop()
{
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(-800);
delay(500);


Quote
triform
(@triform)
Reputable Member
Joined: 12 months ago
Posts: 305
2020-05-21 2:14 pm  

Hi @anthonydkay,

Can you elaborate on the 12v input signal you will have?  What type of components is it?  Typically Arduinos want 3-5v input on the GPIO pins.  A direct 12v would probably hurt the chip and or board. 


M4krD4d liked
ReplyQuote
Anthonydkay
(@anthonydkay)
Active Member
Joined: 2 months ago
Posts: 7
2020-05-21 2:18 pm  

Ahh thanks for the heads up, it is a signal that comes from a plc that starts another motor  


ReplyQuote
Ruplicator
(@ruplicator)
Trusted Member
Joined: 3 months ago
Posts: 95
2020-05-21 3:56 pm  
Posted by: @anthonydkay

I would appreciate help to add the code to activate the following sketch 

The signal voltage would be a 12v signal from another component so when it starts the stepper motor will run the following sequence , reset and wait for the next signal 

Hi and welcome to the forum. There are a vast number of ways to implement the function you are wanting. However, choosing the best one depends a lot on what you want to accomplish beyond one rotation of the motor.

A simple insertion of: while(digitalRead(!signal_pin)) delay(1);

Where signal_pin is defined as a  GPIO digital input pin where your signal places a HIGH signal when activation is needed.

But this approach may not be appropriate for more complex needs. There are numerous samples available from many sources that will give you ways to approach your project. Asking questions on specific problems seems to get the best results from forum members. Asking them to write code for you may not get the desired results you want. 😉  


ReplyQuote
triform
(@triform)
Reputable Member
Joined: 12 months ago
Posts: 305
2020-05-21 4:04 pm  

@anthonydkay

You will need to add a TTL contact activate in the ladder (if you have a TTL IO card) or tack another relay in parallel with the motor relay and have that switch 5v to the Arduino.  If you go the relay route, then you will need to debounce the contact out to the Arduino GPIO or it will fire multiple times. You would also need a flyback diode across the relay.  There may be a pre-built switch that will work in place if the added relay, I think there is, but it's been a long time sis I did PLC stuff.

I hope this helps.

Scott


ReplyQuote
Anthonydkay
(@anthonydkay)
Active Member
Joined: 2 months ago
Posts: 7
2020-05-21 4:14 pm  

@triform

thanks for the input i will give it a try


ReplyQuote
Anthonydkay
(@anthonydkay)
Active Member
Joined: 2 months ago
Posts: 7
2020-05-21 4:16 pm  

@ruplicator thanks much appreciated 


ReplyQuote
Anthonydkay
(@anthonydkay)
Active Member
Joined: 2 months ago
Posts: 7
2020-05-24 1:08 pm  

@ruplicator

Hi Thanks for the advice 

this is what i compiled, but i still cant get it to work 

any idea wher i am going wrong?

// Include the Arduino Stepper Library
#include <Stepper.h>

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
//configure pin 2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);

}

 

void loop() {
//read the signal value into a variable
int sensorVal = digitalRead(2);
//print out the value of the signal
Serial.println(sensorVal);
while(digitalRead(2)) delay(1);

// Number of steps per output rotation
const int stepsPerRevolution = 200;

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

// set the speed at 300 rpm:
myStepper.setSpeed(300);

// step 4 revolution in one direction:

myStepper.step(-800);
delay(500);

}


ReplyQuote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 467
2020-05-24 1:51 pm  

It is unclear what it is you are trying to do?

Are you able to post the associated circuit details?

To make the source code look nice and easy read and to copy/paste.

https://forum.dronebotworkshop.com/question-suggestion/sticky-post-for-editing/#post-4939

 

// Include the Arduino Stepper Library
#include <Stepper.h>

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  //configure pin 2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  //read the signal value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the signal
  Serial.println(sensorVal);
  
  while(digitalRead(2)) delay(1);

  // Number of steps per output rotation
  const int stepsPerRevolution = 200;

  // Create Instance of Stepper library
  Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

  // set the speed at 300 rpm:
  myStepper.setSpeed(300);

  // step 4 revolution in one direction:
  myStepper.step(-800);

  delay(500);

}


 

This post was modified 2 months ago 2 times by robotBuilder

ReplyQuote
Ruplicator
(@ruplicator)
Trusted Member
Joined: 3 months ago
Posts: 95
2020-05-24 2:36 pm  

robotBuilder is correct that we are missing several details and a general understanding of what your trying to accomplish.

1. Give us a general overall description of what your are trying to accomplish and what this project will be used for. This is very important for us to give guidance on the best approach.

2. Does the 12 volt signal continue to stay high beyond the completions of stepper revolutions? 

3. Written the way you have it "while(digitalRead(2)) delay(1);" the MCU will continue to read the GPIO pin 2 while it is HIGH. It will exit the while loop when the pin goes LOW. If you change the line of code and add the exclamation point NOT symbol e.g. "while(!digitalRead(2)) delay(1);" It will loop while the signal on GPIO pin 2 is LOW and exit the loop when the signal goes high.

However, if you use the while(!digitalRead(2)) delay(1); and the signal on GPIO pin 2 stays high, the program will continue to loop and the stepper will keep running. It would require additional code to manage this issue. 

I hope this shows why we keep asking for more information as the general objective and all the details are very important as computers are not forgiving when it come to logic.


ReplyQuote
Anthonydkay
(@anthonydkay)
Active Member
Joined: 2 months ago
Posts: 7
2020-05-24 4:09 pm  

THanks for trying to help , I am really just a beginner at this trying hard , but a bit frustrated 

That being said i am using arduino uno and a l298n H bridge controller to run a nema 17 stepper motor 

pins 8,9,10,11 on the Uno connected to motor outputs on the L298n.  12v dc supplied to the L298n and the 5 v output from the l298n is supplying power th the Uno.

 

What I am trying to achieve is that a Omron plc that supplies a signal to run another operation on a machine, once this signal is received buy the Uno (not sure if i should connect it to the analog or digital inputs) it will run the stepper motor at 300 rpm for 800 steps  in one direction, then pause and wait for the next signal 

I think that the stepper motor sequence should stop once the signal from the plc is dropped 

i hope that this makes it clearer any help really appreciated


ReplyQuote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 467
2020-05-24 11:18 pm  

Your requirements seem trivial but the devil is in the detail.
This "signal" from the plc is at what voltage and how long does it last and how frequently does it occur?
You can test the Arduino code using just a simple button switch as a stand in for the plc signal.
The code needs to wait for a signal (button press) and then set a got_signal flag. If the got_signal flag is set then the code needs to carry out the stepper motor sequence. When the sequence is finished the got_signal flag can be reset.

If button pressed set flag
if flag is set then carry out stepper motor sequence ending with a flag reset

 


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 455
2020-05-25 8:57 am  

@anthonydkay

If I said:
My car is broken, I'd appreciate some help.
What would your response be? 🙂

Same situation here... so please confirm:
A) That you are only applying 5v to the GPIO sensor pin
B) Current behavior VS expected behavior

Without those details, we'll all be just guessing.

Cheers!


ReplyQuote
Anthonydkay
(@anthonydkay)
Active Member
Joined: 2 months ago
Posts: 7
2020-05-25 11:41 am  

@frogandtoad

Yep 5V to the GPIO sensor pin 

I managed to sort it out 

thanks for your help 


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 455
2020-05-28 10:20 am  

@anthonydkay

Posted by: @anthonydkay

@frogandtoad

Yep 5V to the GPIO sensor pin 

I managed to sort it out 

thanks for your help 

No problem, glad you sorted it out.

For the benefit of others following this thread, what was the actual problem?


ReplyQuote