Advertisement
microrobotics

Pololu Digital Distance Sensor 10cm

Apr 14th, 2023
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu Digital Distance Sensor 10cm is a simple IR sensor that can be used with a variety of microcontrollers. In this example, I'll provide you with an Arduino code to read the distance from the sensor. Note that this code assumes you are using an Arduino Uno or a similar board.
  3.  
  4. First, connect the sensor to the Arduino as follows:
  5.  
  6. Vcc to 5V
  7. GND to GND
  8. OUT to Digital Pin 2
  9.  
  10. Upload this code to your Arduino and open the Serial Monitor. The sensor will output messages about whether an object is detected within 10 cm or not, and the LED will also turn on or off accordingly.
  11. */
  12.  
  13. // Pololu Digital Distance Sensor 10cm
  14. // Example code for Arduino
  15.  
  16. const int sensorPin = 2; // Connect the OUT pin of the sensor to Digital Pin 2
  17. const int ledPin = 13;   // Connect an LED to Digital Pin 13 (built-in LED on the Arduino Uno)
  18.  
  19. void setup() {
  20.   pinMode(sensorPin, INPUT);
  21.   pinMode(ledPin, OUTPUT);
  22.  
  23.   Serial.begin(9600); // Initialize the serial communication
  24. }
  25.  
  26. void loop() {
  27.   int sensorValue = digitalRead(sensorPin);
  28.  
  29.   if (sensorValue == HIGH) {
  30.     digitalWrite(ledPin, HIGH); // Turn the LED on if the object is within 10cm
  31.     Serial.println("Object detected within 10cm");
  32.   } else {
  33.     digitalWrite(ledPin, LOW); // Turn the LED off if the object is farther than 10cm
  34.     Serial.println("No object detected within 10cm");
  35.   }
  36.  
  37.   delay(500); // Wait for half a second before reading the sensor again
  38. }
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement