This tutorial show how to use an HC-SR04 ultrasonic sensor using VSCODE and PlatformIO to program the Arduino board.
Here is the code from the tutorial.
const int trigPin = 10;
const int echoPin = 9;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = (duration * 0.034) / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
![](https://i.ytimg.com/vi/yQKiPUfxuL4/maxresdefault.jpg)