// HC-SR04 (or similar) UltraSonic Sensor interface to Arduino // Written by Bradley S. Worthington-White // HC-SR04 Module Information /* * When triggered it transmits an 8 cycle burst at 40kHz and raises the echo pin HIGH. When the reflection (echo) of that burst * is received by the module the echo pin is brought LOW. The distance can now be calculated based on the length of time the * echo pin was HIGH. * * HC-SR04 pins; * Vcc = Voltage In (5v normally, 4.5v min - 5.5v max) * Trig = Trigger (minimum to trigger, 5v for at least 10us) Note: us = microsecond * Echo = Echo (the Echo pin is pulled HIGH when burst transmits, it is returned to LOW when a reflection from that burst is received) * Gnd = Ground (return to ground on power supply) */ // HC-SR04 and Arduino Setup /* * 1. Arduino 5v OUT to Vcc IN on HC-SR04 * 2. Arduino GND to GND on HC-SR04 * 3. Arduino digital pin 7 to Trig pin on HC-SR04 (you may use diffent pins, remember to change appropriate values in sketch if you do) * 4. Arduino digital pin 8 to Echo pin on HC-SR04 (you may use diffent pins, remember to change appropriate values in sketch if you do) */ // Arduino Sketch Begin /* * NOTE: You will need to open Arduino's serial monitor to see the results of the distances being measured by the HC-SR04 module * * The sketch begins by defining the constants/variables to be used within the "loop" section of the sketch below */ // Defining Constants (constants are things that will not change, you'll get a compile error if you try to change the value of a constant within the sketch) const int triggerPin = 7; // creates a constant named "triggerPin" and assigns digital pin 7 to it const int echoPin = 8; // creates a constant named "echoPin" and assigns digital pin 8 to it // Defining Variables (variables can and normally will change within a sketch, they are basically storage locations for values) int duration = 0; // creates a variable to store the value returned by echo, the value is being preset to "0" int distance = 0; // creates a variable to store the value calculated to be the distance to an object in front of the sensor,the value is being preset to "0" void setup() //Use this section to configure your board and other features as required by your program) { // beginning of setup Serial.begin(9600); // initialize serial communications via USB from Arduino to computer //defining pins and their use pinMode(triggerPin, OUTPUT); // "triggerPin" will be used for OUTPUT, the pin number is declared above under Defining Variables pinMode(echoPin, INPUT); // "echoPin" will be used for INPUT, the pin pin number is declared above under Defining Variables } // end of setup // everything above this point is only read ONCE by a program - at Startup or Reset void loop() // this is the main section of a sketch and is read continually and repeated until power off or reset { //beginning of loop digitalWrite(triggerPin, HIGH); //starts the Ultrasonic wave out from the HC-SR04 module delay(5); // short pause for the cause - required for module to function correctly (you can lower the number, I run it at 1 in other sketches) digitalWrite(triggerPin, LOW); //stop the Ultrasonic wave out from the HC-SR04 module duration = pulseIn(echoPin, HIGH); //special function to determine the length of time the echo pin was HIGH for the last complete cycle of sound burts delay(10); // short pause for the cause, waits before next line of sketch is read. This delay is for stability, too short a delay or none and no worky distance = (duration/2) / 74; //transforming duration to distance (the value stored in "duration" is divided by 2, that result is then divided by 74) // Note: for centimeters replace 74 with 58 delay(500); // waiting before next line of sketch is read Serial.print(distance); //prints the value calculated to be the distance to the serial monitor Serial.println(" inches"); //adds the word "inches" after the distance value above and starts a new line on the serial monitor Serial.println(); //adds a blank line on serial monitor for readability } // End of Loop