센서 > 초음파센서 SRF04 and SRF05 -128 코드비젼

TODAY1,215 TOTAL2,232,014
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | 센서 | 초음파센서 | 초음파센서 SRF04 and SRF05 -128 코드비젼

페이지 정보

작성자 키트 작성일2017-08-23 13:40 조회4,770회 댓글0건

본문


 HC-SR04
중국산 초음파 모듈입니다. 영국 Devantech 의 SRF-04 와 비슷합니다.
2093095301_KLvAWZGU_hcsr04t.gif3Ftype3Dw740




SRF04 - Ultra-Sonic Ranger
Technical Specification

2093095301_6UdetYCI_srf04.jpg

This project started after I looked at the Polaroid Ultrasonic Ranging module. It has a number of disadvantages for use in small robots etc.

  1. The maximum range of 10.7 metre is far more than is normally required, and as a result
  2. The current consumption, at 2.5 Amps during the sonic burst is truly horrendous.
  3. The 150mA quiescent current is also far too high.
  4. The minimum range of 26cm is useless. 1-2cm is more like it.
  5. The module is quite large to fit into small systems, and
  6. It’s EXPENSIVE.

The SRF04 was designed to be just as easy to use as the Polaroid sonar, requiring a short trigger pulse and providing an echo pulse. Your controller only has to time the length of this pulse to find the range. The connections to the SRF04 are shown below:

+5v
Echo output
Trigger input
Do not connect
GND
2093095301_A7yPEFaS_srf04con.jpg

The SRF04 Timing diagram is shown below. You only need to supply a short 10uS pulse to the trigger input to start the ranging. The SRF04 will send out an 8 cycle burst of ultrasound at 40khz and raise its echo line high. It then listens for an echo, and as soon as it detects one it lowers the echo line again. The echo line is therefore a pulse whose width is proportional to the distance to the object. By timing the pulse it is possible to calculate the range in inches/centimeters or anything else. If nothing is detected then the SRF04 will lower its echo line anyway after about 36mS.

2093095301_cJwfNHQ1_srf04timing.gif

Here is the schematic, You can download a better quality pdf (161k) version srf1.pdf

2093095301_MycRLJjH_srf04diagram.gif

 

 

 

The circuit is designed to be low cost. It uses a PIC12C508 to perform the control functions and standard 40khz piezo transducers. The drive to the transmitting transducer could be simplest driven directly from the PIC. The 5v drive can give a useful range for large objects, but can be problematic detecting smaller objects. The transducer can handle 20v of drive, so I decided to get up close to this level. A MAX232 IC, usually used for RS232 communication makes and ideal driver, providing about 16v of drive.

The receiver is a classic two stage op-amp circuit. The input capacitor C8 blocks some residual DC which always seems to be present. Each gain stage is set to 24 for a total gain of 576-ish. This is close the 25 maximum gain available using the LM1458. The gain bandwidth product for the LM1458 is 1Mhz. The maximum gain at 40khz is 1000000/40000 = 25. The output of the amplifier is fed into an LM311 comparator. A small amount of positive feedback provides some hysterisis to give a clean stable output.

The problem of getting operation down to 1-2cm is that the receiver will pick up direct coupling from the transmitter, which is right next to it. To make matters worse the piezo transducer is a mechanical object that keeps resonating some time after the drive has been removed. Up to 1mS depending on when you decide it has stopped. It is much harder to tell the difference between this direct coupled ringing and a returning echo, which is why many designs, including the Polaroid module, simply blank out this period. Looking at the returning echo on an oscilloscope shows that it is much larger in magnitude at close quarters than the cross-coupled signal. I therefore adjust the detection threshold during this time so that only the echo is detectable. The 100n capacitor C10 is charged to about –6v during the burst. This discharges quite quickly through the 10k resistor R6 to restore sensitivity for more distant echo’s.

A convenient negative voltage for the op-amp and comparator is generated by the MAX232. Unfortunately, this also generates quite a bit of high frequency noise. I therefore shut it down whilst listening for the echo. The 10uF capacitor C9 holds the negative rail just long enough to do this.

