Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Here's a simple code example for Arduino that reads the distance value and prints it to the Serial Monitor:
- 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.
- Remember to connect the sensor's VCC to the 5V pin on the Arduino and the GND to the GND pin on the Arduino.
- */
- const int sensorPin = 2; // Connect the sensor's output to digital pin 2 on the Arduino
- void setup() {
- pinMode(sensorPin, INPUT); // Set the sensorPin as input
- Serial.begin(9600); // Start the Serial Monitor with a baud rate of 9600
- }
- void loop() {
- int distance = digitalRead(sensorPin); // Read the sensor's output
- if (distance == HIGH) { // If the output is HIGH, the distance is less than 5 cm
- Serial.println("Distance is less than 5 cm");
- } else { // If the output is LOW, the distance is greater than or equal to 5 cm
- Serial.println("Distance is greater than or equal to 5 cm");
- }
- delay(1000); // Wait for 1 second (1000 milliseconds) before reading the sensor again
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement