Ultrasonic Sensor(Distance Sensor) with Arduino
Distance Sensor with Arduino
Components and supplies
1
Arduino UNO
1
Breadboard (generic)
1
Ultrasonic Sensor - HC-SR04 (Generic)
1
Jumper wires (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Ultrasonic Sensor
c_cpp
Ultrasonic Sensor with Arduino
1//...............JehanKandy........................ 2//........www.jehankandy.com....................... 3//........www.github.com/JehanKandy................ 4 5//....................Distance Sensor.................... 6//.....................Ultra Sonic..................... 7 8 9//create Variables..... 10int trig = 6; //Trig Pin as OUTPUT 11int echo = 5; //Echo Pin as INPUT 12long duration, distance; 13 14void setup() { 15 //Identify inputs, outputs and Serial Monitor 16 pinMode(trig,OUTPUT); //OUTPUT 17 pinMode(echo,INPUT); //INPUT 18 Serial.begin(9600); //for open the Serial Monitor, 966 19} 20 21void loop() { 22 //send the original Signal -> trig signal 23 digitalWrite(trig,HIGH); 24 delayMicroseconds(2); 25 digitalWrite(trig,LOW); 26 delayMicroseconds(10); 27 digitalWrite(trig,HIGH); 28 29 //Receive the Signal -> echo signal 30 31 duration = pulseIn(echo,HIGH); 32 distance = (duration/2)*0.0346; // to create 'cm' value 33 34 Serial.println(distance); 35 Serial.print("cm"); 36} 37 38 39//............coded by : JehanKandy.................... 40//................Thank You............................ 41
Ultrasonic Sensor
c_cpp
Ultrasonic Sensor with Arduino
1//...............JehanKandy........................ 2//........www.jehankandy.com....................... 3//........www.github.com/JehanKandy................ 4 5//....................Distance 6 Sensor.................... 7//.....................Ultra Sonic..................... 8 9 10//create 11 Variables..... 12int trig = 6; //Trig Pin as OUTPUT 13int echo = 5; //Echo Pin 14 as INPUT 15long duration, distance; 16 17void setup() { 18 //Identify inputs, 19 outputs and Serial Monitor 20 pinMode(trig,OUTPUT); //OUTPUT 21 pinMode(echo,INPUT); 22 //INPUT 23 Serial.begin(9600); //for open the Serial Monitor, 966 24} 25 26void 27 loop() { 28 //send the original Signal -> trig signal 29 digitalWrite(trig,HIGH); 30 31 delayMicroseconds(2); 32 digitalWrite(trig,LOW); 33 delayMicroseconds(10); 34 35 digitalWrite(trig,HIGH); 36 37 //Receive the Signal -> echo signal 38 39 40 duration = pulseIn(echo,HIGH); 41 distance = (duration/2)*0.0346; // to create 42 'cm' value 43 44 Serial.println(distance); 45 Serial.print("cm"); 46} 47 48 49//............coded 50 by : JehanKandy.................... 51//................Thank You............................ 52
Ultrasonic Sensor
c_cpp
Ultrasonic Sensor with Arduino
1//...............JehanKandy........................ 2//........www.jehankandy.com....................... 3//........www.github.com/JehanKandy................ 4 5//....................Distance Sensor.................... 6//.....................Ultra Sonic..................... 7 8 9//create Variables..... 10int trig = 6; //Trig Pin as OUTPUT 11int echo = 5; //Echo Pin as INPUT 12long duration, distance; 13 14void setup() { 15 //Identify inputs, outputs and Serial Monitor 16 pinMode(trig,OUTPUT); //OUTPUT 17 pinMode(echo,INPUT); //INPUT 18 Serial.begin(9600); //for open the Serial Monitor, 966 19} 20 21void loop() { 22 //send the original Signal -> trig signal 23 digitalWrite(trig,HIGH); 24 delayMicroseconds(2); 25 digitalWrite(trig,LOW); 26 delayMicroseconds(10); 27 digitalWrite(trig,HIGH); 28 29 //Receive the Signal -> echo signal 30 31 duration = pulseIn(echo,HIGH); 32 distance = (duration/2)*0.0346; // to create 'cm' value 33 34 Serial.println(distance); 35 Serial.print("cm"); 36} 37 38 39//............coded by : JehanKandy.................... 40//................Thank You............................ 41
Comments
Only logged in users can leave comments