NAME
RPi::HCSR04 - Interface to the HC-SR04 ultrasonic distance measurement sensor on the Raspberry Pi
SYNOPSIS
use RPi::HCSR04;
my $trig_pin = 23;
my $echo_pin = 24;
my $sensor = RPi::HCSR04->new($trig_pin, $echo_pin);
my $inches = $sensor->inch;
my $cm = $sensor->cm;
my $raw = $sensor->raw;
...
DESCRIPTION
Easy to use interface to retrieve distance measurements from the HC-SR04 ultrasonic distance measurement sensor.
Requires wiringPi to be installed.
VOLTAGE DIVIDER
The HC-SR04 sensor requires 5V input, and that is returned back to a Pi GPIO pin from the ECHO
output on the sensor. The GPIO on the Pi can only handle a maximum of 3.3V in, so either a voltage regulator or a voltage divider must be used to ensure you don't damage the Pi.
Here's a diagram showing how to create a voltage divider with a 1k and a 2k Ohm resistor to lower the ECHO
voltage output down to a safe ~3.29V. In this case, TRIG
is connected to GPIO 18, and ECHO
is connected to GPIO 23.
METHODS
new
Instantiates and returns a new RPi::HCSR04 object.
Parameters:
$trig
Mandatory: Integer, the GPIO pin number of the Raspberry Pi that the TRIG
pin is connected to.
$echo
Mandatory: Integer, the GPIO pin number of the Raspberry Pi that the ECHO
pin is connected to.
inch
Returns a floating point number containing the distance in inches. Takes no parameters.
cm
Returns a floating point number containing the distance in centemetres. Takes no parameters.
raw
Returns an integer representing the return from the sensor in raw original form. Takes no parameters.
C FUNCTIONS
These are to be only used within the module itself.
setup
Performs the wiringPi setup routine.
raw_c
Called by raw()
.
inch_c
Called by inch()
.
cm_c
Called by cm()
.
REQUIREMENTS
* wiringPi must be installed. * At this time, your program will have to be run as root (under sudo
). * You must regulate the voltage from the ECHO
pin down to a safe 3.3V from the 5V input. See "VOLTAGE DIVIDER" for details.
AUTHOR
Steve Bertrand, <steveb at cpan.org>
LICENSE AND COPYRIGHT
Copyright 2017 Steve Bertrand.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.