Advertisement
microrobotics

Pololu GP2Y0A41SK0F

Mar 30th, 2023
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*The Pololu GP2Y0A41SK0F is an analog 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.  
  3. Note: This code assumes that the Pololu GP2Y0A41SK0F 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.
  4. */
  5.  
  6. const int SENSOR_PIN = A0;  // the pin connected to the sensor
  7. float voltage;  // the voltage reading from the sensor
  8. float distance;  // the distance calculated from the voltage
  9.  
  10. void setup() {
  11.   Serial.begin(9600);
  12.  
  13.   pinMode(SENSOR_PIN, INPUT);
  14. }
  15.  
  16. void loop() {
  17.   // read the voltage from the sensor
  18.   voltage = analogRead(SENSOR_PIN) * (5.0 / 1023.0);
  19.  
  20.   // calculate the distance to the object in centimeters
  21.   distance = 13.46 * pow(voltage, -1.22);
  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