IAN LANG ELECTRONICS

I don't know why I haven't got round to doing one of these before now to be honest with you as they are much easier than mucking about with infra-red and quite a lot cheaper too.

 

I'm sure you'll have seen one of these before. They're popular with robot builders and pop up everywhere, and the one I got is an HC SR04 from Hobby Components for the exceptionally extortionate sum of £1.99 (really 1.99! Quick, buy a dozen!). That's the bunny on the right there and if you click on the picture it takes you to their webpage for it. Whilst you're there poke around in the other stuff too, because I'm genuinely impressed with this bunch. They're based in an old ironworks very near to where I grew up and even nearer to the place I did my apprenticeship- that clocktower used to be a daily feature as I biked to work.

Go Back

A Low Cost Distance Finder

This device works on sound. The normal speed of sound in air is given as:

343.2 m/s

However that is in ideal conditions. We're back to that spherical chickens in a vacuum thing if we take that as the absolute. In the standard atmosphere (1 bar) at zero degrees centigrade it's 331.3 m/s and at temp 20 degrees it's

343.2 m/s.

This is of course an embuggerance; it means we have to measure atmospheric pressure and temperature to get a fully accurate reading and that bumps up the expense of a device, defying the point of being low cost. So what we do when we need a precision in cm rather than mm is take a standard to measure against. So, for our purposes, we say:

340 m/s

hcsr04

Having said that I don't personally know the people running it but they look as though they've opened a company for makers that's run by makers as they appear to know just what they're doing and consequently they are going to get a lot more of my pounds in future as that's a rare thing these days.

Apart from my nostalgic wallow there, if you don't live in the UK you can buy these things from a number of companies; Google HC SR04 and see how many hits you get.

 

Right then. What are we going to do with this thing?  We're going to attach it to an Arduino, slap said Arduino with some code and make a ruler. Here's a connection diagram:

wiringhcsr04

 

 

// distance measure routine using HC SR04 ultrasound module

// Ian Lang Electronics

//http://ianlangelectronic.webeden.co.uk/

int Trigger = 3; //  pin 3 to Trigger on module

int Echoback = 4; // pin 4 to Echo on module

void setup() {

Serial.begin(9600);// Start serial

pinMode(Trigger, OUTPUT);//set trigger pin to deliver a pulse

}

 

void loop()

{

long travelTime, cmValue;

 

digitalWrite(Trigger, LOW);// clean any highs

delayMicroseconds(2);// wait

digitalWrite(Trigger, HIGH);//send signal to start pulse forward

delayMicroseconds(5);//wait

digitalWrite(Trigger, LOW);//stop signal

travelTime = pulseIn(Echoback, HIGH);//time pulse back

cmValue = travelTime / 29 / 2;//convert pulse back into a distance

// The assumption here is that sound is travelling at 1cm per 29 uS (340 m/s).

Serial.print("Distance = ");

Serial.print(cmValue);

Serial.print(" cm   ");

Serial.println();

 

delay(100);

}

 

HCSR04

Having connected up your wires as above you'll need some code with which to slap your Arduino. Here it comes, either copy and paste or download as a text file by clicking the link button on the left marked HCSR04:

Those of you that have trawled through some of the other articles on this site will know that we seldom leave it at that. What we like to do here at Lang Towers is go through it bit by bit and bang on at length using twenty-eight words where one would have done and occasionally have a rant about things. There's no reason for this one to be any different and so here we go. Let's begin with the setup:

 

void setup() {

Serial.begin(9600);// Start serial

 

So, all that line above does is to set the Arduino up to talk to the serial monitor at a baud rate of 9600. Plenty fast enough for this game. Next comes:

 

pinMode(Trigger, OUTPUT);//set trigger pin to deliver a pulse

}

 

The trigger of the module needs to get a 5V blast for a few microseconds to send out the sound pulse. To do it, we need to set the pin to output mode. If not, we'll get a weak output that may not be seen by the trigger. That's all we do in setup, here comes the loop part:

 

void loop()

{

long travelTime, cmValue;

 

In that line above, all we've done is set up two variables of a long type. The first, travelTime, is going to measure time in microseconds between the trigger of the pulse and the time the detector receives the back pulse. The second, cmValue, is going to be used to covert that time into a distance.

The next line is where we start the pulse going. It reads:

 

 

digitalWrite(Trigger, LOW);// clean any highs

 

We want the trigger pin to be at 0 volts before we light it up. Chances are it will be, but if there is a floating voltage on there, this line will clear it. Next we say:

 

delayMicroseconds(2);// wait

 

Two microseconds allows ample time for the trigger pin to go low and not enough for a floating voltage to establish and so we can be sure that the pin is at ground. Which is good, because now we use three lines to send a pulse:

 

digitalWrite(Trigger, HIGH);//send signal to start pulse forward

delayMicroseconds(5);//wait

digitalWrite(Trigger, LOW);//stop signal

 

Simply, we send a sound pulse by switching the trigger on, waiting  uS and switching it off again. Having sent it we need to detect it coming back:

 

travelTime = pulseIn(Echoback, HIGH);//time pulse back

 

What that line does is to set the echo receiver pin low and wait for a high signal to hit it. Once it does, we measure the time from the set to the reception and from that we can calculate the distance. Now here's where it gets a bit mathsy. If we say sound is 340m/s  it's the same as 34,000 cm/s. in a microsecond then, the sound wave goes 0.034 cm/s and to get about 1cm it needs 30 uS. That will take us slightly over a centimetre and we need to account for a delay in sending the pulse and switching the pulseIn function on (microseconds pass very quickly). Trial and error suggests that twenty-nine microseconds makes a better marker. But that's the total path time and we just want the path back. Since the path back must equal the path forward, we want half the time, which is the same as time /  2 and so in code we say:

 

cmValue = travelTime / 29 / 2;//convert pulse back into a distance

// The assumption here is that sound is travelling at 1cm per 29 uS (340 m/s).

 

Then it's just a matter of printing the data out in the serial monitor:

 

Serial.print("Distance = ");

Serial.print(cmValue);

Serial.print(" cm   ");

Serial.println();

 

and waiting before we send the next pulse:

 

delay(100);

}

 

If you try this, test it by stretching a tape measure in front of the module and placing a piece of card at various points along the tape. You should find that the monitor will show the nearest whole centimetre away; for instance 10.4 cm will be 10cm and 20.7 cm will be 21 cm. Keep the target as straight and parallel as you can; if you angle it you'll get funny readings. The device works down to about 3cm and after that it picks up ringback and becomes unpredictable. I haven't tested full range but it should do at least 1m and looking, probably it might go up to 3m accurately. More than enough for robot building, which is what this thing is predominantly used for.

 

Let's do something practical with it. How about an alarm system?

 

 

 

 

 

More>