Go Down

Topic: Ultrasonic Sensor HC-SR04 controlling (Read 4369 times) previous topic - next topic


Im a total noob on Arduino staff so any suggestions for improvement are very welcome.
I have created the follwing scetch that displays and evaluates data derived from an ultrasonic Sensor HC-SR04. I have also attached a fritizing drawing. I wish this might help someone.


  Aronis Projects
  This Project Uses Arduino, an Ultrasonic (HC-SR04) sensor, a servo motor
  and a LCD 16x02.
  Data from the HC-SR04 sensor is translated via ultrasonic library and displayed on the LCD (again using the LCD library).
 If the distance from target is equal or below 3 cm then the servo motor moves 180 degrees. When the distance gets greater than
3 cm then the motor moves to its starting position. 
  The circuit:
 * LCD RS pin to digital pin 2
 * LCD Enable pin to digital pin 3
 * LCD D4 pin to digital pin 4
 * LCD D5 pin to digital pin 5
 * LCD D6 pin to digital pin 6
 * LCD D7 pin to digital pin 7
 * LCD R/W pin to ground
 * 10K variable resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * Servo motor signal terminal connects to arduino digital pin 11.
 * HC-SR04 Trigger pin hooks to digital pin 12
 * HC-SR04 Echo pin to digital pin 13

// include the library code:
#include <LiquidCrystal.h>
// include the ultrasonic library
#include "Ultrasonic.h"
// ultrasonic trig pin at digital pin 12 and echo pin at digital 13
Ultrasonic ultrasonic(12,13);
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

// include the servo library
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int pos = 0;  // define position as integer
int change = 0; // variable to avoid constant use of the servo

void setup() {
   myservo.attach(11);  // attaches the servo on pin 11 to the servo object
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("ARONIS PROJECTS!");
  lcd.setCursor(0,1); // Go to the second line
  delay(3000); // wait 3 secs
   lcd.clear(); // Clear screen
   // Print standard message
      lcd.setCursor(0, 0);
      lcd.print("Distance from");

void loop() {
  pos = ultrasonic.Ranging(CM); // Get the distance from sensor
      lcd.print("cm   ");
     if (change == 0 && pos<= 3) {
        change = 1;
  if (change == 1 && pos> 3) {
  change = 0;


Feb 07, 2012, 01:03 pm Last Edit: Feb 07, 2012, 01:14 pm by pYro_65 Reason: 1
Looks like a nice example.
I would recommend:

Change the 'change' type to a boolean it more closely reflects what its being used for.
Also you use 'pos' to store the sensor value used in calculations. You should re-use 'pos' to send to the display also as re-reading the sensor can produce odd results ( screen says <= 3 but pos says 4 ).


Also I think the library ( ultrasonic ) may return floats ( decimal numbers ).
By using an int the decimal places are dropped off. ( 1.99 is not rounded up to 2 )
Then again, it may only return int values.


When you have success in using HC-SR04 could post if possible how far away you could get.
I have one that is only 1.5 meters and the manual should be 4 meters.
thank you

I use this program in a Arduino 2560

  HC-SR04 Ping distance sensor]
  VCC to arduino 5v GND to arduino GND
  echo pin 22 trigger pin 24

int echoPin = 22;
int trigPin = 24;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

void loop() {
  unsigned long duration, cm;
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  cm = duration / 29 / 2;
/* Sound velocity=29cm/microsec,
    then divide by 2 because of the return time */
  Serial.println(" cm");


I managed 3 metres with mine, above that was erratic. The humidity level and other variables may have an effect on its maximum distance.


Have you ever used the maxbotix?
The circuit is so simple to do everthing that promise.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131