In operation, the processor waits for an active low trigger pulse to come in. It then generates just eight cycles of 40khz. The echo line is then raised to signal the host processor to start timing. The raising of the echo line also shuts of the MAX232. After a while – no more than 10-12mS normally, the returning echo will be detected and the PIC will lower the echo line. The width of this pulse represents the flight time of the sonic burst. If no echo is detected then it will automatically time out after about 30mS (Its two times the WDT period of the PIC). Because the MAX232 is shut down during echo detection, you must wait at least 10mS between measurement cycles for the +/- 10v to recharge.

Performance of this design is, I think, quite good. It will reliably measure down to 3cm and will continue detecting down to 1cm or less but after 2-3cm the pulse width doesn’t get any smaller.

Maximum range is a little over 3m. As and example of the sensitivity of this design, it will detect a 1inch thick plastic broom handle at 2.4m.
Average current consumption is reasonable at less than 50mA and typically about 30mA.

Download the source code and a ready assembled hex file

Calculating the Distance
The SRF04 provides an echo pulse proportional to distance. If the width of the pulse is measured in uS, then dividing by 58 will give you the distance in cm, or dividing by 148 will give the distance in inches. uS/58=cm or uS/148=inches.

Changing beam pattern and beam width
You can't!This is a question which crops up regularly, however there is no easy way to reduce or change the beam width that I'm aware of. The beam pattern of the SRF04 is conical with the width of the beam being a function of the surface area of the transducers and is fixed.  The beam pattern of the transducers used on the SRF04, taken from the manufacturers data sheet, is shown below.

2093095301_KC0glySJ_beam.gif

There is more information in the sonar faq.

Update - May 2003
Since the original design of the SRF04 was published, there have been incremental improvements to improve performance and manufacturing reliability. The op-amp is now an LMC6032 and the comparator is an LP311. The 10uF capacitor is now 22uF and a few resistor values have been tweaked.  These changes have happened over a period of time.

All SRF04's manufactured after May 2003 have new software implementing an optional timing control input using the "do not connect" pin. This connection is the PIC's Vpp line used to program the chip after assembly. After programming its just an unused input with a pull-up resistor. When left unconnected the SRF04 behaves exactly as it always has and is described above. When the "do not connect" pin is connected to ground (0v), the timing is changed slightly to allow the SRF04 to work with the slower controllers such as the Picaxe. The SRF04's "do not connect" pin now acts as a timing control. This pin is pulled high by default and when left unconnected, the timing remains exactly as before. With the timing pin pulled low (grounded) a 300uS delay is added between the end of the trigger pulse and transmitting the sonic burst. Since the echo output is not raised until the burst is completed, there is no change to the range timing, but the 300uS delay gives the Picaxe time to sort out which pin to look at and start doing so. The new code has shipped in all SRF04's since the end of April 2003. The new code is also useful when connecting the SRF04 to the slower Stamps such as the BS2. Although the SRF04 works with the BS2, the echo line needs to be connected to the lower numbered input pins. This is because the Stamps take progressively longer to look at the higher numbered pins and can miss the rising edge of the echo signal. In this case you can connect the "do not connect" pin to ground and give it an extra 300uS to get there.

2093095301_ZuzbIqNf_25C125A625B825F1_25BE25F825C025BD.png3Ftype3Dw740

http://www.robot-electronics.co.uk/htm/srf04tech.htm

 

SRF05 - Ultra-Sonic Ranger
Technical Specification

2093095301_4tmLnxf0_srf05front.jpg

Introduction
The SRF05 is an evolutionary step from the SRF04, and has been designed to increase flexibility, increase range, and to reduce costs still further. As such, the SRF05 is fully compatible with the SRF04. Range is increased from 3 meters to 4 meters. A new operating mode (tying the mode pin to ground) allows the SRF05 to use a single pin for both trigger and echo, thereby saving valuable pins on your controller. When the mode pin is left unconnected, the SRF05 operates with separate trigger and echo pins, like the SRF04. The SRF05 includes a small delay before the echo pulse to give slower controllers such as the Basic Stamp and Picaxe time to execute their pulse in commands.

