Application - HC-SR04, Ultrasonic ranging module

出自 flip the world
前往: 導覽搜尋
RPi2 with ultrasonic ranging module(HC-SR04)

HW connection

PRi2 --  HC-SR04
VCC  --  VCC(5V) 
Trig --  GPIO17(output)
Echo --  GPIO27(input)
Gnd  --  Gnd

Timing

HC-SR04 timing diagram
The Timing diagram is shown below. You only need to
 supply a short 10uS 
pulse to the trigger input to start the ranging, an
d then the module will send out 
an 8 cycle burst of ultrasound at 40 kHz and raise 
its echo. The Echo is a 
distance object that is pulse width and the range i
n proportion .You can 
calculate the range through the time interval betwe
en sending trigger signal and 
receiving echo signal. Formula: uS / 58 = centimete
rs or uS / 148 =inch; or: the 
range = high level time * velocity (340M/S) / 2; we
 suggest to use over 60ms 
measurement cycle, in order to prevent trigger sign
al to the echo signal.



GPIO control with gpio library

  • Install gpio library
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.25.tar.gz
gunzip /bcm2835-1.25.tar.gz
tar xvf bcm2835-1.25.tar
cd bcm2835-1.25
./configure
make
sudo make install
  • For RPi2, should modify the bcm2835.h before compile
    • #define BCM2835_PERI_BASE 0x3F000000
  • C example
#include <bcm2835.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

//GPIO 17
#define TRIG_PIN RPI_V2_GPIO_P1_11

//GPIO 27
#define ECHO_PIN RPI_V2_GPIO_P1_13 

int main()
{
	uint8_t value;
	struct timeval t1, t2;
	double d1,d2;
	double dist=0;

	if(!bcm2835_init())
	{
		perror("Init error!");
		return 1;
	}
	

	bcm2835_gpio_fsel(TRIG_PIN, BCM2835_GPIO_FSEL_OUTP);

	bcm2835_delayMicroseconds(3000);

	bcm2835_gpio_write(TRIG_PIN, HIGH);

	bcm2835_delayMicroseconds(10);

	bcm2835_gpio_write(TRIG_PIN, LOW);

        //wait first echo and timestamps to t1
	while( (value=bcm2835_gpio_lev(ECHO_PIN)) == 0)
		;
	gettimeofday(&t1, NULL);

        //wait second echo and timestamps to t1
        while( (value=bcm2835_gpio_lev(ECHO_PIN)) == 1)
		;
        gettimeofday(&t2, NULL);

	d1 = t1.tv_sec + (t1.tv_usec/1000000.0);
	d2 = t2.tv_sec + (t2.tv_usec/1000000.0);

	dist = (d2-d1) * 17000 ;

	printf("Distance: %5.2f cm\n", dist);
	return 0;
}
  • Compile with gpio library
    • gcc -o sensor sensor.c -lbcm2835

Debugfs for GPIO

  • Show gpio status
sudo mount -t debugfs debug /home/pi/debugfs/
sudo cat /home/pi/debugfs/gpio
---->
GPIOs 0-53, platform/3f200000.gpio, pinctrl-bcm2835:
 gpio-9   (sysfs               ) in  lo    
 gpio-10  (sysfs               ) out lo    
 gpio-35  (led1                ) in  hi    
 gpio-47  (led0                ) out lo


  • For specific GPIO pin
#!/bin/bash
#set gpio 17 as trig pin
echo 17 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio17/direction

#set gpio 27 as echo pin
echo 27 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio27/direction

Testing

Ranging testing using C

Ranging data from C
- Test with C code -
#	5cm	10cm	25cm
1	6	8.12	24.14
2	6.07	8.21	23.1
3	6.12	8.5	23.07
4	6.07	8.18	23.02
5	5.97	8.58	24.16
6	7.21	8.52	22.97
7	6.07	8.06	23.1
8	6.39	8.6	22.99
9	6.1	8.58	23.12
10	6.1	8.43	23
			
Average	6.21	8.378	23.267
Ranging data from Python
- Test with Python code -
#	5cm	10cm	25cm
1	6.6	10.3	24.6
2	6.7	10.2	24.5
3	6.6	10.1	24.5
4	6.6	10	24.6
5	6.6	10.1	24.5
6	6.7	10.1	24.6
7	6.6	9.7	24.6
8	6.6	9.6	24.6
9	6.6	9.6	24.6
10	6.6	9.7	24.6

Average	6.62	9.94	24.57

Reference

Using an Ultrasonic Sensor (HC-SR04) on a Raspberry Pi with Python

gpio c library

datasheet