A video to show how to interface a sinking proximity sensor to an Arduino. When the sensor is activated a green LED lights up and a tone is generated from a speaker. When the sensor is inactivated a red LED lights up.
int sensorVal;
void setup(){
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop(){
sensorVal = digitalRead(2);
// The Logic is inverted a low on pin 2 means a sinking switch is activated
// and a high on pin 2 means the switch is unactivated and pulled up by the internal resistor
// this is not a problem since the controller can interpret the data any way we tell it to
if (sensorVal == HIGH) {
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
noTone(13);
}
else { //sensorVal = LOW
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
tone(13,1000);
}
}
Check out out the following link to see how to connect an PNP sensor:
[ Ссылка ]
Hope this helps.
Dorian
Ещё видео!