Mode 1 - SRF04 compatible - Separate Trigger and Echo
This mode uses separate trigger and echo pins, and is the simplest mode to use. All code examples for the SRF04 will work for the SRF05 in this mode. To use this mode, just leave the mode pin unconnected - the SRF05 has an internal pull up resistor on this pin.

2093095301_4ue0sgSH_srf05p2.jpg

2093095301_NtiVFQpS_srf05tma.gif

Mode 2 - Single pin for both Trigger and Echo
This mode uses a single pin for both Trigger and Echo signals, and is designed to save valuable pins on embedded controllers. To use this mode, connect the mode pin to the 0v Ground pin. The echo signal will appear on the same pin as the trigger signal. The SRF05 will not raise the echo line until 700uS after the end of the trigger signal. You have that long to turn the trigger pin around and make it an input and to have your pulse measuring code ready. The PULSIN command found on many popular controllers does this automatically.

2093095301_lsyXEbRM_srf05p1.jpg

2093095301_QKAgvFYm_srf05tmb.gif

To use mode 2 with the Basic Stamp BS2, you simply use PULSOUT and PULSIN on the same pin, like this:


SRF05 PIN 15             ' use any pin for both trigger and echo
Range VAR Word           ' define the 16 bit range variable

SRF05 = 0                ' start with pin low
PULSOUT SRF05, 5         ' issue 10uS trigger pulse (5 x 2uS)
PULSIN SRF05, 1, Range   ' measure echo time
Range = Range/29         ' convert to cm (divide by 74 for inches) 
  

Calculating the Distance
The SRF05 Timing diagrams are shown above for each mode. You only need to supply a short 10uS pulse to the trigger input to start the ranging. The SRF05 will send out an 8 cycle burst of ultrasound at 40khz and raise its echo line high (or trigger line in mode 2). It then listens for an echo, and as soon as it detects one it lowers the echo line again. The echo line is therefore a pulse whose width is proportional to the distance to the object. By timing the pulse it is possible to calculate the range in inches/centimeters or anything else. If nothing is detected then the SRF05 will lower its echo line anyway after about 30mS.

The SRF04 provides an echo pulse proportional to distance. If the width of the pulse is measured in uS, then dividing by 58 will give you the distance in cm, or dividing by 148 will give the distance in inches. uS/58=cm or uS/148=inches.

The SRF05 can be triggered as fast as every 50mS, or 20 times each second. You should wait 50ms before the next trigger, even if the SRF05 detects a close object and the echo pulse is shorter. This is to ensure the ultrasonic "beep" has faded away and will not cause a false echo on the next ranging.

The other set of 5 pins
The 5 pins marked "programming pins" are used once only during manufacture to program the Flash memory on the PIC16F630 chip. The PIC16F630's programming pins are also used for other functions on the SRF05, so make sure you don't connect anything to these pins, or you will disrupt the modules operation.

Changing beam pattern and beam width
You can't!This is a question which crops up regularly, however there is no easy way to reduce or change the beam width that I'm aware of. The beam pattern of the SRF05 is conical with the width of the beam being a function of the surface area of the transducers and is fixed.  The beam pattern of the transducers used on the SRF05, taken from the manufacturers data sheet, is shown below.

2093095301_KC0glySJ_beam.gif

There is more information in the sonar faq.

http://www.robot-electronics.co.uk/htm/srf05tech.htm

 

 

 

// http://www.robot-electronics.co.uk/htm/ATMEGA32_examples.htm#SRF04%20Ultrasonic%20Ranger
// 위 예제를 조금 손봐서
// 128 코드비젼 용으로 만들어 봤습니다.

 

// DateTime : 2018-05-05 오전 3:24:56
// by Ok-Hyun Park
//
/****************************************
*SRF04/SRF05 example code for ATMEGA128*
*As both the SRF04 and SRF05 can use the*
*same interface this example will work*
*for both of these modules.*
*****************************************/
//
#include <mega128.h>
#include <delay.h>
//
//
#asm
  .equ __lcd_port=0x1B// lcd포트 PORTA
