匿名
尚未登入
登入
flip the world
搜尋
檢視Application - HC-SR04, Ultrasonic ranging module的原始碼
出自flip the world
命名空間
頁面
討論
更多
更多
頁面操作
閱讀
檢視原始碼
歷史
←
Application - HC-SR04, Ultrasonic ranging module
您沒有權限執行編輯此頁面,由於以下原因:
您請求的操作只有這個群組的使用者能使用:
使用者
您可以檢視並複製此頁面的原始碼。
[[File:IMAG1074.jpg|thumb|200px|RPi2 with ultrasonic ranging module(HC-SR04)]] == HW connection == <pre> PRi2 -- HC-SR04 VCC -- VCC(5V) Trig -- GPIO17(output) Echo -- GPIO27(input) Gnd -- Gnd </pre> == Timing == [[File:hc-sr04-timing.png|thumb|700px|HC-SR04 timing diagram]] <pre> 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. </pre> == GPIO control with gpio library == * Install gpio library <pre> 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 </pre> *For RPi2, should modify the bcm2835.h before compile ** #define BCM2835_PERI_BASE 0x3F000000 *C example <pre> #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; } </pre> * Compile with gpio library **gcc -o sensor sensor.c -lbcm2835 == Debugfs for GPIO == *Show gpio status <pre> 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 </pre> *For specific GPIO pin <pre> #!/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 </pre> == Testing == [[File:ranging_test.jpg|Ranging testing using C]] [[File:Ranging test data.png|thumb|300px|Ranging data from C]] <pre> - 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 </pre> [[File:Python hc-sr04.jpg|thumb|300px|Ranging data from Python]] <pre> - 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 </pre> == Reference == [http://www.bytecreation.com/blog/2013/10/13/raspberry-pi-ultrasonic-sensor-hc-sr04, Using an Ultrasonic Sensor (HC-SR04) on a Raspberry Pi with Python] [http://coldnew.github.io/blog/2013/06/27_1aced.html, gpio c library] [http://www.micropik.com/PDF/HCSR04.pdf, datasheet] [[Category:Projects]]
返回到「
Application - HC-SR04, Ultrasonic ranging module
」。
導覽
導覽
首頁
近期變更
隨機頁面
MediaWiki說明
wiki工具
wiki工具
特殊頁面
頁面工具
頁面工具
使用者頁面工具
更多
連結至此的頁面
相關變更
頁面資訊
頁面日誌