You are here

Non-invasive measurement of distances with Arduino Yun [Part 3]



The following illustrates alternative method of calculating the distance within the Arduino yun sketch for some ultrasonic sensors as hc-SR04 :




The method of calculation is similar to the calculation of the distance from themselves to thunder after seeing the lightning and hearing the thunder later.


Calculate the speed of sound in m/s. In formula of variable “v”, the value “25” is environment temperature. Follow this link :

The sound is a vibration through the air and sound travels faster with increasing temperature. So the calculation of the speed of sound is very different, for example if calculated in Norway or Congo. In dettail, the vibration and soundwave propagation would also depend on the air pressure, but this aspect was considered negligible. However, in some places of high mountain would be appropriate to consider a light sensor calibration because of this variable (air pressure/humidity):

Theformula in m/s is as follows ==> v= 331.3+0,606*T.

At 20°C thespeed is 343m/s and therefore moreof 1200Km/h.

float v = 331.5+0.6*25;

int pingPin=3;

Send thepulse sound. We uses the same pin for input and output (3). It initially sets the pin to LOW and then wait three microseconds to be sure that the value isset. Then we send a high signal (beep) for 5 micro seconds.

pinMode(pingPin, OUTPUT);






It calculates how much time has elapsed until the return of the pulse so that thesignal is returned low (pulseIn function):

pinMode(pingPin, INPUT);

float t1=pulseIn(pingPin, HIGH);

Transformationin seconds, and division by two, since the trip includes round trip.


Assuming a constant speed, the distance is time multiplied by the speed. It also multiplies by 100 in order to have a measure in cm.

float d = t2*v *100;

After, you can send with YunClient result onbrowser via json (key variable defined at will by the developer):

client.println(“Status: 200”);

client.println(“Content-type: application/json;charset=utf-8”);



Please remember to put at the end of the loop()function at least a delay of 200 ms in order to avoid overloading the powersupply and CPU processor.

I have over ten years experience in project management and software architect in complex and innovative projects for multinational ICT. I have a electronic engineer extraction with automatic specialization. For years I deal with innovation also as an active contributor to several open hardware community as Rfduino, Arduino Yun, etc ... I think that in every company there is a need to co-create ideas and innovation from every person with tools and working groups motivated and sharing and dissemination of culture at all levels. Technology and research are not enough, if there is no interaction and sharing there can be no innovation.

Leave a Reply