Stepper motor with opto coupler on analog pin 2 forward and reverse

Hello,

I wrote this code to make the stepper motor run forward untill analog pin 2 is high and than it should run reverse, untill it's high again to run foward again and so forth.
But when pin 2 is high the motor stops and is just humming.
What am I doing wrong here?

Any help will be much appreciated!


#include <AccelStepper.h>

// Define the AccelStepper interface type:
#define MotorInterfaceType 4

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(MotorInterfaceType, 8, 9, 10, 11);

// Define a variable to store the state of analog pin 2:
int pin2state;

void setup() {
  // Set the maximum steps per second:
  stepper.setMaxSpeed(1000);
  // Set the acceleration rate
  stepper.setAcceleration(100);
  // Set the pin mode for analog pin 2:
  pinMode(A2, INPUT);
}

void loop() {
  // Set the current position to 0:
  stepper.setCurrentPosition(0);

  // Store the current state of analog pin 2:
  pin2state = analogRead(A2);

  // Run the motor forward at 150 steps/second if analog pin 2 is high, otherwise run it backwards:
  while (stepper.currentPosition() != 1000)  {
    pin2state = analogRead(A2);
    if(pin2state > 512){
      stepper.setSpeed(150);
    }else{
      stepper.setSpeed(-150);
    }
    stepper.run();
  }

  // Reset the position to 0:
  stepper.setCurrentPosition(0);
}


Sounds like the motor is changing direction (repeatedly). What is connected to A2, and how is A2 changed from > 512 to < 512 to allow the motor to not keep changing direction?

i read this as the motor direction should be reversed when the pin goes high. is this correct?

the code says it runs on one direction when the pin is high and in the opposite direction when low

How did you power the hardware?

This works for me using regular limit switches at each end with a NEMA 34.
Mine just goes backwards and forwards repeatedly as I have limits at each end.
There is also a speed control on mine as seen by the code.

However you would need to include a schematic of how you have things hooked up.
Layout can be very important with stepper motors and noise on signal lines can also play a large part in how they react (or don't)

/*
  Stepper Motor Test
  stepper-test01.ino
  Uses MA860H or similar Stepper Driver Unit
  Has speed control & reverse switch
  
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/
 
// Defin pins
 
int reverseSwitch = 2;  // Push button for reverse
int driverPUL = 7;    // PUL- pin
int driverDIR = 6;    // DIR- pin
int spd = A0;     // Potentiometer
 
// Variables
 
int pd = 100;       // Pulse Delay period
boolean setdir = LOW; // Set Direction
 
// Interrupt Handler
 
void revmotor (){
 
  setdir = !setdir;
  
}
 
 
void setup() {
 
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
  
}
 
void loop() {
  
    pd = map((analogRead(spd)),0,1023,5000,50);
    digitalWrite(driverDIR,setdir);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
 
}

are these mechanical (not optical) limit switches that need debouncing?

In my case they are mechanical NOT optical.
Optical should not need any form of debounce.

However the program was also tested here with optical and even proximity switches.
Optical and prox both need a stable PSU (NOT the Arduino) and I have a decent regulator to spur off the 24 volt 30 amp PSU to provide that.
Optical were ruled out because there will be a lot of sawdust in the application.
Not ruled out prox yet as this is a project in DEV to give me an AXIS on a wood lathe for facing or tapers.

since they are mechanical, don't they need debouncing?

That is where the pulse delays come in useful.
Between that and the .47uF cap and the 5k resistor which also smooths out any bounce I don't really get any issues.
Use the same method on my baby cnc and never had any issues.

If you google "grbl limit switch" under images you can see lots of examples.
I do run any limit switches in the NC mode and not the NO mode.

image

If you use the Chinese limit switch modules they usually have the debounce built in.

In ALL CASES I use shielded cable !

isn't there already a 30-60k resistor on the input (328p data sheet pg 258)?

I'm using an optical sensor H21A1 with a 100 ohm resitor serial on the + and a 1k ohm pull down resistor paralell to ground on analog pin 2 of the Arduino.

The code and the optical sensor is working, because the motor stops and humms when it reaches the optical sensor. The problem looks like its stuk in a loop when it reaches the "else"part of the code.

In the serial monitor, I can see the value going from 928 to 6 on the analog port when the sensor is closed.

It's a very simple code, but I can't quite jet find the problem, or solution.

Better to use an external with CNC and steppers etc.
But this is off topic as the OP is using optical.

Proper schematic please then people can see better.
Pictures too !

Some "stepper" motors have strict limits on the steps, pulses, speed, and right now we have very little to go on.

I use an optical sensor to do this, I see the value change in the serial monitor from 928 to 6 when closed

If you are unwilling to provide a schematic or pictures then I feel it only fair to tell you that some of us may step away from this topic.

Hi,

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

With photo-interrupters you should not need to use analog input, digital input should work fine.

Have you written your code in stages, getting each stage to work before going on to the next stage?
If so you should have many sketches testing each bit of I/O separately.

Have you written code JUST to test your limit switches on thier own?
Have you written code JUST to drive your stepper forward and backward?

Do you have a DMM?

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

I have written the code JUST to drive the stepper forward and backward.
What is a DMM?

Hope this helps

1 Like

It takes a while to write a schematic, please see the post below :slight_smile:

It could well be that the sensor is buffering hi/low.
Maybe a delay either just before or during the ELSE (not too sure which)

It looks like you don't have any recovery time or back-off but I am not sure.

On my CNC with an optical switch it had to back off 2.0 mm before the switch would read correctly again. Luckily for me in GRBL there is a setting for that.
Also be aware that excessive light can also trigger those types of optical so better to check with a temporary cover of some sort. It doesn't need to be light proof but keep out just the larger amount of light.