Arduino

Ultra-Sonic Rangefinder Kit

Contents
  • 25 resistors (5 of each: 100R, 220R, 330R, 1K, 10K)
  • 15 LEDs (5 of each: Red, Yellow, Green)
  • 1N4148 small signal diode
  • 10K miniature preset potentiometer
  • 5 miniature "tact" switches (2-pin, 6mm square)
  • 830 tie-point breadboard
  • Miniature speaker (12mm diameter, 40 ohm)
  • 9V PP3 battery connector for Arduino
  • 65 male-male jumper wires
  • 16 chars by 2 line LCD module with blue backlight
  • HC-SR04 Ultrasonic distance sensor
HC-SR04
4 pins - can plug directly into breadboard
5V, 0V, Ping and Echo
Send pulse on Ping pin from Arduino digital output
Measure width of pulse on the Echo pin with Arduino digital input
LCD Module 1602
Solder the 16-pin header (provided) to the module
Use a 100Ω resistor to connect the backlight
Use a 10K potentiometer (or 1K and 10K resistors as shown below) for contrast
Only 4 data pins required in 4 bit mode
Click for larger image
HC-SR04 Wiring
This circuit uses a 16x2 LCD to display the distance to the nearest object
  • Gnd to 0V
  • Vcc to 5V
  • Ping/Trig to Ardino pin 7
  • Echo to Arduino pin 6
Arduino Source Code
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pingPin = 7; //output 10us pulse to this pin
int inPin = 6; //measure return pulse width from this pin
long duration, inches, cm;
int indec, cmdec;
int inchconv = 147; // ratio between puls width and inches
int cmconv = 59; // ratio between pulse width and cm
String s1, s2;

// initialise LCD library and pins
void setup() {
  lcd.begin(16, 2);
  pinMode(pingPin, OUTPUT);
  pinMode(inPin, INPUT);
}

void loop()
{
  // Send a short LOW followed by HIGH pulse to Trigger input:
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);

  // read the length of the return pulse on Echo output
  duration = pulseIn(inPin, HIGH);

  // convert the time into a distance (non-floating point with decimals
  inches = microsecondsToInches(duration);
  indec = (duration - inches * inchconv) * 10 / inchconv;
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  s1 = String(inches) + "." + String(indec) + "in" + "     ";
  s2 = String(cm) + "." + String(cmdec) + "cm" + "     ";
  lcd.setCursor(0, 0); // print inches on top line of LCD
  lcd.print(s1);
  lcd.setCursor(0,1); // print cm on second line of LCD
  lcd.print(s2);

  delay(100);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}

Close-up of LCD Wiring
  1. 0V
  2. 5V
  3. Contrast:  1k to Ground and 10k to 5V
  4. RS:  to Arduino pin12
  5. R/W:  to 0V
  6. E:  to Arduino pin11
  7. DB0:  n/c
  8. DB1:  n/c
  9. DB2:  n/c
  10. DB3:  n/c
  11. DB4:  to Arduino pin 5
  12. DB5:  to Arduino pin 4
  13. DB6:  to Arduino pin 3
  14. DB7:  to Arduino pin 2
  15. Anode:  to 100Ω or 220Ω resistor; other end of resistor to 5V
  16. Cathode:  to 0V