Inductive Proximity Switch/Limit Switch not working as expected with Arduino

Someone has posted something very similar before with the same proximity limit switch but i am still confused in regards to it in my context.

I am using a 10-30V, PNP, NC proximity switch /limit switch a with linear actuator and Arduino Nano. The objective is that the motor first moves backwards util the extreme limit is reached, afterwards it goes through the main void loop to move three more times twice in forwards and once backwards.

The sensors' works fine, so when the object is in its proximity the LED on it turns off and turns on when it is away. However, the output (in form of 0 and 1) I am getting is quite random and not according to the sensors' LED.

The value I am expecting is Low/High when LED is on and opposite is when it is off. However, I am not getting this. I am about 6 months into Arduino and very stuck.

Any help would be amazing because im out of options atm :smiling_face_with_tear:

Below is my code and my current wiring:

Schematic:

Code:

const int stepPin = 31;
const int dirPin = 33;
const int enPin = 35;

const int limitSwitchPin = 25; // Pin connected to the limit switch

bool homingCompleted = false; // Flag to indicate whether homing is completed

void setup() {
    // Motor setup
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);
    pinMode(enPin, OUTPUT);
    digitalWrite(enPin, LOW);

    // Limit switch setup
    pinMode(limitSwitchPin, INPUT); // Set limit switch pin as input withOUT pull-up resistor

    // Move the actuator to origin during setup
    moveStepperToOrigin(LOW, limitSwitchPin);
}

void moveStepper(int direction, int steps, int delayMicros) {
    digitalWrite(dirPin, direction);
    for (int x = 0; x < steps; x++) {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(delayMicros);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(delayMicros);
    }
}

void moveStepperToOrigin(int direction, int limitSwitchPin) {
    digitalWrite(dirPin, direction); // Move in reverse direction
    while (digitalRead(limitSwitchPin) == LOW) { // Keep moving until limit switch is pressed (set to LOW as nature of NC switch)
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(15); // Adjust this delay as needed
        digitalWrite(stepPin, LOW);
        delayMicroseconds(15); // Adjust this delay as needed
    }

    homingCompleted = true; // Set the homing completed flag
}

void loop() {
    if (homingCompleted) {
        // Movement stages
        // Movement 1
        moveStepper(HIGH, 15000, 15);
        delay(1000); // Pause between movements

        // Movement 2
        moveStepper(HIGH, 5000, 50);
        delay(1000); // Pause between movements

        // Movement 3
        moveStepper(LOW, 20000, 15);
        delay(2000); // Pause between movements (adjust as needed)

        homingCompleted = false; // Reset the homing completed flag to stop loop execution
    }
}

How do you have it wired and are you actually using 10-30V?

So the wiring is as above in the picture, im not sure what else to add but im using 24V. Someone mentioned in a previous post about attaching both grounds to the arduino but i was pretty confused. (Inductive Proximity Switch/Limit Switch not working as expected with Arduino)

By the proximity switch document and your wiring diagram, you seem to be switching the 24 volts into pin 25 of your Arduino.

Yes, you need to connect the 24V power supply GND to the Arduino GND.

You also need to use a potential divider to reduce the 24V output of the PNP, NC proximity switch to a safe value (5V) to connect to an Arduino digital input.

So the power is going to 24V power supply (brown) the first negative/signal wire is to the digital pin 25 (black) and the second negative/ground is going to ground (Blue). Is the wiring incorrect then for this, i had a chat with the brand and that is what they had recommended but i may have overlooked something?
would love any suggestions

so which is the 24v output of the pnp, the black wire? sorry im new to all of this

Yes, that is correct.
The brown wire is +24V, and the blue wire is GND.

From the datasheet:

You do also need to connect a potential divider to the limit switch output, to reduce the 24V to <5V.

Suitable values of resistors to use for a potential divider would be 39kΩ and 10kΩ.

Resistive_divider2

Amazing thank you for your help il try this out!

And if you previously gave 24V to your pin, you should choose another pin now, if your board is still working.

its still working didnt seem like ive burnt it out :sweat_smile:

What you did was the equivalent of trying to measure a voltage with a multimeter, but only using one probe.

It doesn't work, but no damage done.

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