Apps and platforms
Arduino IDE
Project description
Code
Parallax Ultrasonic Sensor
arduino
1//Interface a PING ultrasonic sensor which uses ultrasonic waves to measure distance 2//It consists of 3 pins: GND, 5V, and SIG 3//Datasheet available: https://www.parallax.com/sites/default/files/downloads/28015-PING-Sensor-Product-Guide-v2.0.pdf 4 5#include <LiquidCrystal.h> 6 7//variable declarations 8long duration; 9int distancecm; 10long distancein; 11const int RS = 2, EN = 3, D4 = 4, D5 = 5, D6 = 6, D7 = 7; 12 13LiquidCrystal lcd(RS,EN,D4,D5,D6,D7); //set Uno pins that are connected to LCD, 4-bit mode 14 15void setup() { 16 lcd.begin(16,2); //set 16 columns and 2 rows of 16x2 LCD 17 pinMode(8, OUTPUT); 18 digitalWrite(8, LOW); 19} 20 21void loop() { 22 //Trigger the sensor with a 2usec HIGH pulse 23 digitalWrite(8, HIGH); 24 delayMicroseconds(2); 25 digitalWrite(8, LOW); 26 27 pinMode(8, INPUT); //set pin as input to read return pulse from sensor 28 duration = pulseIn(8, HIGH); //obtain length of HIGH pulse in microseconds 29 30 //Ping sensor returns HIGH output pulse that gives the time required for the burst 31 //echo to return back to the sensor 32 33 //Speed of sound travels at 1130ft/sec (0.00113ft/usec) or 340m/sec (0.034cm/usec) 34 //Must divide by 2 since time received is time for sound wave to travel to and from object 35 36 //Calculate distance in inches and centimeters 37 distancecm = duration*0.034/2; 38 distancein = distancecm*0.3937; //1cm = 0.3937in 39 40 //display distance to LCD 41 lcd.clear(); 42 lcd.print("Distance: "); 43 lcd.print(distancecm); 44 lcd.print("cm"); 45 lcd.setCursor(10,1); 46 lcd.print(distancein); 47 lcd.print("in"); 48 delay(200); 49 pinMode(8, OUTPUT); //set pin 8 back to an output to trigger next distance measurement 50 digitalWrite(8, LOW); 51 52}
HC-SR04 Ultrasonic Sensor
arduino
1//Interface a HC-SR04 module which uses ultrasonic waves to measure distance 2//It consists of 4 pins: VCC, TRIG, ECHO, and GND 3 4#include <LiquidCrystal.h> 5 6//variable declarations 7long duration; 8int distancecm; 9long distancein; 10const int RS = 2, EN = 3, D4 = 4, D5 = 5, D6 = 6, D7 = 7; 11 12LiquidCrystal lcd(RS,EN,D4,D5,D6,D7); //set Uno pins that are connected to LCD, 4-bit mode 13 14void setup() { 15 lcd.begin(16,2); //set 16 columns and 2 rows of 16x2 LCD 16 pinMode(8, OUTPUT); //TRIG pin 17 pinMode(9, INPUT); //ECHO pin 18 digitalWrite(8, LOW); //set TRIG pin low 19} 20 21void loop() { 22 //Trigger the sensor with a 10usec HIGH pulse 23 digitalWrite(8, HIGH); 24 delayMicroseconds(10); 25 digitalWrite(8, LOW); 26 27 duration = pulseIn(9, HIGH); //obtain length of HIGH pulse in microseconds 28 29 //HIGH output pulse is returned that gives the time required for the burst 30 //echo to return back to the sensor 31 32 //Speed of sound travels at 1130ft/sec (0.00113ft/usec) or 340m/sec (0.034cm/usec) 33 //Must divide by 2 since time received is time for sound wave to travel to and from object 34 35 //Calculate distance in inches and centimeters 36 distancecm = duration*0.034/2; 37 distancein = distancecm*0.3937; //1cm = 0.3937in 38 39 //display distance to LCD 40 lcd.clear(); 41 lcd.print("Distance: "); 42 lcd.print(distancecm); 43 lcd.print("cm"); 44 lcd.setCursor(10,1); 45 lcd.print(distancein); 46 lcd.print("in"); 47 delay(200); 48 49}
Parallax Ultrasonic Sensor
arduino
1//Interface a PING ultrasonic sensor which uses ultrasonic waves to measure distance 2//It consists of 3 pins: GND, 5V, and SIG 3//Datasheet available: https://www.parallax.com/sites/default/files/downloads/28015-PING-Sensor-Product-Guide-v2.0.pdf 4 5#include <LiquidCrystal.h> 6 7//variable declarations 8long duration; 9int distancecm; 10long distancein; 11const int RS = 2, EN = 3, D4 = 4, D5 = 5, D6 = 6, D7 = 7; 12 13LiquidCrystal lcd(RS,EN,D4,D5,D6,D7); //set Uno pins that are connected to LCD, 4-bit mode 14 15void setup() { 16 lcd.begin(16,2); //set 16 columns and 2 rows of 16x2 LCD 17 pinMode(8, OUTPUT); 18 digitalWrite(8, LOW); 19} 20 21void loop() { 22 //Trigger the sensor with a 2usec HIGH pulse 23 digitalWrite(8, HIGH); 24 delayMicroseconds(2); 25 digitalWrite(8, LOW); 26 27 pinMode(8, INPUT); //set pin as input to read return pulse from sensor 28 duration = pulseIn(8, HIGH); //obtain length of HIGH pulse in microseconds 29 30 //Ping sensor returns HIGH output pulse that gives the time required for the burst 31 //echo to return back to the sensor 32 33 //Speed of sound travels at 1130ft/sec (0.00113ft/usec) or 340m/sec (0.034cm/usec) 34 //Must divide by 2 since time received is time for sound wave to travel to and from object 35 36 //Calculate distance in inches and centimeters 37 distancecm = duration*0.034/2; 38 distancein = distancecm*0.3937; //1cm = 0.3937in 39 40 //display distance to LCD 41 lcd.clear(); 42 lcd.print("Distance: "); 43 lcd.print(distancecm); 44 lcd.print("cm"); 45 lcd.setCursor(10,1); 46 lcd.print(distancein); 47 lcd.print("in"); 48 delay(200); 49 pinMode(8, OUTPUT); //set pin 8 back to an output to trigger next distance measurement 50 digitalWrite(8, LOW); 51 52}
Downloadable files
Final Schematic
Final Schematic
Final Schematic
Final Schematic
Comments
Only logged in users can leave comments
ejshea
0 Followers
•0 Projects
Table of contents
Intro
1
0