Menu

sr04 ultra sonic

2018-11-20
2018-11-24
  • stan cartwright

    stan cartwright - 2018-11-20

    Hi all. I want to finish some robot projects and dug out hsr04 ultrasonic rangefinders and code.
    The code I used for a working robot vehicle used include sr04 and not sure how it works.
    I noticed all my sr04 units I've soldered a 2.2k resistor between echo and trig and use just trig to one port.
    The gcb sr04 include sends a ping by turning a port on,wait 10 us,turn pot off,then I don't know.
    I found this link about sr04 and the method is bit different and range is different conversion value.
    https://www.mpja.com/download/hc-sr04_ultrasonic_module_user_guidejohn.pdf last page.
    Some code I'l try. Any thoughts?

    #define ultra portb.0
    dim temp as byte
    dim range as word
    
    dir ultra out
    for temp = 1 to 8
      set ultra on
      wait 25 us
      set ultra off
      wait 25 us
    next
    dir ultra in
    pulsein ultra,range,us
    range=range/58 ;cm
    
     
  • Anobium

    Anobium - 2018-11-23

    The GCB SR04 library works. It is sends a ping and wait for a ping. If supports many devices.

    I do not think I posted any demos as it is very simple to use.

      #chip 16f1939,32
      #config MCLRE_ON, PLLEN_ON,
      #include <srf04.h>
    
      #include <glcd.h>
      #define GLCD_TYPE GLCD_TYPE_KS0108               ' This is the Default value, not required.
    
    
      #define GLCDDirection 1
    
       #define GLCD_CS1 PORTC.1    'D12 to actually since CS1, CS2 can be reversed on some devices
       #define GLCD_CS2 PORTC.0
       #define GLCD_DB0 PORTD.0 'D0 to pin 7 on LCD
       #define GLCD_DB1 PORTD.1 'D1 to pin 8 on LCD
       #define GLCD_DB2 PORTD.2 'D2 to pin 9 on LCD
       #define GLCD_DB3 PORTD.3 'D3 to pin 10 on LCD
       #define GLCD_DB4 PORTD.4 'D4 to pin 11 on LCD
       #define GLCD_DB5 PORTD.5 'D5 to pin 12 on LCD
       #define GLCD_DB6 PORTD.6 'D6 to pin 13 on LCD
       #define GLCD_DB7 PORTD.7 'D7 to pin 14 on LCD
    
       #define GLCD_RS PORTe.0
       #define GLCD_Enable PORTe.2
       #define GLCD_RW PORTe.1
       #define GLCD_RESET PORTC.2
    
       ' Timing for 32 mhz
       #define KS0108ReadDelay    6
       #define KS0108WriteDelay   0
       #define KS0108ClockDelay   0
    
    
    ; ----- Constants
    
        #define US1Ping PORTC.7
        #define US1Echo PORTC.6
    
        #define AlarmDistance 50
    
    ; ----- Variables
        Dim Distance As word
    
    ; ----- Main body of program commences here.
    
        GLCDCLS
        glcdprint 0,   0, "GCBasic 2016"
        glcdprint 0,   8, "SRF04 Demo"
        wait 2 s
    
        Do Forever
    
    
                  'Take a distance reading from the SRF04
                  Distance = USDistance(1)
    
                  'Check the reading, is the object too close to the SRF04?
                  if Distance < AlarmDistance then
                            GLCDPrint ( 0, 24, "Close" )
                            GLCDPrint (0, 32 , str(Distance)+"  " )
                  Else
                            GLCDPrint ( 0, 24, "Far  " )
                            GLCDPrint (0, 32 , "     " )
                  end if
    
                  'Delay for a few milliseconds before taking the next reading
                  Wait 10 ms
        Loop
    
    
    end
    
     
  • Anobium

    Anobium - 2018-11-23

    another demo,

      #chip 16f1939,32
      #config Osc = INT, VCAPEN_OFF, MCLRE_On, PLLEN_ON, CLKOUTEN_OFF, WDTE_SWDTEN
      ;Include files (Libraries)
      #include <srf04.h>
    
    
    
    ; ----- Constants
    
        #define LED1 PORTD.1
        #define LED2 PORTD.2
        #define LED3 PORTD.3
        #define LED4 PORTD.4
        #define LED5 PORTD.5
    
        #define US1Ping PORTC.7
        #define US1Echo PORTC.6
        #define AlarmDistance 50
    
        '7 segment
        #define DISP_SEG_A PORTB.0
        #define DISP_SEG_B PORTB.1
        #define DISP_SEG_C PORTB.2
        #define DISP_SEG_D PORTB.3
        #define DISP_SEG_E PORTB.4
        #define DISP_SEG_F PORTB.5
        #define DISP_SEG_G PORTB.6
    
        #define DISP_SEL_1 PORTA.0
        #define DISP_SEL_2 PORTA.1
    
    
    ; ----- Variables
        Dim Distance As word
    
    ; ----- Main body of program commences here.
    
        Do Forever
    
                  'Take a distance reading from the SRF04
                  Distance = USDistance(1)
    
                  'Check the reading, is the object too close to the SRF04?
                  if Distance < AlarmDistance then
                      Select Case Distance
    
                        case < 10
                          SetWith ( led1, 1 )
                          SetWith ( led2, 0 )
                          SetWith ( led3, 0 )
                          SetWith ( led4, 0 )
                          SetWith ( led5, 0 )
    
                        case < 20
                          SetWith ( led1, 1 )
                          SetWith ( led2, 1 )
                          SetWith ( led3, 0 )
                          SetWith ( led4, 0 )
                          SetWith ( led5, 0 )
    
                        case < 30
                          SetWith ( led1, 1 )
                          SetWith ( led2, 1 )
                          SetWith ( led3, 1 )
                          SetWith ( led4, 0 )
                          SetWith ( led5, 0 )
                        case < 40
                          SetWith ( led1, 1 )
                          SetWith ( led2, 1 )
                          SetWith ( led3, 1 )
                          SetWith ( led4, 1 )
                          SetWith ( led5, 0 )
                        case < 50
                          SetWith ( led1, 1 )
                          SetWith ( led2, 1 )
                          SetWith ( led3, 1 )
                          SetWith ( led4, 1 )
                          SetWith ( led5, 1 )
    
                        case else
    
                          SetWith ( led1, 0 )
                          SetWith ( led2, 0 )
                          SetWith ( led3, 0 )
                          SetWith ( led4, 0 )
                          SetWith ( led5, 0 )
    
                      End Select
    
                      '7 Segment here
    
                      'Get the 2 digits
                      Number = Distance
                      Num1 = 0
                      If Number >= 10 Then
                          Num1 = Number / 10
                          'SysCalcTempX stores remainder after division
                          Number = SysCalcTempX
                      End If
                      Num2 = Number
    
                      'Show the digits
                      'Each DisplayValue will erase the other (multiplexing)
                      'So they must be called often enough that the flickering
                      'cannot be seen.
                      Repeat 100
                          DisplayValue DISP_SEL_1, Num1
                          Wait 1 ms
                          DisplayValue DISP_SEL_2, Num2
                          Wait 1 ms
                      End Repeat
    
                  end if
    
                  'Delay for a few milliseconds before taking the next reading
                  Wait 10 ms
        Loop
    
    
    end
    
     
  • stan cartwright

    stan cartwright - 2018-11-23

    Thanks. There were demos. The ping is one pulse that makes the device oscillate or "ring" for a few cycles at it's resonant frequency but that wasn't explained in the link I posted.
    4 pin devices are more common but if you connect echo and trig via 2K2 resistor you just use the trig pin and the 3 pin sro4 device option. You save a port.

     
    • Anobium

      Anobium - 2018-11-24

      Are you asking? or, telling us?

       
      • stan cartwright

        stan cartwright - 2018-11-24

        The link I posted and others, make it seem you need a 40KHz signal to drive the sr04. It says "8 pulses at 40KHz"... not so as I found with more searching.
        I also found the devices work better upright than horizontal as there's an offset to near objects ie not at a right angle to sr04.

         

Log in to post a comment.