Usage of HC-SR04 ultrasonic sensor

Hi everyone,

The idea is I want to use the HC-SR04 ultrasonic sensors to determine the direction my robotic car is going. Two sensors on the car on the front right and left, only use the microphone; and one sensor attached to a battery pack carrying by me, only use the generator. I want the sensor I carry to just keep generating signal so the two sensors on car can determine my location by the difference of the distances two sensors return.

In this case can I apply constant 5v to the trig pin so it can keep generating signal? if not, how can I do that without attaching to a microcontroller?

Thank you.

Interesting project. Unfortunately a constant high signal will not work to regularly trigger the sensor that is generating the signal.

Instead, you need a signal of 20 Hz or less. There are a few ways of doing this. My approach would be to use a microcontroller. If space is an issue, I would use the Pro Micro. There’s also the PICAXE-08M2.

Another way would be to use a 555 timer, but we do not have any in the RobotShop catalogue. Yet another could be to use an inexpensive wireless link so you can produce the signal with the microcontroller on your robot and receive the signal in the device that you’re carrying.

These solutions however are more complex than a microcontroller approach. What is the reason you want to do this without using one?

Hi jarcand,

Thank you so much for your reply, the reason why I don’t want to attach a microcontroller to it is because I want to keep it as small as possible so I can just put it in my pocket or something.

I’ve uploaded a pic of my transmitter device at the moment, I soldered the positive lead of the battery box to both Vcc and Trig, and negative to GND. I guess I’ll have to modify it now.

http://i303.photobucket.com/albums/nn142/a8616034/2_zpse76480fb.jpg

Also the sensors on my robotic car, do they only need to connect Vcc and GND in order to get signals and output from Echo or do they also need to trigger in order to receive signals?

And I’ve already applied constant 5v to the Trig pin for several mins, would it damage the sensor?

Thank you.

Thanks for the info. It seems to me that you have enough space to add a Pro Mini :wink: Leaving the trig constant 5V shouldn’t damage the sensor, it would just send one pulse when it is turned on and nothing after that.

For the car, I don’t think you need to send the trig signal to receive the echo signal. Some sensors require, but I don’t think it’s the case for this one.

Hi jarcand,

If I choose to use a Pro Micro, it says the freq is 16MHz, but HC-SR04 requires at least 10us pulse. In this case even the PWM is set to 100% it’s still only 62.5ns of the pulse, will it enough to trigger?

Thank you.

I’m not sure how you did your calculation for the PWM pulse…

You should be able to produce the pulses necessary (on pin 6) by using code something like this:

[code]
const int pin = 4;

void setup() {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}

void loop() {
digitalWrite(pin, HIGH);
delayMicroseconds(20); // wait 20 uS
digitalWrite(pin, LOW);
delay(50); // wait 50 ms
}[/code]

Hi jarcand,

Sorry I forgot it’s a microcontroller, I thought the freq is fixed. I’ve already ordered it.

Thanks a lot.

Glad we can help. Let us know how your project goes!

Hi jarcand,

I’ve got the uC and finish the tracking device, however the sensors on the car turned out to be not working. (Tracking device is tested working)
I hooked up the Vcc and GND, and the Trig pin was originally high, I connected it to ground. Then I turned on the tracking device put in front of the sensors, the Echo pin didn’t give anything. Does that mean the Trig pin has to be triggered not just to generate signal but also to turn on the mic for receiving? without triggering Trig pin, the mic is not working. In this case all I can do is also connect the sensors on the car to the uC and trigger the Trig pin, but I don’t want the sensors on the car to generate any signal, so I want to desolder the speaker.

Can you tell me if my approach is correct and if so, which one is the speaker?

It’s possible. The way the pins are setup would suggest it would work for echo-only, but it’s hard to be sure if it’s not documented, and there’s not a lot of documentation for a $4 sensor…

We have contacted the manufacturer to see if they have recommendations.

On the other hand, these two models from Devantech have documented features for echo-only:

See the section on “Fake Ranging” in the manual for the RB-Dev-20:

Before doing any permanent desoldering, we would recommend starting with trying to cover the element with tape: that should be enough to kill a transmitted pulse. We’re not sure which is which: we would recommend start by covering one, and then try the other.

