Stepper, Relay, and Inductive Proximity Sensor

Hello everyone,

I am doing a project with all the three mentioned equipments. The logic that I need to work on was to rotate the motor CW and CCW to a particular degree and simultaneously trigger the relay board. I chose the stepper motor for this purpose. Later I realised I need the stepper motor to start from a particular position or zero position so that the device I am attaching to it should work properly. For this I’ve attached a proximity sensor which is 180deg to the position I want my motor to start. The logic I thought is as follows:

  1. As soon as the arduino is plugged in (assuming the motor is not at it’s home position), it should rotate till it finds the proximity sensor. Once it has detected the proximity sensor it must go back 180deg from the sensor.

  2. Since the motor is now at the position, I will attach the device to it and the rest of the code must begin.

P.S: The first step must execute only one time i.e when the arduino is plugged in and the motor is not at the desired position.

I took some online tutorials and combined the code myself. I know it’s noway near to the logic I want. The relay and motors are working in coordination but I ca’n’t figure out how to code this logic with proximity sensor.

#define DIR_PIN 8
#define STEP_PIN 9
#define RELAY1  2
#define RELAY2  3
#define RELAY3  4
#define LED 12
int sensorVal;
void setup() {
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(5, INPUT_PULLUP);
  pinMode(12, OUTPUT);
  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(LED, LOW);

void loop() {
   sensorVal = digitalRead(5);
if (sensorVal==HIGH) {
  digitalWrite(12, HIGH);
  digitalWrite(RELAY1, LOW);          // Turns ON Relays 1
  rotateDeg(90, .2);
  rotateDeg(-90, .1);                //reverse
  digitalWrite(RELAY1, HIGH);         // Turns Relay Off

  digitalWrite(RELAY2, LOW);          // Turns ON Relays 2
  rotateDeg(90, .2);
  rotateDeg(-90, .1);                //reverse
  digitalWrite(RELAY2, HIGH);         // Turns Relay Off

  digitalWrite(RELAY3, LOW);
  rotateDeg(90, .2);
  rotateDeg(-90, .1);                //reverse
  digitalWrite(RELAY3, HIGH);
else {
  digitalWrite(12, LOW);
  rotateDeg(180, 0.1);


void rotateDeg(float deg, float speed) {
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (deg > 0) ? HIGH : LOW;
  digitalWrite(DIR_PIN, dir);

  int steps = abs(deg) * (2 / 0.225);
  float usDelay = (1 / speed) * 70;

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

    digitalWrite(STEP_PIN, LOW);

If anyone can help me with some suggestions or modifications that my code need to follow the above mentioned logic, I’d be grateful.


In general the way to set the ZERO or HOME position for a stepper motor is to move towards the sensor one step at a time and check the sensor beween steps. Something like this pseudo code

read sensor
  are we there yet?
    if not
       take one step

I would write a separate function to do that and then it could be called from setup() and again from any other place where it may be required.

Stepper Motor Basics
Simple Stepper Code