Using a single bi-directional pin with HC-SR04 Ultrasonic Sensor

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I'm encountering a problem when attempting to integrate the HC-SR04 ultrasonic sensor using a Psoc4 Pioneer Kit 044.  The HC-SR04 has a 4 pin interface: vcc, gnd, trigger, and echo.  The trigger pin sends a 10us pulse triggering the sensor to send its 40khz ultrasonic pulse.  The echo pin receives a digital pulse from the sensor proportional to the distance detected by the sensor.

   

I have successfully interfaced the HC-SR04 sensor with an Arduino using 3 pins by tying echo and trigger to the same pin.  Within the Arduino firmware, the pin is set to an output for the trigger and then set to an input to receive the pulse.

   

I have 8 sensors that I would like to interface with the Psoc4 so I really need to achieve the same 3 pin interface as there are other components in this design.  I have successfully configured the Psoc4 to interface with the HC-SR04 using separate trigger and echo pins.  The firmware triggers the pulse on the trigger pin and a counter detects the echo pulse.

   

What I am unable to do is to combine the trigger and echo pins onto a single pin.  When I connect them, the 10us pulse never happens.  It looks like the echo pin is pulling the trigger pin to ground.  I have tried all of the pin output options to no success.  I expect that if the Arduino can do this, it should be possible with the Psoc4 -- event if there needs to be some external components.

   

Basically, I have two questions:

   

1. Can/How should the pin interface be implemented to allow both the echo and trigger pins of the sensor to be tied to a single Psoc4 pin?

   

2. How can single sensor solution be expanded to support 8 sensors?

   

I have attached the HC-SR04 datasheet.  Unfortunately, it doesn't provide much in terms of electrical specs.

   

Regards,

   

Tim

0 Likes
3 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome to the forum.

   

Use an output enable to gate the trigger pulse. Because you do not need (hopefully) to read all sensors in parallel you could use a different approach: Tie all echos together and use 8 triggers.

   

 

   

Bob

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Tim,

   

I "believe" the pin can be switched between I and O modes in hardware or in software.

   

#define Pin_DM_DIG_HIZ (0x01u) // High Impedance Digital.
#define Pin_DM_STRONG (0x06u) // Strong Drive.
 

   

PIN_SetDriveMode(Pin_DM_STRONG); //digital output

   

PIN_SetDriveMode(Pin_DM_DIG_HIZ); //digital input 

   

Else you can try to declare pin for both input and output with "output enable" and try to get it work this way (untested).

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Thanks for the above suggestions.

   

Regarding tying the echo pins together, the problem there is that they aren't open collector outputs.  The echo pin is pulling 23 ma.  I tried tying just two sensors together.  I could see a pulse but the output voltage was less than a volt.

   

As for setting the pin direction, that really isn't the problem.  The problem is the output drive of the echo pin vs the Psoc4 pin.  On the Psoc4, electrically, I can't tie echo and trigger pins together and still get the Psoc4 to drive the pulse.  So, the same problem exists even with the output enable on the pin.  If I enable the Psoc4 pin output while its tied to echo pin, the echo pin will pull it down.

   

As an experiment, I tried tying the trigger outputs together, just to reversing the idea by Bob, but that causes all the sensors to emit their 40kHz signal at the same time which causes interference.

   

I've been searching to understand the drive output for the Arduino to understand how that's working.  At this point, I still like the idea of tying all the echos pins together, but it looks like I'll to electrically convert them before I can connect to the Psoc.  I did find a schematic for the HCSR04 and it shows the both echo and trigger pins being pulled up via a 10k resister into a OTP.  Maybe it will help.

0 Likes