How can I control a stepper motor and then a relay

Hi can anyone help I am trying to control a stepper motor to make one full turn then to switch on a relay for 2 seconds then loop that sketch. I have the stepper going but don’t know how to add the relay into the sketch loop or wire it up to the Arduino uno. Any help would be greatly appreciated. Thanks in advance for any help :+1:

Hi,

would you mind posting your sketch as it is now?

And just for my understanding, is this the timeline of your sketch

LOOP:

  • Stepper motor makes full turn and stands still
  • After that a relay switches ON
  • After the two seconds the relay switches OFF
  • goto LOOP

We also need to know what hardware that you have and how it is all connected and powered.

Please post a schematic or wiring diagram. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Post data sheets or other technical documentation on each component of the project.

Please post the test code.

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.

Yes that’s what I’m looking for👍

Hi I don’t have it put together. So far I have connected a nema 17 to a l298n driver and copied robojax code for running the stepper 1 full turn. I was thinking on using pin 7 for the relay but have no idea how to do this so was looking for some assistance to achieve my end goal which is the stepper to turn a full turn when it stops the relay to come on for 1 second then it starts over repeating that cycle.

Stepper Motor Control - one revolution

This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.

The motor should revolve one revolution in one direction, then
one revolution in the other direction.

Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe

Modefied by Ahmad S. for Robojax.com
on May 19, 2018 at 19:31, at Ajax, Ontario, Canada

*/

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(300);
// initialize the serial port:
Serial.begin(9600);
// L298N for Robojax.com
pinMode(2,OUTPUT);// for EN1
digitalWrite(2,HIGH);// enable EN1
pinMode(3,OUTPUT);// for EN1
digitalWrite(3,HIGH); // enable EN2
}

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

}

The L298 is an very inappropriate driver for a modern bipolar stepper motor.
Please post a data sheet for the motor and we can help to find a better driver.

We can't see your relay. How can we say how to connect it?

Please supply as much information as you can concerning the hardware that you have.

Hi I will take some pictures tomorrow and post you as much info that I have. I’m happy to take advice on what I should use and do it any other way.
Cheers David

If you do not have a data sheet for the stepper, get a clear photo of the nameplate on the stepper so that we can see the part number.

I moved your topic to an appropriate forum category @davidpirie.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Hi thank you for pointing out the driver that I was using I can see that I’m using the wrong driver for the stepper so I will change that
I will change it to ( TB6600 4A 9-42V Stepper Motor Driver) I got on Amazon
The stepper is from Amazon too ( Mcwdoit 5 PCS Nema 17 Stepper Motor Bipolar 2.0 A 83.6oz.in(59Ncm) 47mm Body 4-lead w/ 1m 4-Pin Cable +5 PCS Nema 17 Mounting Brackets for 3D Printer/CNC)
Hope this helps. I don’t understand how to insert the code into the sketch to get the relay to operate it also was purchased on Amazon ( ARCELI 6Pcs 5V One 1 Channel Relay Module Board Shield With Optocoupler Support High And Low Level Trigger Power Supply Module
Cheers
David

Thank you. Sorry about that I didn’t realise

1 Like

To use a TB6600 driver you will need a different library to control the stepper. The Stepper library will not work with your step/dir type driver.

I recommend the MobaTools library. It is available to install uding the IDE library manager. The motors run (non-blocking) in the background so working with the relay is easier.

Please look at the forum guidelines that i linked in post #3 and post the code in post #6 properly (formatted and in a code block).

Please fix the link to the relay so that we dont have to jump through hoops to see it. I haven't the time.

hi thank you for the assistance with the driver i have done that change and stepper works fine with the setup, how do i add a sketch to the sketch (photo attached) and which pin should i use so once the stepper has turned full turn it swithes on for 1 second then off and that just is a continuous loop.
its a single 5v relay.
cheers
David


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