Hope this helps,

Thank you jarcand.

I guess I’ll start by covering the speaker and testing.

Covering one of them seems to work, it reacts when I turn my tracking device on.

I installed 2 HC-SR04 on my Arduino Uno, and wrote a code:

[code]int trig = 2;
int echor = 13;
int echol = 12;
long durr, disr, durl, disl;

void setup(){
pinMode(trig, OUTPUT);
pinMode(echor, INPUT);
pinMode(echol, INPUT);
Serial.begin(9600);
}

void loop(){
digitalWrite(trig, LOW);
delayMicroseconds(20);
digitalWrite(trig, HIGH);
durr = pulseIn(echor, HIGH);
disr = durr/58.2;
durl = pulseIn(echol, HIGH);
disl = durl/58.2;
Serial.print("Right: ");
if(disr>200){Serial.println(“Out of Range”);}
else{Serial.println(disr);}
Serial.print("Left: ");
Serial.println(disl);
delay(1000);
}[/code]

why is it only giving me the right sensor value?

That problem would be due the way that the pulseIn function works: it’s a blocking function that in this case waits for the pin to go from low to high and back from high to low and gives the time it was high.

In this application however, the pulses of the left and right sensors will overlap, so this function won’t work for this: by the time the high->low transition of the left sensor is done, the low->high transition (or the whole pulse) of the right sensor was missed.

Instead, you would have to implement the logic of the pulseIn function manually in your code, with two instances done at the same time, which can be tricky. For a starting point, you can refer to the code for the pulseIn function that can be found in hardware/arduino/cores/arduino/wiring_pulse.c inside the Arduino IDE install directory. The source is also available on GitHub here: github.com/arduino/Arduino/blob … ng_pulse.c

Hope this helps,

Hi, I’ve got the SRF02 sensors. I put two on the robotic car and one on the tracking device, operated by different micro controller. I’ve worked out the fake ranging, right now I’m stuck on the synchronization between the transducer and receiver, do you have any idea how to do that?

Thank you.

This case can I use this : robotshop.com/en/sfe-maxstre … ennae.html to send commands from one microcontroller to another?

Hi, yes, the XBee Transceiver Module can be used for communicating between to microcontrollers.

Hi, here I’m again. I finally bought the RF Link transmitter and receiver 434MHz, I’m working on the code now.

This is the transmitter code:

[code]#include <VirtualWire.h>

const int transmit_pin = 1;

void setup()
{
//Serial.begin(9600);
// Initialise the IO and ISR
vw_set_tx_pin(transmit_pin);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
}

void loop()
{
char *msg = “Tx”;
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
//Serial.println(“Txed”);
delay(1000);
}
[/code]
and this is the receiver code:

[code]
#include <VirtualWire.h>
const int receive_pin = 0;

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // Debugging only
// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}

void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen)) // Non-blocking
{
Serial.print("Got: ");
for (int i = 0; i < buflen; i++)
{
Serial.print(buf);
Serial.print(’ ');
}
Serial.println();
}
}[/code]

This way I can successfully receive the data, thing is I want to see the serial screen on the transmitter so I can debug, but if I add the line Serial.begin(9600) to the code of transmitter I can’t receive anything on the receiver. Why the receiver is able to add that line for debugging only but not transmitter? and what does Serial.begin diff from vw_setup(), I tried to get rid of vw_setup and use Serial.begin and it didn’t work.

Thank you.

The “Serial” object uses pins 0 and 1 on the Arduino, so the pins can’t usually be used for other things at the same time. In this case, since the receiver code is only using pin 0 to receive the data, pin 1 is still available for transmitting data via Serial. But for the transmit code, pin 1 is being used by VirtualWire so it can’t be used also with Serial.

It might be possible to change the pin that VirtualWire is using to something like 2 or 3, that way Serial can use pin 1. You would need to verify the documentation for VirtualWire to ensure you can do this. I just saw also that VirtualWire is being discontinued and the programmer recommends using RadioHead instead…

Thank you so much jarcand, I’ve finished my project. Altho it’s not perfect but it’s working. You have been a great help, thanks again.

Great, glad to help! If you have any videos or pictures of your robot, that would be nice to see!