# \[Solved\] Inductive Sensor Not working

**URL:** <https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950>\
**Category:** 3rd Party Boards\
**Created:** [June 11, 2023, 9:06pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950 "2023-06-11T21:06:02Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![drisszorro](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drisszorro/32/976108_2.png) [@drisszorro](https://forum.arduino.cc/u/drisszorro)\
**Post date:** [June 11, 2023, 9:06pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/1 "2023-06-11T21:06:03Z")

</div>

Hi Guys, I am using an SN04-N Green inductive proximity sensor connected to my arduino, I can assure that alone it is working properly (the red led in it turns on when it touches a metal) but it when I use it with the arduino keeps sending random values (0-1), First I thought that the problem is with the power since it uses 6-36V, so I powered it with a 9V supply, but the problem still persists, Any Help Please!

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 11, 2023, 9:10pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/2 "2023-06-11T21:10:26Z")

</div>

> [@drisszorro](#):
>
> I powered it with a 9V supply,

Are you using a PP3 battery by any chance ?

Please post your sketch and a schematic of your project

---

<div class="post-metadata">

**Author:** ![drisszorro](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drisszorro/32/976108_2.png) [@drisszorro](https://forum.arduino.cc/u/drisszorro)\
**Post date:** [June 11, 2023, 9:39pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/3 "2023-06-11T21:39:37Z")

</div>

The shematic looks something like this (The exact pins are the same as in the code not this picture, the stepper motor is connected to an independant power adapter)

 ![SHEME](https://europe1.discourse-cdn.com/arduino/original/4X/d/1/2/d1215e5d06cb500eacd12af22356dbfca55f88af.png)

```arduino
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Servo.h>
#include <Stepper.h>

const int irSensorPin = 7; // Pin connected to the OUT pin of the IR sensor
const int servoPin = 9;
const int buzzerPin = 4;
const int redLedPin = 6;
const int greenLedPin = 5;
const int moistureSensorPin = A0;
const int inductiveSensorPin = 8;
const int trigPin = 2;
const int echoPin = 3;

Servo myservo;

// IR DETECTION
bool itemDetected = false;

// INDUCTIVE SENSOR
int inductiveValue = LOW;

// ULTRASONIC SENSOR
unsigned long openStartTime = 0;
const unsigned long openDuration = 2000; // 2 seconds

//LiquidCrystal_I2C lcd(0x27, 2, 16); //FOR LCD SCREEN

// STEPPER MOTOR
const int stepsPerRevolution = 2048;
Stepper myStepper = Stepper(stepsPerRevolution, 11, 12, 2, 3); //FOR STEPPER MOTOR (Stepper.h LIB)

void setup() {
  myservo.attach(servoPin);
  myservo.write(90);
  pinMode(irSensorPin, INPUT); // Set the IR sensor pin as an input
  pinMode(buzzerPin, OUTPUT);
  pinMode(inductiveSensorPin, INPUT);
  pinMode(redLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //lcd.begin(16, 2); // Initialize the LCD
  //lcd.backlight(); // Turn on the backlight
  //lcd.setCursor(0, 0);
  //lcd.print("Smart Bin");
  //lcd.setCursor(0, 1);
  //lcd.print("Waiting...");
  Serial.begin(9600);
  digitalWrite(redLedPin, HIGH);
  digitalWrite(greenLedPin, LOW);
}

void loop() {
  calculateBinDepth();
  MetalTest();
  WetTest();
  int irValue = digitalRead(irSensorPin); // Read the value from the IR sensor
  if (irValue == LOW && !itemDetected) {
    itemDetected = true;
    bool IsWet = WetTest();
    bool IsMetal = MetalTest();
    //lcd.clear();
    //lcd.setCursor(0, 0);
    //lcd.print("Item detected");
    Serial.println("Item detected");
    digitalWrite(redLedPin, LOW);
    digitalWrite(greenLedPin, HIGH);
    playBuzzer();
    if (IsMetal){
      Serial.println("Metallic Waste");
      openBin();
      closeBin();
      calculateBinDepth();
    } else {
      if (IsWet){
        Serial.println("Wet Waste");
        MoveStepper(0.33);
        openBin();
        closeBin();
        calculateBinDepth();
        MoveStepper(1-0.33);
      } else {
        Serial.println("Dry Non Metallic Waste");
        MoveStepper(0.66);
        openBin();
        closeBin();
        calculateBinDepth();
        MoveStepper(1-0.66);
      }
    }
    digitalWrite(redLedPin, HIGH);
    digitalWrite(greenLedPin, LOW);
    //lcd.clear();
    //lcd.setCursor(0, 0);
    //lcd.print("Smart Bin");
    //lcd.setCursor(0, 1);
    //lcd.print("Waiting...");
    delay(2000);
    itemDetected = false;
  }
}

void openBin() {
  myservo.write(180);
  delay(200);
}

void closeBin() {
  myservo.write(90);
  delay(500);
}

void playBuzzer() {
  tone(buzzerPin, 4000);
  delay(500);
  noTone(buzzerPin);
}

void MoveStepper(float x) {
	myStepper.setSpeed(10);
	myStepper.step(x*stepsPerRevolution);
	delay(1000);
}

bool MetalTest() {
  int inductiveValue = digitalRead(inductiveSensorPin);
  Serial.println(inductiveValue);
  if (inductiveValue == 1) {
    return true;
  } else {
    return false;
  }
}

bool WetTest() {
  int moistureValue = analogRead(moistureSensorPin);
  if (moistureValue > 900) {
    Serial.println(moistureValue);
    return false;
  } else {
    Serial.println(moistureValue);
    return true;
  }
}

void calculateBinDepth() {
  // Measure the distance using the ultrasonic sensor
  delay(500);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  float distance = duration * 0.034 / 2;

  // Print the bin depth to the Serial monitor
  Serial.print("Bin Depth: ");
  Serial.print(distance);
  Serial.print(" cm\n");
  delay(500);
}

```

---

<div class="post-metadata">

**Author:** ![drisszorro](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drisszorro/32/976108_2.png) [@drisszorro](https://forum.arduino.cc/u/drisszorro)\
**Post date:** [June 11, 2023, 9:40pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/4 "2023-06-11T21:40:24Z")

</div>

I am not using PP3 batterys

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [June 11, 2023, 9:49pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/5 "2023-06-11T21:49:22Z")

</div>

I think the output of the sensor is just a pulse to use as an interrupt.

> **[Arduino Tachometer - Codrey Electronics](https://www.codrey.com/arduino-projects/arduino-tachometer/)**
>
> Here’s a simple Arduino Tachometer project for measuring and displaying speed of rotation. You can find similar projects everywhere on the web but this one has some unique features. First off, the idea is based \[…\]

---

<div class="post-metadata">

**Author:** ![drisszorro](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drisszorro/32/976108_2.png) [@drisszorro](https://forum.arduino.cc/u/drisszorro)\
**Post date:** [June 11, 2023, 10:13pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/6 "2023-06-11T22:13:34Z")

</div>

so is there anyway I can use it for my project to detect metal objects?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [June 11, 2023, 10:40pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/7 "2023-06-11T22:40:39Z")

</div>

What does your data sheet say about the value the inductive sensor will provide? Your code seems to say there will be different levels.

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [June 11, 2023, 10:58pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/8 "2023-06-11T22:58:21Z")

</div>

Change:

```arduino
 pinMode(inductiveSensorPin, INPUT);

```

to

```arduino
 pinMode(inductiveSensorPin, INPUT_PULLUP);

```

The sensor has an npn transistor to pull the input low, but there is nothing to ensure it correctly goes high.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [June 11, 2023, 11:32pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/9 "2023-06-11T23:32:22Z")

</div>

> [@JohnLincoln](#):
>
> npn transistor to pull the input low

The link in post 5 seems to indicate the sensor has a (external) pullup resistor.

> "... inductive proximity sensor with a normally-open (N/O) NPN transistor output, surprisingly with an internal pull-up [resistor](https://www.codrey.com/resistor/resistor-basics/) that’s not clearly mentioned in the datasheet!..."

---

<div class="post-metadata">

**Author:** ![drisszorro](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drisszorro/32/976108_2.png) [@drisszorro](https://forum.arduino.cc/u/drisszorro)\
**Post date:** [June 11, 2023, 11:34pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/10 "2023-06-11T23:34:58Z")

</div>

I tried it, it did not fix the problem but the output became constant 1 instead of random 1 & 0

---

<div class="post-metadata">

**Author:** ![drisszorro](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drisszorro/32/976108_2.png) [@drisszorro](https://forum.arduino.cc/u/drisszorro)\
**Post date:** [June 11, 2023, 11:36pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/11 "2023-06-11T23:36:43Z")

</div>

here is a description of it from where I bought it

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/a/e/c/aec7cba82fb79a352bea2a39a246914c1843c881.png)

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [June 11, 2023, 11:48pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/12 "2023-06-11T23:48:35Z")

</div>

The paragraph reads like it only sends a level or a pulse. With @JohnLincoln INPUT\_PULLUP keeping the normal state HIGH without floating, do you get an output LOW when metal is sensed?

---

<div class="post-metadata">

**Author:** ![drisszorro](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drisszorro/32/976108_2.png) [@drisszorro](https://forum.arduino.cc/u/drisszorro)\
**Post date:** [June 11, 2023, 11:52pm UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/13 "2023-06-11T23:52:27Z")

</div>

No the output is constant, I think that the problem might be in the installation as i see in this guide he used a diode between the signal output and the arduino, what do you think ?[Como usar com Arduino - Sensor Indutivo NPN de Proximidade SN04-N - BLOG MASTERWALKER SHOP](https://blogmasterwalkershop.com.br/arduino/como-usar-com-arduino-sensor-indutivo-npn-de-proximidade-sn04-n)

---

<div class="post-metadata">

**Author:** ![drisszorro](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drisszorro/32/976108_2.png) [@drisszorro](https://forum.arduino.cc/u/drisszorro)\
**Post date:** [June 12, 2023, 12:10am UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/14 "2023-06-12T00:10:57Z")

</div>

Oh God, finally worked after I powererd the sensor with 5V + I changed the line that you told me  
THANK YOU!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 9, 2023, 12:11am UTC](https://forum.arduino.cc/t/solved-inductive-sensor-not-working/1136950/15 "2023-12-09T00:11:30Z")

</div>

Closed on December 9, 2023, 12:11am UTC

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