HC-SR04 and FreeBSD

Luiz Otavio O Souza lists.br at gmail.com
Fri Aug 22 19:55:16 UTC 2014

On 22 August 2014 00:43, Evandro Nunes wrote:
> On Thu, Aug 21, 2014 at 4:13 PM, Rui Paulo wrote:
>> You can use an led instead of a multimeter. The point I'm trying to make
>> is to make sure the gpio number really corresponds to that port number.
> still no success, but just an update...
> ok I added two led:
> pin 02:    0    gpio_2<IN>  ===> echo (orange LED)
> pin 03:    0    gpio_3<OUT> ===> trigger (blue LED)
> pin 49:    0    gpio_49<IN> ===> previous echo
> and I have the two simple loops below.
> when I run loop1, BLUE LED blinks every second;
> when I run loop2 while loop1 stills run, ORANGE LED won't blink, and loop2
> value still shows 0 value
> if I "gpioctl -c 2 OUT ; gpioctl -t 2", ORANGE LED will light, confirming
> LED is OK; thoses leds will light with 2-5v input... however I have no idea
> if the sonar output will range 2-5 or if it will be below 2 (i tried adding
> my hand very close and far away from the sensor but led was never lit)

Probably the echo output doesn't provide enough current to drive the
LED (LEDs are driven by current and not by voltage).

Be aware that you also need to limit the voltage on the sensor output
to be within the 3.3V limit:


(you can can follow the explanation and the schematics)

> so I'd better use a multimeter for sure...

You'd be better with a logic analyser, this kind of short pulses can't
be seen on a multimeter.

> loop1:
> while true ; do
>  gpioctl -t 3; sleep .200; gpioctl -t 3
>  #gpioctl 3
>  sleep 1
> done
> loop2:
> while true ; do
>  gpioctl 2
>  sleep .500
> done

The sensor output pulse will be active for a period of 116us ~ 23200us
(2cm ~ 400cm, given the formula from data sheet: cm = us / 58).

I'm not sure you can reliably measure such short periods from userland
(but as the RPi link says it works... let's try it).

You can try to remove the LED from the echo pin (to reduce its load)
and reduce the sleep time on loop2 to see if you can read the sensor

I'll see if i can get my hands on HC-SR04 so i can try it out.


More information about the freebsd-arm mailing list