Unexpected Multiple Detection by Inductive proximity sensor

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.

// 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.

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.

Which pullup resistor did you mount where?

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

1 Like

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.


Find the edited image. Refer the pin configuration mentioned in first post if the image is not clear. Hope this helps.

Change to:

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

That's one reason of your phantom counts.

May I know where it should be connected?

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

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.

if possible, share a connection diagram.

Arduino Uno 的ATmega328P 芯片已经内置了上拉电阻,简单修改一下外部电路接线和程序,就可以起用。这是ATmega328P 的 Datasheet 的电路结构如下:
5e0e020ed4060591c4c0b95d6b509568_watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JsdWVfc2t5TFpX,size_16,color_FFFFFF,t_70#pic_center
上拉电阻的概念
将一个不确定的信号(高或低电平),通过一个电阻与电源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

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!

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

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.

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.. :smiley: :+1: :coffee: :australia:
PS. I'm off to bed, 33 minutes past midnight here, and I undisputedly need my beauty sleep. :sleeping: :sleeping: :sleeping: :sleeping: :sleeping: :sleeping:

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.



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.

Spec and part number of proximity sensor:-

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.

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.. :smiley: :+1: :coffee: :australia:

1 Like