const int InfraredSensorPin = 4;//Connect the signal pin to the digital pin 4
const int LedDisp = 13;
void setup()
{
Serial.begin(57600);
Serial.println("Start!");
pinMode(InfraredSensorPin,INPUT);
pinMode(LedDisp,OUTPUT);
digitalWrite(LedDisp,LOW);
}
void loop()
{
if(digitalRead(InfraredSensorPin) == LOW) digitalWrite(LedDisp,HIGH);
else digitalWrite(LedDisp,LOW);
Serial.print("Infrared Switch Status:");
Serial.println(digitalRead(InfraredSensorPin),BIN);
delay(50);
}
Result
Cover the sensor head with your hand, the LED(Pin13) on board will light up, hold it toward an open area, the LED will be off. Open Arduino IDE serial monitor, you can also get the Infrared sensor status.
Analog Adjustable Infrared Proximity Sensor (3cm - 50cm range)
The adjustable infrared sensor features a high-sensitivity photoreflector to perform distance detection function,ranging from 3cm to 50cm. When the infrared light emitted by the emitter gets reflected on a surface that blocked it, the phototransistor can pick up the signal for distance calculation. With a potentiometer for adjustment is arranged for easy and clear use. Compared with regular infrared sensor switch , it has advantages in distance, low interference by visible light, affordable, easy to assemble, easy to use, and can be widely used in robot obstacle Avoidance, assembly lines and many other occasions.
The obstacle detection distance can be adjusted with a potentiometer in copper screw, once done adjusting (eg max 60cm), the sensor will output low flat within the effective distance (eg obstructions at 40cm & 10cm ) to the microcontroller.
Specification
Power supply: 5V
Control Outout: 100mA when supply 5V
Circuit consume: <25mA
Response time: <2ms
Direction: ≤15°
Range: 3~50cm adjustable for opacity and transparent objects
Shell marterial: plastic
A low voltage output will be given when a object is in its detecting scope
When connect to MCU pins, you need to add an 1~10K Ohm resistor for pull-up
Sharp: 30mm(length) x 20mm(wide) x 13mm(thickness) with a 45cm lead wire
RED--->VCC
YELLOW-->Signal
GREY-->GND
Notice : For making the sensor working more stable with your arduino processor, we use an external resistor(range from 1K to 10K) for pull-up!
However, it's also available to connect the signal pin directly to the Arduino digital pin for testing. Just take care of the pin mode initialization in your Arduino software. Set the "INPUT_PULLUP" mode for the digital detection. For more details, please check the Official PinMode function description .
Sample Code
void setup()
{
pinMode(3,INPUT);
Serial.begin(9600);
}
void loop()
{
int val=digitalRead(3);
Serial.println(val);
delay(500);
}