#endasm
#include
//
#define Trigger   PORTC.0 //초음파 트리거
#define Echo      PINC.1  //초음파 에코
//
char s[21];
//
unsignedint getEcho(void){
  unsigned int range;
  while(!Echo);              // Wait for echo pin to go high
  TCNT1=0TCCR1B=2;         // 1: 8 prescaler=0.5us
  while(Echo);               // Wait for echo pin to go low
  TCCR1B=8range=TCNT1/116// the range in CM
  return(range);
}
//
void main(void){
  unsigned int range;
  DDRC=1;             // PC0 ouput Trigger,PC1 input Echo
  TCCR1A=0TCCR1B=8// Set timer up in CTC mode
  delay_ms(100); lcd_init(16);
  while(1){
    Trigger=1delay_us(10); Trigger=0// 10uS
    // Wait for the echo line to go high and then measure the length of this high
    range=getEcho();
    lcd_gotoxy(0,0); sprintf(s,"SRF04/SRF05 Test"); lcd_puts(s);
    lcd_gotoxy(1,0); sprintf(s,"Range=%u cm",range); lcd_puts(s);
    delay_ms(100);
  }
}

 

// ==================================================================

 

// DateTime : 2018-05-05 오전 3:26:47
// by Ok-Hyun Park
//
// 5Cm간격으로 C포트 LED 레벨 미터 //
//
#include <mega128.h>
#include <delay.h>
//
//
#define Trigger         PORTC.0 //초음파 트리거
#define Echo            PINC.1  //초음파 에코
//
unsigned int range;
//
void getEcho(void){
  while(!Echo);      // high가 될때까지 대기 //
  TCNT1=0TCCR1B=2// 카운터 시작,8분주=0.5us //
  while(Echo);       // low가 될때까지 대기 //
  TCCR1B=8;          // 카운터 정지 //
  range=TCNT1/116;   // Cm로 변경 //
}
//
void main(void){
  DDRC=1;             // PC0 ouput Trigger,PC1 input Echo
  DDRB=0xFF;          // LED out
  TCCR1A=0TCCR1B=8// CTC mode
  while(1){
    Trigger=1delay_us(10); Trigger=0// 10uS
    getEcho();
    if    (range5){ PORTB=0b00000000; }
    elseif(range<10){ PORTB=0b00000001; }
    elseif(range<15){ PORTB=0b00000010; }
    elseif(range<20){ PORTB=0b00000100; }
    elseif(range<25){ PORTB=0b00001000; }
    elseif(range<30){ PORTB=0b00010000; }
    elseif(range<35){ PORTB=0b00100000; }
    elseif(range<40){ PORTB=0b01000000; }
    else            { PORTB=0b10000000; }
    delay_ms(100);
  }
}

 


// ==================================================================

 

// DateTime : 2018-05-05 오전 3:29:18
// by Ok-Hyun Park
//
// mm 단위로 표시
//
#include <mega128.h>
#include <delay.h>
//
#asm
  .equ __lcd_port=0x1B// lcd포트 PORTA
#endasm
//
#define Trigger         PORTC.0 //초음파 트리거
#define Echo            PINC.1  //초음파 에코
//
char s[21];
unsigned int range_I;
float range_F;
//
voidgetEcho(void){
  while(!Echo);            // Wait for echo pin to go high
  TCNT1=0TCCR1B=2;       // 1: 8 prescaler=0.5us
  while(Echo);             // Wait for echo pin to go low
  TCCR1B=8range_I=TCNT1// the range in CM
  range_F=(float)range_I;
  range_F/=11.6;           // mm
  range_I=(unsignedint)range_F;
}
//
void main(void){
  unsigned int range;
  DDRC=1;             // PC0 ouput Trigger,PC1 input Echo
  TCCR1A=0TCCR1B=8// Set timer up in CTC mode
  delay_ms(100); lcd_init(16);
  while(1){
    Trigger=1delay_us(10); Trigger=0// 10uS
    getEcho();
    lcd_gotoxy(0,0); sprintf(s,"SRF04/SRF05 Test"); lcd_puts(s);
    lcd_gotoxy(1,0); sprintf(s,"%u mm",range_I);    lcd_puts(s);
    delay_ms(100);
  }
}

 


