Created
October 31, 2019 14:03
-
-
Save RobolinkAkademi/237939ac620e101f62e2a24e9b96b6c0 to your computer and use it in GitHub Desktop.
arduino_JSN-SR04T_ultrasonic_module_example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define Trig and Echo pin: | |
#define trigPin 2 | |
#define echoPin 3 | |
// Define variables: | |
long duration; | |
int distance; | |
void setup() { | |
// Define inputs and outputs | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
// Begin Serial communication at a baudrate of 9600: | |
Serial.begin(9600); | |
} | |
void loop() { | |
// Clear the trigPin by setting it LOW: | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(5); | |
// Trigger the sensor by setting the trigPin high for 10 microseconds: | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
// Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds: | |
duration = pulseIn(echoPin, HIGH); | |
// Calculate the distance: | |
distance = duration*0.034/2; | |
// Print the distance on the Serial Monitor (Ctrl+Shift+M): | |
Serial.print("Distance = "); | |
Serial.print(distance); | |
Serial.println(" cm"); | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment