Advertisement
microrobotics

Pololu Analog Distance Sensor 10-150cm

Mar 30th, 2023
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*The Pololu Analog Distance Sensor 10-150cm is a non-contact distance sensor that uses infrared (IR) radiation to measure the distance of an object. Here's an example code in Arduino that reads the distance from the sensor:
  2. Note: This code assumes that the Pololu Analog Distance Sensor is connected to pin A0 of the Arduino board. If you're using a different pin, you'll need to modify the SENSOR_PIN definition accordingly. Also, the equation used to calculate the distance was determined experimentally, so you may need to adjust it for your specific sensor.
  3. */
  4.  
  5. const int SENSOR_PIN = A0;  // the pin connected to the sensor
  6. float voltage;  // the voltage reading from the sensor
  7. float distance;  // the distance calculated from the voltage
  8.  
  9. void setup() {
  10.   Serial.begin(9600);
  11.  
  12.   pinMode(SENSOR_PIN, INPUT);
  13. }
  14.  
  15. void loop() {
  16.   // read the voltage from the sensor
  17.   voltage = analogRead(SENSOR_PIN) * (5.0 / 1023.0);
  18.  
  19.   // calculate the distance to the object in centimeters
  20.   distance = 27.86 * pow(voltage, -1.15);
  21.  
  22.   // print the distance to the object
  23.   Serial.print("Distance: ");
  24.   Serial.print(distance);
  25.   Serial.println(" cm");
  26.  
  27.   // delay before reading from the sensor again
  28.   delay(1000);
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement