Advertisement
microrobotics

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

Mar 30th, 2023 (edited)
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Here's an example code in Arduino that reads the proximity from the Pololu 38 kHz IR Proximity Sensor - Fixed Gain, Low Brightness:
  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. The calibration factor of 8.9 was determined experimentally, so you may need to adjust it for your specific sensor. */
  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. float calibration_factor = 8.9;  // the calibration factor for the sensor
  7.  
  8. void setup() {
  9.   Serial.begin(9600);
  10.  
  11.   pinMode(IR_PIN, INPUT);
  12. }
  13.  
  14. void loop() {
  15.   // wait for the sensor to detect an object
  16.   while (digitalRead(IR_PIN) == LOW);
  17.  
  18.   // measure the duration of the IR pulse
  19.   pulse_length = pulseIn(IR_PIN, HIGH);
  20.  
  21.   // calculate the distance to the object in centimeters
  22.   float distance = pulse_length / calibration_factor;
  23.  
  24.   // print the distance to the object
  25.   Serial.print("Distance: ");
  26.   Serial.print(distance);
  27.   Serial.println(" cm");
  28.  
  29.   // delay before reading from the sensor again
  30.   delay(1000);
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement