Test of the ultrasonic sensor HC-SR04

Simple test to check the distance sensor with an Arduino and JavaScript code.

This component sends ultrasounds and determines the distance of an object from the time it takes for the signal to return to the starting point, once reflected on this item. Like a bat but silent! It is very useful to automate his house. For example, it can trigger the lighting in a room when the sensor detects that one goes through the door, then turn it off when one goes back the other way... This is just a sample application from hundreds ... And of course, its interest is obvious to prevent a robot from bumping against objects...

For the test, you will place the transmitter facing nothing, and if you are in a room, it will display the distance from the wall in front of it... An easy way to calculate the dimensions of a room! Then you put an object near the sensor and the program will display in the console the distance between the object and the assembly in centimeters (you can also use inches).

Parts

Assembly

The HC-SR04 component is plugged directly on the breadboard.

It has 4 connectors:

The distance is determined by a formula that calculates the time between sending the signal and the return of the reflected signal on an object multiplied by the speed and divided by 2. But when you program in JavaScript, all this is handled by the Johnny-Five framework, which just indicates the distance in centimeters (or inches).

Capteur de distance

It is not very easy to see in the diagram, but the Trig and Echo pins are connected together by a jumper.
This assembly is special for JavaScript and JF, if we had used C code instead, we would have connected the two pins on two pins on the Arduino.

Photo of the assembly

Test du capteur de distance hc-sr04

The JavaScript code

The code does not work directly with the Standard Firmata driver, you have to patch it to add an instruction, and thus replace the driver that you already have loaded into your Arduino with this version. This code comes from the Johnny-Five wiki.

Replace Standard Firmata code in the Arduino IDE by this code and update the microcontroller as explained in the introduction page.

Install the JavaScript library on your computer:

npm install johnny-five 

Then you can run this program:

let five = require("johnny-five");
let board = new five.Board();

board.on("ready", function() {
  var ping = new five.Ping(4);     // digital pin 4
  ping.on("change", function(err,val) {
    console.log("Distance " + this.cm + " cm");
  });
});

Replace this.cm by this.inches for the distance in inches.

You can then place your hand at different distances in front of the sensor to check the operation.

Troubleshooting

Nothing is displayed

Have you updated the Firmata driver? This can not function without it.

The program displays fanciful values

This is surely a wiring problem. Check that the wires are connected to the right pins. Try also another digital port, the current installation uses port 4, replace for example 4 by 8 in the code and connect the signal wire to the digital pin 8.