Advertisement
microrobotics

Pololu Digital Distance Sensor (15 cm)

Apr 21st, 2023
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. To interface with the Pololu Digital Distance Sensor (15 cm), you'll need an Arduino or a similar microcontroller. Here's a simple code example for Arduino that reads the distance value and prints it to the Serial Monitor:
  3.  
  4. To use the code, simply connect the sensor's output to the digital pin 2 on your Arduino board, and upload the code using the Arduino IDE. Open the Serial Monitor to see the distance readings.
  5.  
  6. Remember to connect the sensor's VCC to the 5V pin on the Arduino and the GND to the GND pin on the Arduino.
  7. */
  8.  
  9. // Pololu Digital Distance Sensor - 15 cm example for Arduino
  10.  
  11. const int sensorPin = 2; // Connect the sensor's output to digital pin 2 on the Arduino
  12.  
  13. void setup() {
  14.   pinMode(sensorPin, INPUT); // Set the sensorPin as input
  15.   Serial.begin(9600); // Start the Serial Monitor with a baud rate of 9600
  16. }
  17.  
  18. void loop() {
  19.   int distance = digitalRead(sensorPin); // Read the sensor's output
  20.  
  21.   if (distance == HIGH) { // If the output is HIGH, the distance is less than 15 cm
  22.     Serial.println("Distance is less than 15 cm");
  23.   } else { // If the output is LOW, the distance is greater than or equal to 15 cm
  24.     Serial.println("Distance is greater than or equal to 15 cm");
  25.   }
  26.  
  27.   delay(1000); // Wait for 1 second (1000 milliseconds) before reading the sensor again
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement