Advertisement
microrobotics

Pololu Digital Distance Sensor (5 cm)

Apr 21st, 2023
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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. const int sensorPin = 2; // Connect the sensor's output to digital pin 2 on the Arduino
  10.  
  11. void setup() {
  12.   pinMode(sensorPin, INPUT); // Set the sensorPin as input
  13.   Serial.begin(9600); // Start the Serial Monitor with a baud rate of 9600
  14. }
  15.  
  16. void loop() {
  17.   int distance = digitalRead(sensorPin); // Read the sensor's output
  18.  
  19.   if (distance == HIGH) { // If the output is HIGH, the distance is less than 5 cm
  20.     Serial.println("Distance is less than 5 cm");
  21.   } else { // If the output is LOW, the distance is greater than or equal to 5 cm
  22.     Serial.println("Distance is greater than or equal to 5 cm");
  23.   }
  24.  
  25.   delay(1000); // Wait for 1 second (1000 milliseconds) before reading the sensor again
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement