-
ARM MBED 엠베드 초음파센서 HC SR04 구동 방법개발 팁/Embedded 2017. 11. 11. 15:41반응형
1) 테스트할 보드는 NXP FRDM-K64F이다
2) 연결할 센서는 HCSR04 초음파센서이다.
데이터시트: http://www.micropik.com/PDF/HCSR04.pdf
HCSR04 초음파 센서의 동작은 아래와 같다:
보드에서 trigger pin을 통해 센서에 10us 짜리 trigger pulse를 보낸다.
센서에서 초음파 burst가 발생한다.
초음파가 객체에 부딪혀서 반사되어 돌아오면 echo pin을 통해 보드에 pulse가 전달된다.
3) 직접 코드를 작성해도 되지만 MBED의 기존 코드를 가져오기로 하자
HCSR04로 검색하면 여러 코드가 나오는데, 일단 아래의 코드를 가져온다:
https://os.mbed.com/users/goeltanu/code/HCSR04/
mbed add http://mbed.org/users/goeltanu/code/HCSR04/4) 이 라이브러리를 사용한 예제 코드는 아래와 같다:
#include "mbed.h"
#include "hcsr04.h" // mbed add http://mbed.org/users/goeltanu/code/HCSR04/DigitalOut led1(LED1);DigitalOut trig_pin(D8);DigitalIn echo_pin(D9);// main() runs in its own thread in the OSint main() {printf( "\nultrasonic\n" );echo_pin.mode(PullDown);HCSR04 hcsr(D8, D9);while (true) {led1 = !led1;hcsr.start();wait_ms(500);printf ( "Distance(cm): %d\n", hcsr.get_dist_cm() );wait(0.5);}}# NXP FRDM-K64 보드의 경우 Digital In 핀들이 기본적으로 1을 리턴하므로, PullDown 셋팅을 적용해서 센서에서 리턴 펄스가 오지 않으면 0으로 리턴하게 하도록 해야 한다.
5) 실행 터미널 화면은 다음과 같다
반응형'개발 팁 > Embedded' 카테고리의 다른 글
ARM MBED 엠베드 음악 연주하기 (내장 DAC 사용) (0) 2017.11.12 임베디드 구현을 위한 음계 주파수 옥타브 정보 (0) 2017.11.12 ARM MBED 엠베드 압력센서 QA3040P 구동 방법 (0) 2017.11.11 ARM MBED 엠베드 FRDM-K64F 보드 PIN 핀 배치 설명 그림 (0) 2017.11.11 ARM MBED 예제 (Blinky/블링키) 빌드/컴파일/다운로드 (0) 2017.10.28