Coder Social home page Coder Social logo

hc-sr04_driver's Introduction

HC-SR04 Raspberry Pi Driver

This driver is designed to make reading the distance from the senor as simple as possible.

Build Instructions

git clone https://github.com/Jex-y/HC-SR04_driver
cd HC-SR04_driver
sudo apt install raspberrypi-kernel-headers
make
sudo insmod hcsr04_sensor.ko

To immediately load the module run:

sudo insmod hcsr04_sensor.ko

Note that this means that the driver will be removed on reboot. To load it on boot:

sudo mkdir -p /lib/modules/$(uname -r)/kernel/drivers/sensors
sudo cp hcsr04_sensor.ko /lib/modules/$(uname -r)/kernel/drivers/sensors
sudo depmod

and add the line hcsr04_sensor to /etc/modules

Usage

The device created is /dev/hcsr04

The GPIO pins can be set by writing to the device file and the round trip time in microseconds (as the bytes of an unsigned long) can be read from this file.

Example

ECHO_PIN = 3
TRIG_PIN = 2

with open('/dev/hcsr04', 'w') as file:
    file.write(f'ECHO {ECHO_PIN:02d}')
    file.write(f'TRIG {TRIG_PIN:02d}')

with open('/dev/hcsr04', 'rb') as file:
    data = file.read()

trip = int.from_bytes(data, 'little')
secs = trip * 1e-6 / 2
dist = 340 * secs
print(f'Total roundtrip took {trip} us, this gives us a distance of {dist * 100:.2f} cm')

Limitations

  • Note that the driver requires that the pin numbers are zero padded to two digits so that the input is a constant length.
  • The return value of a read is the single round trip time in microseconds. Conversion to distance should be done in higher level code as the user may want to account for pressure, temperature, humiditity etc. that affect the speed of sound.

hc-sr04_driver's People

Contributors

jex-y avatar

Stargazers

 avatar

Watchers

 avatar

hc-sr04_driver's Issues

Sensor giving inaccurate and inconsistent readings for distances > 1m

When trying to read distances of over about 1m the sensor returns values of 186000us-187000us with varying frequencies e.g. about 40% and 1.4 meters.

These values are about 30-32 meters and are just under half the timeout value for the sensor.

This is not expected behaviour for a timeout as the value returned should be MAX_ULONG.

Driver deals with writes incorrectly

When writing to the driver in python, the buffer is not flushed between writes therefore the driver receives all of the input at once. This causes a buffer overflow.

Driver deals with reading incorrectly

The driver does not take into account the offset when being read. This means that if

cat /dev/hcsr04_device

is called it repeatedly reads the sensor rather than once.

Sensor Timeout

The sensor is not able to read distances due to what appears to be a timeout error.

Sensor Time Variability

The sensors seems to be outputting changing values as it repeats the short distance test. Possibly due to echoes or background vibrations.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.