0% found this document useful (0 votes)
3 views

Note 18_ Safety driving (Project 2)

Uploaded by

Live Channel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Note 18_ Safety driving (Project 2)

Uploaded by

Live Channel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Safety Driving (Project 3)

Index

Circuit setup

Circuit in Tinkercad

Basic sonar codes

Page 01
Safety Driving (Project 3)

Circuit setup

Sonar Sensor

Arduino UNO

Circuit in Tinkercad

Page 02
Safety Driving (Project 3)

Basic sonar codes

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04


#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // Serial Communication is starting with 9600 of baud rate
speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial
Monitor
Serial.println("with Arduino UNO R3");
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and
back)
// Displays the distance on the Serial Monitor, Look at video no: 17 and it’s theory
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}

Page 03

You might also like