Go Down

Topic: Infrared proximity sensor - IR 3 pin HCSR501 or HC-SR501 (Read 1 time) previous topic - next topic

spankymonkey

For some reason I was not able to find enough information and sketch that works with this device.
So I decided to put together some short test sketch that worked for me and hopefully it might help those who were looking for similar thing. Notice that no IR Remote or IR Library is required.
Values output are 0 for no activity or 1 for active.
After a few adjustments on the potentiometers with the ideal conditions on my device I noticed a slight delay and not always an immediate response to movement. Moving the sensor itself also promote activity.

[font=Courier]
//Sketch START
//Simple test sketch for IR Proximity sensor 3 pin HCSR501
//VSS, OUT, GND => 5VC, Digital pin 2, GND
//value output 0 or 1

int PIN_DETECT = 2; // digital pin
int sensorValue = 0;
void setup()
{
  pinMode(PIN_DETECT, INPUT);
  Serial.begin(9600); //setting up serial interface
}

void loop() {
  sensorValue = digitalRead(PIN_DETECT); //getting values from IR sensor
  Serial.println(sensorValue);//output value to serial display
  delay(25);
}
//Sketch END
[/font]

Go Up