Are you a starter with Croduino? Or a newbie when it comes to electronics? You fancy a specific module, but you don't know how to use it? Don't worry, we have our How To Use series!
How to Use is a series of blog tutorials by e-radionica where you will find everything you need to begin working with your favorite modules. Tutorials include technical characteristics, operating principles, instructions on how to connect the module with Croduino and basic coding. Everything else is up to you and your imagination.
Basic features
The HC-SR04 ultrasonic module uses ultrasonic waves to determine the distance of objects, just like a dolphin or a bat. Not only that it surpasses its competition with its simplicity and cost, it's surpassing it with its technical capabilities, too. It doesn't get confused by sunlight or black materials, such as the most famous competitor Sharp IR.
Characteristics:
Range: 2-200 cm
Accuracy: 3 mm
Effective angle of measurement: 15 °
Voltage: 5V
The maximum quiescent current of 2 mA
Operating current: 15 mA
Ultrasonic frequency: 40 kHz
Dimensions: 45 x 20 x 15 mm
Note: Official datasheet gives more impressive technical characteristics of the module, for example, angle measurement of 30 degrees or a range of up to 7 meters. The above data is provided with our own tests and work on projects, and we can guarantee for that data.
Working principle
Two main parts to the underlying principle of operation of the module are the trig (switch) and echo (reflections). Microcontroller (Croduino or Arduino) is used to send 5V on trig pin module in a duration of minimum 10 microseconds. That way we are activating the ultrasonic transducer which transmits 8 pulses of 40 kHz and waits for their reflection. When the sensor detects the reflected pulse, it sends the information back to the microcontroller via the echo pin. These data are in fact the duration of the reflected pulse, 150 micro to 25 milliseconds. If the "echo" lasts longer than 35 milliseconds, the sensor detects that the subject is out of reach.
In this tutorial, we will use:
Croduino Basic
Ultrasonic Module HC-SR04
Mini experimental PCB
M-M cables
How to connect the module with Croduino
We combine ultrasonic module pins with Croduino (Arduino) like this:
Vcc - +5V
Trig - digital PIN11
Echo - digital PIN12
Gnd - gnd
Module code
At the link below you can see a very simple code that prints distance of the object from the sensor in Serial monitor every 50 milliseconds, shown in centimeters.
/////////////////////////////////////////////////////////////////////////////// /* */ /* */ /* (c) e-radionica.com 2014 - http://www.e-radionica.com/licence */ /* e.radionica@me.com */ /* */ /* Measuring distances using HC-SR04 ultrasonic module */ /* */ /////////////////////////////////////////////////////////////////////////////// const int trig = 11; // Trig on PIN11 const int echo = 12; // Echo on PIN12 void setup() { pinMode(trig, OUTPUT); pinMode(echo, INPUT); Serial.begin(9600); // starting serial communication Serial.println("Measuring distances using HC-SR04 ultrasonic module"); Serial.println(""); } void loop() { // since we do not use the library, below is a written method of communication with the module long time, distance; digitalWrite(trig, HIGH); // Trig sending delayMicroseconds(10); // Duration of sent trig 10 μs = 0.1 ms digitalWrite(trig, LOW); time = pulseIn(echo, HIGH); // Receiving reflected trig distance = (vrijeme/2) / 28; // Calculating the distance, in centimeters // final distance from sensors to items // the variable distance is in cm Serial.print(distance); Serial.println("cm"); delay(50); // recommended pause between readings shouldn't be less than 50ms }