help with coding a stepper motor triggered by a PIR motion sensor

Hello,

I have been trying to write the code for a setup that is essentially a pir motion sensor triggering a stepper motor. I have had some great help from people on the forum, but I’m still struggling to get my head around it. So looking to see if the project interests anyone here.

I would like to have code written that enables a motion sensor to trigger a stepper motor. I will have two set ups of the steppers;

1: a single motion sensor that when triggered activates a stepper motor.

2: a set up with 2 motion sensors and 2 stepper motors with each stepper motor being activated by one of the motion sensors. For example motion sensor A activates stepper 1 and motion sensor B activates Stepper 2. All being run from one Arduino Uno.

This is the run through of actions I would like to happen:

  • When powered up, the motion sensor calibrates.

  • When the motion sensor detects motion, it triggers the stepper motor to turn clockwise 200 steps.

  • Once the stepper motor has turned 200 steps CW it should turn 200 steps counter clockwise (thus returning to its starting position)

  • after it has done one out-and-back it should not start another one for at least 5 seconds.

  • If no motion is detected, the stepper should do nothing.

It would be really helpful to have the code written in a way that I can alter the amount of steps the stepper motors take, and also the amount of time before the motion sensor is triggered again.

I have been working with the Accelstepper and would like to continue working with it.

The Hardware:
I am working with an Arduino Uno, TB6600 Stepper Motor Driver heres a link to it, Pir motion sensor heres a link to it and a 3V 1.7A 68oz-in Stepper Motor heres a link to it.

Here is how the Arudino, stepper driver, stepper and PIR are currently set up:
30715900_10156093522065499_8534738244997218304_n.jpg

Photo-Tom:

  • When powered up, the motion sensor calibrates.

How do you know at which position your motors are? Don't they need to be calibrated to a certain point?

  • When the motion sensor detects motion, it triggers the stepper motor to turn clockwise 200 steps.

  • Once the stepper motor has turned 200 steps CW it should turn 200 steps counter clockwise (thus returning to its starting position)

How fast?

Thanks for the reply wvmarle,

wvmarle:
How do you know at which position your motors are? Don't they need to be calibrated to a certain point?

yes, they would. I have seen a few types, either a limit switch or optical switch. What type of homing switch would you suggest?

And it would be good to be able to have the speed set to say 200, but also have it so i can alter the speed.

And it would be good to be able to have the speed set to say 200

200 furlongs/fortnight? 200 steps per month? 200 months between steps?

Photo-Tom:
Thanks for the reply wvmarle,

yes, they would. I have seen a few types, either a limit switch or optical switch. What type of homing switch would you suggest?

One of those indeed... whatever works for your project. Typical selection parameters include "what space do I have to mount it" and "what is in my box of goodies and other stuff that I happen to have left over from previous projects".

Just re-read your project requirements: you say you want it to move 200 steps CW (at 0.9° per step that makes for 180° total movement) then 200 steps CCW, that's all. This actually sounds much more like an application that calls for a servo instead of a stepper. You get absolute position control so no worries about end stops.

And it would be good to be able to have the speed set to say 200, but also have it so i can alter the speed.

200 of what unit?

Typical selection parameters include "what space do I have to mount it" and "what is in my box of goodies and other stuff that I happen to have left over from previous projects".

Hence the less-than-ideal stepper motor.

With regards to the project requirements, i should say that, i was testing by moving the stepper 200 steps. I would rather be able to move it between 300-360 degrees. That's why i want the steps moved and the speed to be able to be altered a bit.

I don't have any of the parts for the homing switch, so its possibly a case of whats simpler to install for a newby.

With regard to the speed, i was testing it at:
stepper.setMaxSpeed(1000);
stepper.setAcceleration(200);

This still might not give you a unit of speed. Maybe its easier to say that i want it to turn 300 degrees in about 1 second.

Photo-Tom:
I don't have any of the parts for the homing switch, so its possibly a case of whats simpler to install for a newby.

No idea - it depends on your project. They both have their pros and cons.

Optical requires something like a disk with a slot, so it can see when it's in position. The transmitter and receiver go on opposite sides.
End switches require also something mounted to the disk, that will press the switch when in position. Just a single thing to install, but mechanically more difficult.
Magnetic is yet another option. Magnet attached to the motor, reed switch or hall effect sensor to detect when the magnet is in place.
Given the choice I'd prefer contactless over mechanical, optical with magnetic a very good second. Depending on the overall layout of the project, which includes aesthetics.

This still might not give you a unit of speed. Maybe its easier to say that i want it to turn 300 degrees in about 1 second.

That's a more useful number.
Sounds quite fast to me, of course you must take inertia into account, so give it time to accelerate and decelerate. How much time depends on what's attached to your stepper, and often requires some trial and error.

Thanks for the insight.

Do you think its worth using servos over stepper motors, as i find it easier to program them and they position themselves on start up and if i used something like a sail winch servo that can rotate multiple times, it might be simpler than running stepper motors?

Definitely simpler to program, as you know exactly where the servo is. It depends also on your actual application. Steppers tend to provide better speed control and holding power than servos.

Do note that the servo library only allows for 180 steps (common servos are 180° ones and you can set them per whole degree, no decimals). If that 6-turn servo reads 180° as 6 full turns, you're limited to 12° steps! So do carefully check how that one is controlled and how the servo library would deal with it. By setting the pulse length directly you do get better resolution.

I think i could get away with 12° steps, although, i did read on this thread that i could use Servo.writeMicroseconds() rather than the 180 degrees.

i think then i could work with the servos 0-2,160 degrees of travel to 1000 to 2000 usecs command values.

That would give you a resolution of just over 2° which is a lot better for control. The servo will try to move to the new setpoint as fast as possible, which at 1° steps can make for a reasonably smooth slow sweep but at 18° steps certainly not.

Anyway do read the spec sheet of that 6-turn servo carefully as it may work a bit different there.