Arduino Ultrasonic Ranging With HC-SR04

HC-SR04 not only inexpensive but also practical, use a sample AVR development boar –Arduino , with a LCD screen , you can make a easy rangefinder device , which can ranging 2cm to 500cm .

HC-SR04 not only inexpensive but also practical, use a sample AVR development boar –Arduino , with a LCD screen , you can make a easy rangefinder device , which can ranging 2cm to 500cm .

Now first connect the circuit as below : With module : pin13 to Trig ; pin 12 to Echo ; VCC to VCC; GND to GND ; pin 2 to INT With LCD: RS => pin 11 RW => pin 10 EN => pin 9 D4, D5, D6, D7 => pins 7, 6, 5, 4

Download the code below :

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); int pingPin = 13; int inPin = 12;

void setup() { lcd.begin(16, 2); lcd.print("testing..."); }

void loop() {

long duration, inches, cm; pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW);

pinMode(inPin, INPUT); duration = pulseIn(inPin, HIGH);

inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); lcd.clear(); lcd.setCursor(0, 0); lcd.print(inches); lcd.print("in, "); lcd.print(cm); lcd.print("cm");

delay(100); }

long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2; }

long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }

Reset the Arduino , then you can see the distance of object in front on the LCD.

By itead
Created on April 16, 2010, 18:44

Category: Analog / Digital

Difficulty: amateurs

License:  Creative Commons Attribution-Share Alike 3.0 Unported License

Tags: ultrasonic ranging, arduino

Images

Files

Fritzing Files

Code Files

Links

Comments

  1. tommy898 # Aug. 30, 2010, 12:01 p.m.

    How to use two Ultrasonic Sensor in just one arduino duemilanove!?
  2. coromonadalix # March 14, 2011, 4:55 a.m.

    hi i did a rework of the code, i wasn't able to make it work, i dont understand the LiquidCrystal lcd(8, 9, 4, 5, 6, 7) line where's the pin 8 goes ??? and i have an rs232 output at he same time. // this is a reworked pinout and code, the original code wasn't working ?? // with rs232 output while reading it on a lcd // there's is a glitch in the 30 to 40 cm distance, sometimes it display's 4 digits ??? // was reported on a few web sites ??? #include <LiquidCrystal.h> #include <Ultrasonic.h> // initialize the library with the numbers of the interface pins, R/W tied to ground LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //tried HC-SR04 sensor on pins # (6,7, 7,8 8,9 9,10) 1,2 don't work ??? Ultrasonic ultrasonic(9,10); void setup() { Serial.begin( 9600 ); // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("Ultrasonic Range"); } void loop() { Serial.print(ultrasonic.Ranging(CM)); Serial.println("cm"); //refresh rate in seconds, in a rs232 terminal windows delay(2000); lcd.clear(); // set the cursor to column 0, line 0 // start at top/left position lcd.setCursor(0, 0); lcd.print(ultrasonic.Ranging(CM)); // print the value in cm lcd.print("cm"); delay(100); }

Login to post a comment...
  • Print this
© 2007 - 2011 University of Applied Sciences Potsdam