Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Reading from an HC-SR04 ultrasonic distance sensor on the Raspberry Pi

by stevieb (Canon)
on Jan 13, 2017 at 22:38 UTC ( [id://1179554]=CUFP: print w/replies, xml ) Need Help??

I've completed another Raspberry Pi related distribution, RPi::HCSR04. This one allows you to use Perl to read data from the HC-SR04 ultrasonic distance sensor.

It's trivial to use, however, because it uses wiringPi internally, your scripts require root privileges.

use warnings; use strict; use feature 'say'; use RPi::HCSR04; my $trig_pin = 23; my $echo_pin = 24; my $sensor = RPi::HCSR04->new($trig_pin, $echo_pin); # each call is a separate poll say $sensor->raw; say $sensor->cm . " cm"; say $sensor->inch . " \"";

Output:

634 10.915135593358 cm 4.29729747772217 "

There's still a bit more work I have to do (catch out-of-range measurements etc), but it works pretty well and is surprisingly accurate.

Note that per the documentation, the HC-SR04 requires 5v in, and also returns 5v from the ECHO pin back to the Pi's GPIO (which only handles 3.3v), so a voltage regulator or voltage divider is required to limit the voltage to a healthy 3.29v. I opted for the divider while writing the software. Here's a diagram depicting how I achieved that.

Next up, a SN74HC595 shift register, as I need it to continue to work on the other various projects I have going on. I've almost completed the dist for the BMP180 barometric/altimeter sensor, as well as the MCP300x series analog-to-digital converters.

Replies are listed 'Best First'.
Re: Reading from an HC-SR04 ultrasonic distance sensor on the Raspberry Pi
by stevieb (Canon) on Jan 15, 2017 at 18:53 UTC

    The day after I wrote this distribution, Gordon (creator of wiringPi) updated his software, so I've updated WiringPi::API and RPi::WiringPi to support the BMP180 directly, as well as the ability to use the 74HC595 shift registers (four in series for a total of 32 extra output pins using only three GPIO pins).

    I have a bunch more stuff to add to those modules, and I'm very close to including a DAC, digital POTs and a couple of others. After that's all done, I'm going to do a full-blown blog post series on using Perl on the RasPi.

    I also added in a new wiringPiVersion() function in wiringPi itself, and sent Gordon the patch. He's replied and stated he'll incorporate something similar. The reasoning behind my patch was so that the WiringPi::API core distribution can check to see what version of wiringPi we're on, to ensure that installation goes smoothly, else report to the user that there's a version mismatch (reported by Joel Berger a couple of days ago).

Re: Reading from an HC-SR04 ultrasonic distance sensor on the Raspberry Pi
by $h4X4_|=73}{ (Monk) on Jan 25, 2017 at 10:22 UTC

    Hmmm... Sounds like there can be a RPi Boe-Bot made in Perl.

    Nice work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1179554]
Front-paged by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-28 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found