Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*The Pololu 38 kHz IR Proximity Sensor - Fixed Gain, High Brightness is a non-contact infrared sensor that detects objects by emitting and detecting modulated infrared radiation. Here's an example code in Arduino that reads the proximity from the sensor connected to an Arduino board:
- Note: This code assumes that the Pololu 38 kHz IR Proximity Sensor is connected to pin 2 of the Arduino board. If you're using a different pin, you'll need to modify the IR_PIN definition accordingly. Also, this code assumes that the sensor is configured for a fixed gain and high brightness. If your sensor has a different configuration, you'll need to adjust the calibration accordingly.
- */
- const int IR_PIN = 2; // the pin connected to the IR sensor
- unsigned long pulse_length; // the duration of the IR pulse
- void setup() {
- Serial.begin(9600);
- pinMode(IR_PIN, INPUT);
- }
- void loop() {
- // wait for the sensor to detect an object
- while (digitalRead(IR_PIN) == LOW);
- // measure the duration of the IR pulse
- pulse_length = pulseIn(IR_PIN, HIGH);
- // calculate the distance to the object in centimeters
- float distance = pulse_length / 58.0;
- // print the distance to the object
- Serial.print("Distance: ");
- Serial.print(distance);
- Serial.println(" cm");
- // delay before reading from the sensor again
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement