How to use HC-SR04 ultrasonic sensor without hardware-pwm pins?

Hello all,

I'm planning on putting six HC-SR04 ultrasonic distance sensors in my next project.
The thing is I don't have enough pwm pins on my Arduino Pro Mini (3.3v 8Mhz) for all the six trig pins (I also need the SPI pins 10/11 for a USB Host Module).

Would a software-pwm library (like softPWM) work for this purpose?

I also came across the NewPing library where it LOOKS like it can use any digital pin. But I can't confirm whether that is correct or not.

Any advice?

And why would you need to use PWM to activate those sensors?

NewPing works well.

As I understood it, you need to make a 40khz soundwave (using pwm) on the trig pin using the Arduino. Not correct?
All the "hc-sr04 Arduino wiring" google examples also wire the trig pin to a PWM pin. Is that a coincidence?

You need to send the trig pin a 10µs pulse. The module will then send a 40KHz burst (8 cycles) and then you need to monitor the echo pin to measure the time the 40KHz burst took to reach an object and come back.

The Arduino does not need to generate the 40KHz pulses. This is done by the module.

What you have to worry about, is that the echo can be "heard" by more than one of your sensors, depending on the surrounding conditions (walls, etc.), so measuring distances simultaneously can be tricky.

Aaaaaah. Ok. I misunderstood then. I thought the Arduino was generating the 40khz directly.
So that means I can use any GPIO pin, including 'analog' pins. Correct?

Interference will not be a problem I think since I plan on doing the measurements one by one.

Correct.

You need one pin for the trigger, which can be shared by all sensors. And one echo pin for each sensor. 7 pins for 6 sensors.

Sharing the trigger pin means that all sensors will emit a pulse at the same time, but that should not be a problem because you will listen to only one sensor echo pin at a time.

1 Like

Yes.

All you need to do is send a start pulse from the output pin to get things going.

The ultrasonic sensor has the electronics to take it from there.

If I trigger all the sensor at the same time, wouldn't I risk receiving other sensors' reflections on the sensor I'm currently listening to?

Also, anyone know of the NewPing library works also with a STM32F103 board?

Yes, indeed. It will ultimately depend on the specific setup you have and its surroundings. If you have enough pins you will be better off using separate triggers.

I recommend NewPing library, you also have one-pin option, (trig and echo pin shorted), so you`ll need only one GPIO per HC-SR04, or 6 GPIOs.

You might be able to receive the reflection pulse of all the devices if you connected each to one input pin through a 1N4148 diode with a 10k resistor to ground at the input pin
Then you will only receive the reflection pulse from the sensor you trigger.

There are of course more complex methods as this is seen as a bit if a hack, but it will work.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.