HC-SR04 ultrassonic sensor on Raspberry PI + blynk


I converted this library from python ( original here, from Matt Hawkins at Instructables )

Now i can use it at blink.read(virutalpin), and set Value Display to read it in cm:

I used the WiringPi libraries, and included the reference to systime library ( #include <sys/time.h> ).

All variables are inside the code, but you are free to set it on whatever you desire.

// Define GPIO to use on Pi ( wiringPI numbers )
int GPIO_ECHO=5 ;

//Compensate distance from edge to sensor

//Variables used by gettimeofday
struct timeval start, stop ; 

// Set pins as output and input
wiringPiSetup () ;
  pinMode (GPIO_ECHO, INPUT) ;

//Set trigger to False (Low)
digitalWrite (GPIO_TRIGGER,  0) ;

//200 ms delay to start 
delay (200) ;

//Send 10us pulse to trigger
digitalWrite (GPIO_TRIGGER,  1) ;
delayMicroseconds (10) ;
digitalWrite (GPIO_TRIGGER,  0) ;
gettimeofday(&start, NULL) ;
while (digitalRead (GPIO_ECHO)==0) 
gettimeofday(&start, NULL) ;
while (digitalRead (GPIO_ECHO)==1) 
gettimeofday(&stop, NULL) ;

// Calculate pulse length
TIME_DELTA=(stop.tv_sec-start.tv_sec) + (stop.tv_usec-start.tv_usec) ;

// Distance pulse travelled in that time is time multiplied by the speed of sound (cm/s)

// That was the distance there and back so halve the value

// Add compensation

// This command writes result to Virtual Pin (10) and convert usec to sec 
Blynk.virtualWrite(10, DISTANCE  / 1000000) ;


Great! Thanks for sharing. Could you please add video or photo of your set up? That would be awesome!

Hi Pavel,

Yes, there are some pics of my test setup. I tried to post directly here, but pictures upload is blocked for new users, like me.

Actually i am building a water level sensor with this ones. When i finished, i will post some pics too.

- 5 cm distance test

- 20 cm distance test

Interesting . I’ve changed your level. Could you please check if you can post images?

How’s accuracy with these sensors? I used them only once and was very disappointed.


Yes, now i can upload the pics.

I made 4 tests, with 5, 10, 20 and 30 cm measured with a ruler, and got a good precision ( +/- 1cm ).

I think with some compensation, these sensors can achieve a 0.5 cm precision. To my needs is sufficient. But it was tested on short ranges only. Maybe the precision can vary with long range tests.

I uploaded the pics of each test and a close up from ruler and value reported on blynk app.

5 CM Test

10 CM Test

20 CM Test

30 CM Test


Interesting… For a tank with a more or less flat surface or for something with waves? Is there some kind of averaging needed?

Thanks for the pics!

Do you use a timer in your sketch? Calling virtualWrite directly in the loop may cause flood error and your connection will be terminated by server. Please check the PushData example in the library. Read the instructions inside carefully.


Hi Minc,

Yes, i using on water reservoir with low movement, basically a flat surface. To use on liquid with some waves, you can use a solution propose by original instrutables, putting the sensor inside a pvc tube, who have both ends open, measuring the average liquid without waves.

The code is good for 1 measure per sec, but can be optimized to 10 per second easily, and increase the precision by using average. In this case, use a Local blynk server, to avoid flooding and get blocked.

Hi Pavel,

At this time, when i am refining the hardware, i using default 1 sec get of Value display to avoid flood. But its a good point. I will put a timer on my next deployments.

In “production” version, i will use local blynk server on raspberry to avoid flooding.

And thank you for your help. I am a newbie on this and any help by pointing me on right direction can be great.

1 Like

Since you made some research on this. I’m thinking about building a Smart Bottle, which will count how many liquid I drink during the day and send it to Blynk. Do you think that ultrasonic sensor would be a good solution for it?

Are you kidding? If not… I’d rather go with a connected scale for this purpose.

They all mention ultra-sonic sensors:

Nope, i am not kidding. I like the pvc tube solution, because it can avoid a lot of problems when you need to measure a liquid that have inconstant flows in/out, who can causes undesirable waves, and make measures looks crazy.

Ultrasonic sensors can reflect on a single drop, causing wild reads…

I use ultrasonic park system on my car, and when under heavy rain, it render near useless…


Yes, you can adjust the pulse ( 10 usec ) to a value that offers better results on each environment ( a bottle, in this case ).

I think with 0.5 cm precision and a 500 ml water bottle , you can achieve a near 10 ml precision, who looks good to me.

In the calc, i used a cylinder with 7 cm of base (diameter), and a height of 20 cm, to go up to 516 ml of liquid.

Each 0.5 cm in height represent a 11.7 ml of liquid.

1 Like

Sounds very good, thanks! 20ml accuracy is more than enough.

Sorry, was not clear enough. Using the tube makes perfectly sense to me. I was referring to the bottle Pavel mentioned.

@Pavel Didn’t know about these projects. Is the US sensor water resistant? How about drops on top of the sensor? Just could not imagine this with the standard sensors you get for arduino. I’ll have a look at the videos… And keep us posted with you owe projects using blynk. :smile:

It seems that you can wrap it in a thin plastic film and it should still work…


Sorry Minc, my mistake too…


In this case, i think these sensors can be better:


They appear to be smaller enough to put on base or even in top.

Wow! Thank you! I think this is something I was looking for.