# Unexpected Multiple Detection by Inductive proximity sensor

**URL:** <https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271>\
**Category:** Sensors\
**Created:** [July 18, 2023, 11:35am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271 "2023-07-18T11:35:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![RSTAR](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rstar/32/986619_2.png) [@RSTAR](https://forum.arduino.cc/u/RSTAR)\
**Post date:** [July 18, 2023, 11:35am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/1 "2023-07-18T11:35:40Z")

</div>

Hi Community!

I am trying to measure the rpm of a motor using arduino uno clone and a proximity sensor. I have also used an oled display to show the number of rotations and the rpm itself. Everything is working fine, but the proximity sensor picks up multiple signals at a time (varying from 1-7 per revolution) and increments the same value. This leads to multiple rotation read for a single rotation and fluctuation in rpm. I am using NPN NO 3 wire inductive proximity sensor, 1.3" OLED display and a 200 rpm geared motor with 10 amps PWM controller. The sensing part is an allen key connected to the motor through a drill chuck. The connections are as follows:

1. OLED:-  
Vcc - 5v  
GND - GND  
SCK - A5  
SDA - A4
2. Inductive Proximity Sensor:-  
Brown - 5v  
Blue - GND  
Black - PIN 2

I have provided the code also.

```arduino
// Pin connected to the sensor output
#include "U8glib.h"
U8GLIB_SH1106_128X64 oled(U8G_I2C_OPT_NONE);
const int sensorPin = 2;

// Variables
volatile int rotationCount = 0;
volatile bool rotationDetected = false;
unsigned long prevTime = 0;
float rpm = 0.0;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Attach an interrupt to the sensor pin
  attachInterrupt(digitalPinToInterrupt(sensorPin), countRotation, FALLING);
}

void loop() {
   oled.firstPage();
  do {
    page1();
  } while (oled.nextPage() ); 
  if (rotationDetected) {
    unsigned long currTime = micros();
    unsigned long timeDiff = currTime - prevTime;

    // Calculate RPM
    rpm = 60000000.0 / timeDiff; // Convert to RPM
    rotationDetected = false;
    prevTime = currTime;
  }
}

void countRotation() {
  // Increment the rotation count
  rotationCount++;
  rotationDetected = true;
}
void page1(void) {
  oled.setFont(u8g_font_profont12);
  oled.setPrintPos(0, 10);
  oled.print("No of rotation : " );

  oled.setPrintPos(0, 25);
  oled.print(rotationCount);
  oled.setPrintPos(0, 40);
  oled.print("RPM of DC Motor :");
  oled.print(rpm);
}

```

Would like to hear from the community to solve the issue.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [July 18, 2023, 12:28pm UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/2 "2023-07-18T12:28:06Z")

</div>

> [@RSTAR](#):
>
> proximity sensor picks up multiple signals

A scope is very helpful with such issues.

Can you pull the sensor away from the key but leave it activated and test again? If still phantom pulses occur then they are injected into the cables.

> [@RSTAR](#):
>
> NPN NO 3 wire inductive proximity sensor

Which pullup resistor did you mount where?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 18, 2023, 12:30pm UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/3 "2023-07-18T12:30:08Z")

</div>

> [@RSTAR](#):
>
> Would like to hear from the community to solve the issue.

Would like to see images and hardware diagrams as recommended by the forum guidelines.

---

<div class="post-metadata">

**Author:** ![RSTAR](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rstar/32/986619_2.png) [@RSTAR](https://forum.arduino.cc/u/RSTAR)\
**Post date:** [July 19, 2023, 5:04am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/4 "2023-07-19T05:04:29Z")

</div>

Yes, I kept the sensor away and check by using another metal plate by simply bringing it closer and extending. The problem still prevails (multiple counts increase occasionally when I bring close just once). I didn't use any pullup resistor but the setup works fine. I have attached an edited image of the setup in another reply, please find it.

---

<div class="post-metadata">

**Author:** ![RSTAR](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rstar/32/986619_2.png) [@RSTAR](https://forum.arduino.cc/u/RSTAR)\
**Post date:** [July 19, 2023, 5:05am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/5 "2023-07-19T05:05:33Z")

</div>

![Arduino Forum](https://europe1.discourse-cdn.com/arduino/original/4X/8/c/3/8c382206f5b8bba93db9bf0bb724ca9b1d6ab9aa.jpeg)  
Find the edited image. Refer the pin configuration mentioned in first post if the image is not clear. Hope this helps.

---

<div class="post-metadata">

**Author:** ![JCA34F](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jca34f/32/340148_2.png) [@JCA34F](https://forum.arduino.cc/u/JCA34F)\
**Post date:** [July 19, 2023, 5:15am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/6 "2023-07-19T05:15:24Z")

</div>

> [@RSTAR](#):
>
> ```arduino
> void setup() {
> // Initialize serial communication
> Serial.begin(9600);
> 
> // Attach an interrupt to the sensor pin
> attachInterrupt(digitalPinToInterrupt(sensorPin), countRotation, FALLING);
> }
> 
> ```

Change to:

```arduino
void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  pinMode(sensorPin,INPUT_PULLUP);
  // Attach an interrupt to the sensor pin
  attachInterrupt(digitalPinToInterrupt(sensorPin), countRotation, FALLING);
}

```

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [July 19, 2023, 5:33am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/7 "2023-07-19T05:33:00Z")

</div>

> [@RSTAR](#):
>
> I didn't use any pullup resisto

That's one reason of your phantom counts.

---

<div class="post-metadata">

**Author:** ![RSTAR](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rstar/32/986619_2.png) [@RSTAR](https://forum.arduino.cc/u/RSTAR)\
**Post date:** [July 19, 2023, 5:40am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/9 "2023-07-19T05:40:28Z")

</div>

May I know where it should be connected?

---

<div class="post-metadata">

**Author:** ![RSTAR](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rstar/32/986619_2.png) [@RSTAR](https://forum.arduino.cc/u/RSTAR)\
**Post date:** [July 19, 2023, 5:51am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/10 "2023-07-19T05:51:03Z")

</div>

Thank you for suggesting this modification. But the problem still prevails.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [July 19, 2023, 5:54am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/11 "2023-07-19T05:54:58Z")

</div>

> [@RSTAR](#):
>
> May I know where it should be connected?

You should know that NPN outputs require a pullup resistor to Vcc of the controller. Start with 1k and reduce it if the phantom counts did not disappear.

---

<div class="post-metadata">

**Author:** ![RSTAR](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rstar/32/986619_2.png) [@RSTAR](https://forum.arduino.cc/u/RSTAR)\
**Post date:** [July 19, 2023, 8:08am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/12 "2023-07-19T08:08:16Z")

</div>

if possible, share a connection diagram.

---

<div class="post-metadata">

**Author:** ![cgy12300](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@cgy12300](https://forum.arduino.cc/u/cgy12300)\
**Post date:** [July 19, 2023, 10:51am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/13 "2023-07-19T10:51:08Z")

</div>

Arduino Uno 的ATmega328P 芯片已经内置了上拉电阻，简单修改一下外部电路接线和程序，就可以起用。这是ATmega328P 的 Datasheet 的电路结构如下：  
 ![5e0e020ed4060591c4c0b95d6b509568_watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JsdWVfc2t5TFpX,size_16,color_FFFFFF,t_70#pic_center](https://europe1.discourse-cdn.com/arduino/original/4X/2/2/3/223df6e360e0caf9237ca78aa5a6bcfde03e1939.png)  
上拉电阻的概念  
将一个不确定的信号（高或低电平），通过一个电阻与电源VCC相连，固定在高电平。通过这样，上拉电阻可以使引脚即使在未连接外部组件的时候也能保持确定的逻辑电平，即保持在高电平。

与上拉电阻对应的是下拉电阻，它的含义是：将一个不确定的信号（高或低电平），通过一个电阻与地GND相连，固定在低电平。不过uno板内部只有上拉电阻，如果要使用下拉电阻，uno还要外接电阻。

uno上拉电阻的使用  
例如：  
声明引脚2为输入引脚，并且使2号引脚在没有外接信号输入的情况下保持高电位，则写为：  
pinMode(2, INPUT\_PULLUP)；  
如果单纯写为pinMode(2, INPUT)，在程序中用Serial.print(2)打印2号引脚的状态会发现在0和1无规律变化。  
————————————————  
版权声明：本文为CSDN博主「千里飞刀客」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。  
原文链接：[https://blog.csdn.net/blue\_skyLZW/article/details/111479601](https://blog.csdn.net/blue_skyLZW/article/details/111479601)

---

<div class="post-metadata">

**Author:** ![cgy12300](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@cgy12300](https://forum.arduino.cc/u/cgy12300)\
**Post date:** [July 19, 2023, 10:54am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/14 "2023-07-19T10:54:34Z")

</div>

Sorry, because of my poor English, can only use translation software, reply to mean, in front of the arduino itself is take pull resistance, you only need to write ：“pinMode (2, INPUT\_PULLUP); ” Is ok!

---

<div class="post-metadata">

**Author:** ![RSTAR](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rstar/32/986619_2.png) [@RSTAR](https://forum.arduino.cc/u/RSTAR)\
**Post date:** [July 19, 2023, 11:49am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/15 "2023-07-19T11:49:41Z")

</div>

I tried this but the count deviation just increased (deviated for each detection). Without this the deviation was encountered once every 3-5 detection.

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [July 19, 2023, 2:16pm UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/16 "2023-07-19T14:16:57Z")

</div>

> [@RSTAR](#):
>
> Yes, I kept the sensor away and check by using another metal plate by simply bringing it closer and extending.

Unless you ground the metal plate to the Arduino ground, you are waving a BIG antenna around sensitive circuitry. Try grounding the plate to the Arduino ground and then wave it around the sensor.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [July 19, 2023, 2:29pm UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/17 "2023-07-19T14:29:28Z")

</div>

Hi, @RSTAR  
Welcome to the forum.

Can you tell us the part number of the sensor?  
Can you please post a link to data/specs of the sensor?

Can you please post images of your project, showing the sensor, the motor and the shaft, and the target on the shaft that the sensor detects on each revolution.  
Please show us how you have the sensor positioned to measure RPM.

What size is the target?

How fast are you rotating the motor?

All these things are important so we know how your project is constructed.

The fact the your original code had no pinMode statement for the sensor input, yet you were getting "readings" worries me.

Do you have a DMM? Digital MultiMeter?

Thanks.. Tom.. 😃 👍 ☕ 🇦🇺  
PS. I'm off to bed, 33 minutes past midnight here, and I undisputedly need my beauty sleep. 😴 😴 😴 😴 😴 😴

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 19, 2023, 5:16pm UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/18 "2023-07-19T17:16:26Z")

</div>

> [@RSTAR](#):
>
> Find the edited image.

Why can't you post a photo of the actual connections? They must exist, for you to report the problems. I would prefer to solve a real problem, not a hypothetical one that may not even exist.

This only tells me how you **believe** you connected it. As a diagram, it doesn't come close to a schematic, it only barely suffices as a wiring diagram. If you give the bare minimum here, that is what you get back.

---

<div class="post-metadata">

**Author:** ![RSTAR](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rstar/32/986619_2.png) [@RSTAR](https://forum.arduino.cc/u/RSTAR)\
**Post date:** [July 20, 2023, 5:38am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/19 "2023-07-20T05:38:27Z")

</div>

![WhatsApp Image 2023-07-20 at 10.40.21 (2)](https://europe1.discourse-cdn.com/arduino/original/4X/b/2/d/b2d6731ba2c89dab99ff0521fa83980cb05e28fd.jpeg)  
 ![WhatsApp Image 2023-07-20 at 10.40.21 (1)](https://europe1.discourse-cdn.com/arduino/original/4X/b/6/e/b6edc12327a3275c4219597002d713948d54819e.jpeg)  
 ![WhatsApp Image 2023-07-20 at 10.40.21](https://europe1.discourse-cdn.com/arduino/original/4X/6/8/4/6845877bf3192bfa7cc670f2983acd48c7374cc1.jpeg)

Video of the problem: See the no. of rotation jump from 69 to 72 or something.  
The motor is powered by a 12V supply through the PWM.

> **[Google Drive: Sign-in](https://accounts.google.com/v3/signin/identifier?dsh=S873827%3A1689831300881168&continue=https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F19gEGB4legnnI3JvDBGgnC3S75gNoLOz1%2Fview%3Fusp%3Ddrive_link&followup=https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F19gEGB4legnnI3JvDBGgnC3S75gNoLOz1%2Fview%3Fusp%3Ddrive_link&ifkv=AeDOFXg-mUEkDf8lKRRPOkbFj8U8PSpiMONZE_44xOHMKQxzUvi_7_k_qz7_UXEje9S4yzFdFeu_sw&osid=1&passive=1209600&service=wise&flowName=WebLiteSignIn&flowEntry=ServiceLogin)**
>
> Access Google Drive with a Google account (for personal use) or Google Workspace account (for business use).

---

<div class="post-metadata">

**Author:** ![RSTAR](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rstar/32/986619_2.png) [@RSTAR](https://forum.arduino.cc/u/RSTAR)\
**Post date:** [July 20, 2023, 5:56am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/20 "2023-07-20T05:56:20Z")

</div>

Spec and part number of proximity sensor:-

 ![IMG20230720111302](https://europe1.discourse-cdn.com/arduino/original/4X/9/9/5/99572de38f68c1c888f86fd8ad9986ea0cc38f47.jpeg)

The target is an allen key face (hexagon of side size 2mm).

The motor was tested between 50 rpm to 200 rpm. Yes, I have a digital multimeter.  
The proximity sensor's working range is 6-36V as mentioned in the catalogue but it works just fine with +5V of arduino board. I have also tried the sample with 12V supply also but the problem still prevails. So i decided to go with the 5V itself for simplicity of connection.

The exact pin configuration images and demonstration video is provided in post #19. Please refer it.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [July 20, 2023, 9:04am UTC](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271/21 "2023-07-20T09:04:45Z")

</div>

Hi,  
So you are hand holding the sensor and the motor as the shaft rotates.

Can I suggest you put restrain the motor so it will not move.  
Make a bracket to hold the sensor stable so it can read a stable input and produce a stable input pulse to the UNO.

Your sensorspecs.

### Features/Specs:

- Model: JG-GI12-04N1
- Manufacturer: Jigo
- Material: Metal and plastic
- Operating Voltage: 6 ~ 36 VDC
- Output Type: NPN Normally Open
- Detection object: Metal Objects
- **Detection distance: 4 mm**
- Output Current: 300 mA
- Total Length of Sensor: 55mm
- Thread length: 35mm.
- Outer (Theard) Diameter: M12
- Polarity protection: yes
- Surge protection: yes
- Short circuit protection: yes
- High repeated positioning accuracy
- High switching frequency
- Wide voltage range
- Antivibration, dust, water, and oil prevention
- Reverse power protection, short circuit protection, directly connecting with PLC
- Can replace small switches and limit switches
- Working temperature(°C): -25 ~ +70, ±15%
- Cable Length: Approx 1.5 Meter
- Dimensions: 12 mm Screw Diameter

Note the detection distance == 4mm, you need to mount your hardware on a **STABLE** frame.

Lets do some maths.

69 RPM = 69 / 60 RPS = 60 / 69 = 0.869 seconds period of rotation.

72 RPM = 72 / 60 RPS = 60 / 72 = 0.833 seconds period of rotation.

The difference is 36ms, you need a physically STABLE test bench.

Note the output current can be up to 300mA, I would suggest you use an external pullup resistor to have at least 50mA.  
These are industrial devices.

Tom.. 😃 👍 ☕ 🇦🇺

[Next page](https://forum.arduino.cc/t/unexpected-multiple-detection-by-inductive-proximity-sensor/1149271.md?page=2)
