Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- First, connect the sensor to the Arduino as follows:
- Vcc to 5V
- GND to GND
- OUT to Digital Pin 2
- 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.
- */
- // Pololu Digital Distance Sensor 10cm
- // Example code for Arduino
- const int sensorPin = 2; // Connect the OUT pin of the sensor to Digital Pin 2
- const int ledPin = 13; // Connect an LED to Digital Pin 13 (built-in LED on the Arduino Uno)
- void setup() {
- pinMode(sensorPin, INPUT);
- pinMode(ledPin, OUTPUT);
- Serial.begin(9600); // Initialize the serial communication
- }
- void loop() {
- int sensorValue = digitalRead(sensorPin);
- if (sensorValue == HIGH) {
- digitalWrite(ledPin, HIGH); // Turn the LED on if the object is within 10cm
- Serial.println("Object detected within 10cm");
- } else {
- digitalWrite(ledPin, LOW); // Turn the LED off if the object is farther than 10cm
- Serial.println("No object detected within 10cm");
- }
- delay(500); // Wait for half a second before reading the sensor again
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement