Ardumotive Arduino Greek Playground
  • Home
    • About us
    • License
  • Arduino
    • Tutorials
    • Workshop
  • Raspberry Pi
  • DIY 3D Printer
  • News
    • Events >
      • Unboxing & Review
    • Blog
The 1st Arduino Playground in Greece - Open Source Hardware

Ultrasonic Sensor

Available Languages
Greek
English

Introduction 

Picture
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm (or 1” to 13 feet). It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.
In this tutorial you will learn how to use this sensor with the Arduino uno and print the distance from an object to the serial monitor.

What you will need - Hardware

For this tutorial you will need:
  • Arduino uno
  • Breadboard
  • HC-SR04 Ultrasonic Sensor
Picture

The Circuit

Picture
The connections are pretty easy, see the image above with the breadboard circuit schematic.

The code

Here's the code, 
  • ultrasonic.Ranging(CM) will return distance from an object to centimeters
  • ultrasonic.Ranging(INC) will return distance from an object to inches
  • ultrasonic.Timing() will return the time (ms) where the signal took to return from the object
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* How to use the HC-SR04 Ultrasonic Sensor with Arduino 
   Dev: Michalis Vasilakis // Date: 23/7/2015 // www.ardumotive.com */

//Libraries
#include "Ultrasonic.h"

//Define pins ultrasonic(trig,echo)
Ultrasonic ultrasonic(A0,A1);

//Variables
int distance;

void setup() {
  Serial.begin(9600);
}

void loop()
{
  distance = ultrasonic.Ranging(CM); //Use 'CM' for centimeters or 'INC' for inches
  //Print distance...
  Serial.print("Object found at: ");
  Serial.print(distance);
  Serial.println("cm");
  //every 1sec. 
  delay(1000);
}
​Download the code from here and open it with Arduino IDE. Inside you will also find additional libraries.
hcsr04ultrasonic_tutorial.zip
File Size: 1 kb
File Type: zip
Download File

Open the serial monitor from tools menu of Arduino IDE
Picture

Well done!

Picture
You have successfully completed one more Arduino "How to" tutorial and you learned how to use the HC-SR04 Ultrasonic Sensor with Arduino uno. I hope you liked this, let me know in the comments. 
Picture

Search Engine

Picture

Licence 

Picture

Help us to grow up!

Picture


Donate us
About us
License
Cookies policy

Visit the biggest Arduino Shop in Greece!

Picture
find us on dwrean.net
find us on Codebender
find us on Instructables
Developed and designed by Vasilakis Michalis Copyright © 2013 Ardumotive All Rights Reserved
All trademarks referenced herein are properties of their
Powered by Create your own unique website with customizable templates.
Design by DivTag Templates