[Solved] Inductive Sensor Not working

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!

Are you using a PP3 battery by any chance ?

Please post your sketch and a schematic of your project

1 Like

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)

#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);
}





I am not using PP3 batterys

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

1 Like

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

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

1 Like

Change:

 pinMode(inductiveSensorPin, INPUT);

to

 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.

1 Like

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 that’s not clearly mentioned in the datasheet!..."

1 Like

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

here is a description of it from where I bought it

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?

1 Like

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

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

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