Using proximity sensors to drive stepper motor

Conundrum - Help needed (for a gallery installation).

I am an enthusiastic coding novice. I have tried for over a week to get a stepper motor to turn one direction (+1/2 revolution) then reverse (-1/2 revolution from zero) based on 2 IR distance sensor input.

Basically I have fixed an arrow to the top of a stepper motor and want it to point in the direction where an object is closest to one of the sensors (facing left and right). Say if an object was 50cm away from sensor one and the other sensor detected nothing or a distance further away than that, the motor would turn that direction.

I have tried several convoluted codes but can anyone help me out?

I have an EasyDriver motor controller, and a simple Stepper + 2 IR Sharp sensors.

(I hope I’m explaining myself - apologies if not)…

Post what you have now for code and a link to the sensors and we’ll see what the problem is.

click to open

Okay - here goes (Thanks)

#include <DistanceGP2Y0A21YK.h>

DistanceGP2Y0A21YK Dist;
DistanceGP2Y0A21YK Dist2;
int comparison;

int distanceM34;

int distanceIR1;
int distanceIR2;

#define DIR_PIN 4
#define STEP_PIN 6

#define DIR_PIN2 3
#define STEP_PIN2 5

void setup() { 
  pinMode(DIR_PIN, OUTPUT); 
  pinMode(STEP_PIN, OUTPUT); 
  pinMode(DIR_PIN2, OUTPUT); 
  pinMode(STEP_PIN2, OUTPUT); 
  Serial.begin(9600);
  Dist.begin(A0); //We are using analogue pin 0 for one IR sensor
  Dist2.begin(A1); //We are using analogue pin 1 for one IR sensor
  
  distanceM34 = 0;
} 

void loop(){ 
  comparison = distanceIR1 - distanceIR2;
  
  if(comparison > (distanceIR1 / distanceIR2) * 0.25){
    rotateM34(distanceM34 * -1, 0.1);  
  }
    
  if(distanceIR1 > 10 && distanceIR1 < 70){
    //rotate a specific number of degrees
    rotateM34((1600*3), 0.1);
    distanceM34 = distanceM34 + (1600*3);
  }
  
}

void rotateM2(int steps, float speed){ 
  //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (steps > 0)? HIGH:LOW;
  steps = abs(steps);

  digitalWrite(DIR_PIN,dir); 

  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH);   
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN, LOW); 
    delayMicroseconds(usDelay); 
  } 
}

void rotateM34(int steps, float speed){ 
  //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (steps > 0)? HIGH:LOW;
  steps = abs(steps);
  digitalWrite(DIR_PIN2,dir); 

  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN2, HIGH);  
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN2, LOW); 
    delayMicroseconds(usDelay); 
  } 
}

SENSORS = Sharp Infra-red Distance Sensor - GP2Y0A02YK

(link: http://sharp-world.com/products/device/ … 0a02_e.pdf )

There are no obvious errors in the code when compiled, but the motors don’t seem to be doing anything.

Actually what would be better to simplify things would be to just get one motor turning left and right (a maximum no. of revolutions) according to IR sensors.

Have you tried writing a program that only does one of the 2 things? Like measure the IR rangefinder(s) and output it’s value to pc or something to check it is working. Or make the stepper driver run the motor indefinitely through software code. Or manually operate the Easydriver with switches and pull-up or -down resistors alone.

At this point you can’t tell if it is the IR measurement, or the stepper driver routines, that have have a fault. It might even be the Easydriver itself that could have a electrical fault somewhere.

It’s better to partition the complexity and work through the problem in parts. Then when you are sure how/that the individual parts work, you try to integrate them into the desired effect.

Thanks Valen.

The steppers and easydrivers do work (though sometimes one has to vary the potential which can be a bit fiddly). I tested the steppers through some basic back and forth code and the easy driver works fine.

I used a basic sketch to get data from the IR sensors to check if they’re working and they work too.

I have to admit I’m a complete coding novice - this code was really patched together from a few bits and bobs I have collected over recent weeks.

I wondered if it was a power issue whether the 5V of the arduino is enough to power the steppers and IR sensors.

Now however I only want to drive one stepper motor and I don’t know how to edit the code to make it work.

I wondered if it was a power issue whether the 5V of the arduino is enough to power the steppers and IR sensors.

Probably not. The Sharp sensors and stepper motors do consume quite a bit of current. There is a good chance that activation of the steppers and sharp sensors at the same time brings the arduino down due to a voltage glitch.

I think it is wise that you also show us how your have everything wired up.

I think the Easydriver has a seperate voltage input that it uses to drive the stepper motor with. This is usually directly connected to a battery or higher than 5 volt powersource. The 5 volt input is only to power the digital control circuitry on it.

I don’t see where the code actually gets the measurements from the sensors. Something like;

distanceIR1 = Dist.getDistanceRaw();
distanceIR2 = Dist2.getDistanceRaw();

Or perhaps the getDistanceCentimeter() function. W/o one of the two you’re not reading any distances. Put in some print statements to see what those values are.

RE Valen

I think the power issues has been resolved - the stepper motors are only small and are designed to function of 4.5 - 6v.

I’ll get a photo or make a schematic of the set up a soon as I can.

RE Mee_n_Mac

I will try this when I get access to my work - probably this evening.

I’m really quite limited with coding. I thought the code already captured this info but I guess not.

Thanks for your help everyone, and for being patient.

I never realised it would be so difficult.

All I really want to do is use two IR sensors - one facing left, the other facing right, to measure how close people approaching each side are, make a comparison, and get a stepper motor to turn left or right based on whatever object is closer.