Devices & Components
Arduino Uno Rev3
Breadboard (generic)
Jumper wires (generic)
USB-A to B Cable
Ultrasonic Sensor - HC-SR04 (Generic)
Software & Tools
Arduino IDE
Project description
Code
Optimised Code for Arduino with Ultrasonic sensor
c_cpp
1/* 2 * Credits to all sources on Google, acting as reference to this code below. This code is not 3 * TechValer's own creation. TechValer has referred to different projects and websites to 4 * find an easy code for beginners to get started with Ultrasonic Sensor HC-SR04 and Arduino. 5 * TechValer does not claim this code to be its own. TechValer is greatly thankful for original 6 * creaters of this code and also all others who acted as reference. 7 */ 8 9/* 10 * About TechValer 11 * 12 * What comes to mind when you think of tech...hmm, we're sure you're thinking of iPhone, 13 * Alexa, Boston Dynamics robotic dog etc., at least that's what we would have thought of. 14 * Our point here is, when you look inside this tech...you'll find weird boards with 15 * components attached to it. This stuff is electronics and we at Techvaler deeply appreciate 16 * this piece of tech. As the name suggests, we are Tech Enthusiasts who know the Worth and 17 * Potential of these amazing tech stuff! So, check out our website techvaler.com 18 * and Youtube channel: "Techcafe" to find out more. 19 */ 20 21/* 22 * Note: Any model of Arduino can be used for this project. Just keep in mind the digital pins. 23 * The readings available in the serial monitor are almost accurate. 24 * Refer to the documentation for better understanding of this particular code and project. 25 */ 26 27/* 28 * Thanks to Drona Automations Pvt.Ltd for Sponsoring this project! 29 */ 30 31#define trigPin 6 // Connect trigpin to D6 on Arduino 32#define echoPin 5 // Connect echopin to D5 on Arduino 33 34// defines variables 35 36long duration; // It's a variable for the duration of sound wave travel 37int inchdistance; // It's a variable for measurement of distance in inches 38int cmdistance; // It's a variable for measurement of distance in centimeters 39 40void setup() { 41 pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT 42 pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT 43 Serial.begin(9600); // // For Serial Communication 44} 45 46void loop() { 47 48 digitalWrite(trigPin, LOW); // Clears the trigPin condition 49 delayMicroseconds(2); 50 digitalWrite(trigPin, HIGH); 51 delayMicroseconds(10); 52 digitalWrite(trigPin, LOW); 53 54 duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds 55 56 // Calculating the distance 57 58 inchdistance = duration * 0.0133858 / 2; // To calculate the distance in inch 59 cmdistance = duration * 0.034 / 2; // To calculate the distance in cm 60 61 // Displays the distance on the Serial Monitor 62 63 Serial.print("Distance: "); 64 Serial.print(inchdistance); 65 Serial.println(" inch"); 66 Serial.print(cmdistance); 67 Serial.println(" cm"); 68} 69
Downloadable files
Ultrasonic Sensor with Arduino Circuit Diagram
Ultrasonic Sensor with Arduino Circuit Diagram

Ultrasonic Sensor with Arduino Circuit Diagram
Ultrasonic Sensor with Arduino Circuit Diagram

Comments
Only logged in users can leave comments