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!