Inductive Sensor compatibility

Hello everyone, I have a problem with sensors that I would like to connect to my arduino UNO, let me explain: So in fact I have a motor that I run and on this motor I put an aluminum foil to that each time the paper passes in front of a sensor, the sensor sends a signal to my arduino UNO in order to calculate the number of turns made by it. The problem is, there are some weird things going on. Hier is the code I use :

unsigned long DELAY_TIME = 10000;
unsigned long delayStart = 0;
bool delayRunning = false;

int pin = 2;
int rotateminute;

void setup() {
  delayStart = millis();
  delayRunning = true;

  Serial.begin(9600);  
  pinMode(pin, INPUT_PULLUP);
 
}

void loop() {
  
  if (delayRunning && ((millis() - delayStart) >= DELAY_TIME)) {
    delayStart += DELAY_TIME;
    if(rotateminute <= 2) {
      Serial.print(" Motor off");
      rotateminute = 0;
    } else {
      Serial.print("RPM : ");
    Serial.println(rotateminute);
   rotateminute = 0;
    }
  }

  if (!digitalRead(pin)) {
  rotateminute++;
  Serial.println(rotateminute);
  Serial.println(" ");
  } 

}

As you can see I am using the digitalread function to check if the arduino is receiving impulses, the problem is that when I display what the digitalread receives, it only puts 1s and therefore HIGH, even when the motor does not turn, parontre when I no longer put this message to display the 1 and 0 of HIGH or LOW, it does count the number of revolutions made by the engine, but that's not all! Sometimes when the engine is not running, it starts counting revolutions while nothing is happening (there is nothing in front of the sensor). Then on my digitalread condition, when I put an else, it only executes the else even if the else does not respond to the condition it executes more than it does, and does not give a damn about the if, even if this if is correct. So here is my problem and for information I am using a sensor that I did not buy specifically for the arduino, so I am not sure that it is 100% compatible with the arduino, it is an induction sensor. Here you are, thanks in advance for your responses. I want to clarify that I speak French by origin so I may have made mistakes in my translation ::slight_smile: , if so, sorry, and I'm a bit of a beginner in arduino so if there are stupid mistakes or others you know why .. Thanks in advance. If someone is feeling hot, they can test with one of these inductive sensors and see if my code is working. afterwards I do not force anyone.

Here are the references
l18N003 from wenglor
Technical Data :
Range : 5 mm
UB 10 ... 30 V DC : flush
PNP 100 mA

Welcome to the Arduino forum!

Yes, there seems to be some translation problems as well as others.

You write of aluminum foil on the motor shaft, then write of paper.

Please provide a link to the technical data on your sensor, I can find nothing.

I did find a picture and perhaps that is what you are using, but that was all I could find.

Are you trying to us an inductive sensor to detect a shaft rotation with NO variation in the magnetic field from the shaft. Aluminum foil and paper have no effect on the magnetic field.

If I am correct about the sensor, it is designed to monitor the teeth on a rotating gear, so you can count the teeth and compute the rotation speed from that. I have several from junked industrial equipment.

Paul

You have an inductive PROXIMITY switch, you need a protruding part like a key or bolt head for it to sense. Do not connect the output directly to an Arduino pin as it will be the same voltage as the switch's supply voltage (10 to 30V). You will need a voltage divider or other means of reducing the voltage to the Arduino's voltage.
https://www.wenglor.com/wenglor/catalog/productDetail.jsf?wec-appid=Shop_1000_EXT_EN&wec-locale=en_US&itemKey=I18H004

If the sensor has an NPN open-collector output, just use INPUT_PULLUP on the pin its connected to.
If its PNP, do not connect to Arduino pin directly... I suspect this is fine already.

Foil will be fine for inductive proximity sensor, doesn't need to protrude, just be close enough.

You can check the sensor by hand to find its sensitivity - it shouldn't be fluctuating unless there is
some metal moving in front of it - these units have hysteresis built in and are solid reliable industrial
components.

You might be seeing inteference on the wiring though, adding a 2k2 or similar valued pull-up to
bolster the weak internal pull-up resistor might deal with that, if its whats causing fluctuations.

Thank you for all the answers. I think MarkT is right because I was still receiving a HIGH signal even though the sensor was not activated. I didn't have to look if it was an npn or a pnp, and it would be a pnp. So could you provide me with an electrical circuit diagrams that I must use to connect my sensor to my arduino correctly for each "kind" of sensors (For NPN sensors, an another diagram for PNP sensors and a diagram to use a divider as proposed by MarkT) because I think I am not correct with my electrical circuit.

And yes, I have an another question, do some arduino exist that can support or transfer more than
12V like 24V?

PS: The sensor works perfectly with the aluminium sheet.

According to the datasheet, you have a PNP type switch, there is a connection diagram on page 2.
I cannot draw a diagram for you, I don't know what your supply voltage is or whether you are using the switch as normally open or closed.
https://www.wenglor.com/fileadmin/functions/wdm.php?dfile=I18H004.PDF&pfad=fileadmin/download/DATA_SHEETS/EN/&sfile=Data_sheet_I18H004.pdf&zip=false