// ==================================================================

 

// DateTime : 2018-05-05 오전 3:33:39
// by Ok-Hyun Park
//
//==================================================================
// LCD 및 USART0 시리얼 전송
//==================================================================
// mm 단위로 표시
//==================================================================
//
#include <mega128.h>
#include <delay.h>
//
#asm
  .equ __lcd_port=0x1B// lcd포트 PORTA
#endasm
//
#define Trigger         PORTC.0 //초음파 트리거
#define Echo            PINC.1  //초음파 에코
//
char s[30];
unsigned int range_I;
float range_F;
//
void TX0_CH(char ch){ while(!(UCSR0A&0x20)); UDR0=ch; } // 송신함수
//
voidgetEcho(void){
  while(!Echo);            // Wait for echo pin to go high
  TCNT1=0TCCR1B=2;       // 1: 8 prescaler=0.5us
  while(Echo);             // Wait for echo pin to go low
  TCCR1B=8range_I=TCNT1// the range in CM
  range_F=(float)range_I;
  range_F/=11.6;           // mm
  range_I=(unsignedint)range_F;
}
//
void main(void){
  unsigned int range,i;
  DDRC=1;                         // PC0 ouput Trigger,PC1 input Echo
  UCSR0B=8UBRR0H=0UBRR0L=103// 9600,송신
  TCCR1A=0TCCR1B=8;             // Set timer up in CTC mode
  delay_ms(100); lcd_init(16);
  while(1){
    Trigger=1delay_us(10); Trigger=0// 10uS
    getEcho();
    lcd_gotoxy(0,0); sprintf(s,"SRF04/SRF05 Test"); lcd_puts(s);
    lcd_gotoxy(1,0); sprintf(s,"%u mm",range_I);    lcd_puts(s);
    i=0while(s[i])TX0_CH(s[i++]);
    delay_ms(100);
  }
}

 

// ==================================================================


SRF05

MODE1 :  MODE핀 OPEN 시키면 SRF04와 같은 모드가 됩니다.

MODE2 :  MODE핀 접지(0V) 시키면 트리거 라인으로 에코 신호가 옵니다.


 

 

 

// DateTime : 2018-05-05 오전 3:36:23
// by Ok-Hyun Park
//
//=================================================
// AVRStudio 128, 16MHz
//=================================================
#include<avr/io.h>
#include <stdio.h>
#define F_CPU 16000000UL  // 16 MHz
#include <util/delay.h>
//
#define Trigger_1       PORTC|=1  //초음파 트리거
#define Trigger_0       PORTC&=~1 //초음파 트리거
#define Echo            (PINC&2)  //초음파 에코
//
unsigned int range;
//
void getEcho(void){
    while(!Echo);            // high가 될때까지 대기 //
    TCNT1=0TCCR1B=2;       // 카운터 시작, 8분주 = 0.5us //
    while(Echo);             // low가 될때까지 대기 //
    TCCR1B=8;                // 카운터 정지 //
    range=TCNT1/116;         // Cm로 변경 //
}
int main(void){
    DDRC=1;                  // PC0 ouput Trigger, PC1 input Echo
    DDRB=0xFF;               // LED out
    TCCR1A=0TCCR1B=8;      // CTC mode
    while(1){     
        Trigger_1_delay_us(10); Trigger_0;    // 10uS
        getEcho();
        if     (range5){ PORTB=0b00000000; }
        else if(range<10){ PORTB=0b00000001; }
        else if(range<15){ PORTB=0b00000010; }
        else if(range<20){ PORTB=0b00000100; }
        else if(range<25){ PORTB=0b00001000; }
        else if(range<30){ PORTB=0b00010000; }
        else if(range<35){ PORTB=0b00100000; }
        else if(range<40){ PORTB=0b01000000; }
        else             { PORTB=0b10000000; }
        _delay_ms(100);
    }
}

댓글 0

조회수 4,770

등록된 댓글이 없습니다.

센서HOME > 센서 > 전체 목록

게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기