Advertisement
microrobotics

Pololu 38 kHz IR Proximity Sensor - Fixed Gain, High Brightness

Mar 30th, 2023
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*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:
  2. 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.
  3. */
  4. const int IR_PIN = 2;  // the pin connected to the IR sensor
  5. unsigned long pulse_length;  // the duration of the IR pulse
  6.  
  7. void setup() {
  8.   Serial.begin(9600);
  9.  
  10.   pinMode(IR_PIN, INPUT);
  11. }
  12.  
  13. void loop() {
  14.   // wait for the sensor to detect an object
  15.   while (digitalRead(IR_PIN) == LOW);
  16.  
  17.   // measure the duration of the IR pulse
  18.   pulse_length = pulseIn(IR_PIN, HIGH);
  19.  
  20.   // calculate the distance to the object in centimeters
  21.   float distance = pulse_length / 58.0;
  22.  
  23.   // print the distance to the object
  24.   Serial.print("Distance: ");
  25.   Serial.print(distance);
  26.   Serial.println(" cm");
  27.  
  28.   // delay before reading from the sensor again
  29.   delay(1000);
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement