Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Here's an example code in Arduino that reads the proximity from the Pololu 38 kHz IR Proximity Sensor - Fixed Gain, Low Brightness:
- 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. */
- const int IR_PIN = 2; // the pin connected to the IR sensor
- unsigned long pulse_length; // the duration of the IR pulse
- float calibration_factor = 8.9; // the calibration factor for the sensor
- 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 / calibration_factor;
- // 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