How do I connect this PNP sensors to the Arduino?

Hi. I'm having trouble connecting these two NPN photoelectric sensors (Panasonic EX-24B-PN) to my Arduino.

I'm using these two sensors to control two motors with separate drivers. One sensor will be placed at the inlet and the other one at the outlet of the mechanism. The idea is that the first sensor will turn on the motors when something is detected, and the other one turns them off then nothing is detected anymore.

Here's the wiring diagram from the sensors spec sheet compared to how I have them wired:


Here's the code that I wrote:

int Sensor=13;
int Sensor2=12;
int Motor1_RPWM=11;
int Motor1_LPWM=10;
int Motor1_speed=9;
int Motor2_RPWM=6;
int Motor2_LPWM=5;
int Motor2_speed=3;
int Sensor_signal=digitalRead(Sensor);
int Sensor2_signal=digitalRead(Sensor2);


void setup(){
  pinMode(Sensor, INPUT);
  pinMode(Sensor2, INPUT);
  pinMode(Motor1_RPWM, OUTPUT); 	
  pinMode(Motor1_LPWM, OUTPUT);
  pinMode(Motor1_speed,OUTPUT);
  pinMode(Motor2_RPWM, OUTPUT);
  pinMode(Motor2_RPWM, OUTPUT);
  pinMode(Motor2_speed,OUTPUT);

  digitalWrite(Motor1_RPWM, LOW);
  digitalWrite(Motor1_LPWM, LOW);
  digitalWrite(Motor1_speed,0);
  digitalWrite(Motor2_RPWM, LOW);
  digitalWrite(Motor2_LPWM, LOW);
  digitalWrite(Motor2_speed,0);
}


void loop() {
  if(Sensor_signal==LOW){
    digitalWrite(Motor1_RPWM, HIGH); 
    digitalWrite(Motor1_LPWM, LOW);
    analogWrite(Motor1_speed, 255);
    digitalWrite(Motor2_RPWM, HIGH);
    digitalWrite(Motor2_LPWM, LOW);
    analogWrite(Motor2_speed, 255);
  }

  else{
    digitalWrite(Motor1_RPWM, LOW);
    digitalWrite(Motor1_LPWM, LOW);
    analogWrite(Motor1_speed, 0);
    digitalWrite(Motor2_RPWM, LOW);
    digitalWrite(Motor2_LPWM, LOW);
    analogWrite(Motor2_speed, 0);
  }

  if(Sensor2_signal==LOW){
    delay(2000);
    digitalWrite(Motor1_RPWM, LOW);
    digitalWrite(Motor1_LPWM, LOW);
    analogWrite(Motor1_speed, 0);
    digitalWrite(Motor2_RPWM, LOW);
    digitalWrite(Motor2_LPWM, LOW);
    analogWrite(Motor2_speed, 0);
  }
  else{
    digitalWrite(Motor1_RPWM, HIGH);
    digitalWrite(Motor1_LPWM, LOW);
    analogWrite(Motor1_speed, 255);
    digitalWrite(Motor2_RPWM, HIGH);
    digitalWrite(Motor2_LPWM, LOW);
    analogWrite(Motor2_speed, 255);
  }

}

When I connect the sensors to the power, it seems to work fine. It detects objects within distance and the lights on them turn on. However, when I connect the black output wire to the Arduino pins, the lights seem to lose power and no signal is detected.

Please let me know if I'm doing something wrong.

Thanks a lot!

You may have destroyed the Arduino by making that connection. The inputs cannot tolerate a voltage higher than 5V.

To make the connection properly, use a suitable voltage divider to reduce the voltage to the Arduino input range.

If the sensor operates on 12V, then a 10K:5K (divide by 3) voltage divider will work. Be sure to connect the sensor ground to Arduino ground.

Capture

2 Likes

Do you have NPN or PNP sensors? Your post mentions both.

Those are completely different types of output, and you must be certain which you have!

1 Like

It's 12/24 so it might be better to use an optocoupler. If it's an industrial setting the maintenance crew is more likely to understand that setup as well.

1 Like

What's that?
Hint: Sensor_signal never changes in code.

1 Like

Hi, @arturoparra

For the second time in this thread;

PNP in your title and NPN in the link address title???????????.
What is the EXACT part number of your sensor?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

My apologies for the confusion. The sensors are PNP

Thanks for catching that!

Yeah I had to buy a new Arduino, but it works perfectly now. Thanks a lot!

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