Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Measuring distance with ultrasonic sensor HC-SR04 (Oshonsoft Basic Compiler).

DogFlu66

Member
This time I leave a function to measure distance with the HC-SR04 ultrasonic sensor.
The code is commented line by line.

Sensor:
HC-SR04.JPG
LCD_RC-RS04.jpg
RS232_RC-RS04.jpg


Datasheet:

Code:
'**********************************************************************************
'Fucntion for distance measurement with the HC-SR04 sensor with TMR1
'PicEBasic board, By COS, 25/05/2023
'OshonSoft Pic Basic Compiler (PSI), v8.42, Pic16F88
'Pin Echo (in) SR04 = RB4
'Pin Trigger (out) SR04 = RB1
'Pin Led (out) reading indicator = RA4
'**********************************************************************************
Define CONF_WORD = 0x2f50  'Fuse settings.
Define CONF_WORD_2 = 0x3ffc
Define CLOCK_FREQUENCY = 8  '8Mhz oscillator.
Define SINGLE_DECIMAL_PLACES = 1
'Define SIMULATION_WAITMS_VALUE = 1  'Activate for simulation.
'----------------------------------------------------------------------------------
'Librarys
Include "_ProcSetUpEBasic.bas"
Include "_FuncionesPic16F88.bas"
'Include "_FuncionPromedio.bas"
'Include "_FuncionReadHC-SR04_TMR1.bas"
'----------------------------------------------------------------------------------
Call _setup_oscillator_mode_select_bit(_oscillator_mode_defined_by_fosc)
Call _setup_oscillator(_osc_8mhz)
'----------------------------------------------------------------------------------
Call _setupebasic()  'Initializes the PicEBasic header board.
Hseropen 4800  'Initializes the RS232 serial port.
Lcdinit  'Inicializa el LCD sin cursor
Lcdout "Test HC-SR04"  'Print the text on the LCD
Hserout CrLf, CrLf, "Test HC-SR04", CrLf  'Send the text to the RS232 port
Hserout "------------", CrLf  'Send the text to the RS232 port
'----------------------------------------------------------------------------------
main:
    Const Mhz = 8  'System clock in megahertz.
    Const Cm = 0  'Centimeters
    Const In = 1  'Inches
    Const Samples = 5  'Number of samples
    Call _SetUp_HCSR04()  'Set Timer1 for use with sensor
    Call _Promedio(_GetHCSR04(Mhz, Cm), 0)  'Initialize the function
    'Call _Promedio(_gethcsr04(Mhz, In), 0)  'Initialize the function

    Dim Distance As Single  'Read
    Distance = 0
    WaitMs 100  'Pause of 100mSec.

    'Loop
    While True
        'sensor reading
        _ledsr04 = True  'Led to On
        Distance = _Promedio(_GetHCSR04(Mhz, Cm), Samples)  'Average sensor reading
        'Distance = _Promedio(_gethcsr04(Mhz, In), Samples)'Average sensor reading
        _ledsr04 = False  'Led to Off

        'Shows reading
        Lcdcmdout LcdLine2Home  'Cursor to the beginning of line 2
        Lcdout "SR04:", #Distance, "Cm  "  'Display the distance on the LCD
        'Lcdout "SR04:", #Distance, "In.  "  'Display the distance on the LCD

        Hserout "HC-SR04: ", #Distance, "Cm  ", Cr  'Data output through the RR232
        'Hserout "HC-SR04: ", #Distance, "In.  ", Cr  'Data output through the RS232
    
    Wend
End                                           

'************************************************************************
'Include "_FunctionReadHC-SR04_TMR1.bas"
'Functions for the control of the HC-SR04 sensor
'PicEBasic board, By COS, 28/05/2023
'Basic Pic Simulator IDE (PSI), v8.42, Pic16F88
'Use Tmer1
'Pin Echo (In) of SR04 = RB4
'Pin Trigger (Out) of SR04 = RB1
'************************************************************************
'Configure Tmr1
Proc _SetUp_HCSR04()
    'configure Tmr1
    Call _setup_timer1(_tmr1_internal, _tmr1_div1)  'Set up timer1
    Call _timer1(_on)
End Proc                                     
'**************************************************************************
'Function for reading the HC-SR04 sensor, returns real value
'Returns a reading in Cm or In from the HC-SR04 sensor
'_GetHCSR04 = returns the sensor reading in Cm or In.
'_Mhz: system clock speed
'Read = _GetHCSR04(_Mhz)
'_GetHCSR04 > 0 read in Cm and if _GetHCSR04= 0 read error
Symbol _Pin_Trigger = RB1
Symbol _Pin_Echo = RB4
'Const Cm = 0
'Const In = 1
Function _GetHCSR04(_Mhz As Byte, _System As Byte) As Single

    ConfigPin _Pin_Trigger = Output  'Trigger pin as output
    ConfigPin _Pin_Echo = Input  'Echo pin as input
    Dim _Aux As Single
    Dim _Distance As Single
    Dim _Ramp As Single
    _Ramp = 1  'Cm
    If _System = 1 Then _Ramp = 0.4  'In.
    _Aux = 0
    _GetHCSR04 = 0  'Default read error is marked

    'MCU Request a reading from the sensor
    _Pin_Trigger = True  'Start Trigger pulse
    WaitUs 10  'Width of the trigger pulse in uSeg. (10uSec. or greater)
    _Pin_Trigger = False  'End of Trigger pulse

    'MCU waits until the sensor has the reading
    Call _clear_timer1()  'The Timer counter is cleared
    While _Pin_Echo = False And _tmr1if = 0  'Wait until the sensor finishes sending the 8 pulses
    Wend
    If _tmr1if = True Then Exit  'End if Timer overflows (read error).

    'MCU read sensor reading
    Call _clear_timer1()  'The Timer counter is cleared
    While _Pin_Echo = True And _tmr1if = 0  'Wait until the sensor finishes sending the reading
    Wend
    If _tmr1if = True Then Exit  'End if Timer overflows (read error).

    'Reading in Cm
    _GetHCSR04 = _get_timer1() / (_Mhz / 4) * 0.01715  'Cm
    'reading in inches
    If _System = 1 Then _GetHCSR04 = _GetHCSR04 * 0.39  'In.

    'Damper reading
    If _Distance > _GetHCSR04 Then _Aux = _Distance - _GetHCSR04
    If _Distance < _GetHCSR04 Then _Aux = _GetHCSR04 - _Distance
    If _Aux >= _Ramp Then
        _Distance = _GetHCSR04
    Else
        If _Distance < _GetHCSR04 Then
            _Distance = _Distance + 0.001
    Else
        If _Distance > _GetHCSR04 Then
            _Distance = _Distance - 0.001
    Endif
    Endif
    Endif

    'Return value
    _GetHCSR04 = _Distance

End Function                                 

'**************************************************************************
'Include "_FuncionPromedio.bas"
'Arithmetic mean function.
'OshonSoft Pic Basic Compiler (PSI), v8.42, Pic16F88
'By COS, 25/05/2023
'**************************************************************************
'Returns a real number.
'_Sample: Input value.
'_Nsamples: Number of samples.
'Call _Promedio(x, 0)  'Initialize la function.
Function _Promedio(_Sample As Single, _Nsamples As Byte) As Single
    Dim _Media As Single  'Auxiliar variable
    Dim _Index As Byte  'Sample index
    'Initialize the function when the number of elemenst = 0 (_Nsamples = 0)
    If _Nsamples = 0 Then
        _Media = 0  'Initial value
        _Index = 0  'Initial value
        _Promedio = _Sample  'Initial value
        Exit  'Return
    Endif
    'Calculate mean
    _Media = _Media + _Sample  'Accumulate value samples
    _Index++  'Number of samples
    If _Index >= _Nsamples Then  'Control number of samples
        _Promedio = _Media / _Nsamples  'Return the arithmetic mean.
        _Index = 0  'Initial value
        _Media = 0  'Initial value
    Endif
End Function
 

Attachments

  • _FuncionesPic16F88.bas
    26.2 KB · Views: 167
  • Example Reading Lectura HC-SR04 TMR1.bas
    6.5 KB · Views: 180
  • _ProcSetUpEBasic.bas
    2 KB · Views: 172
Last edited:
This example is for Pic and there is also a version for AVR but I don't use it.

6502 Simulator IDE (The current version is 1.26)

Z80 Simulator IDE (The current version is 12.17)

8085 Simulator IDE (The current version is 5.16)

OshonSoft AVR Basic Compiler (The current version is 3.43)

OshonSoft PIC Basic Compiler (The current version is 8.43)

OshonSoft PIC16 Basic Compiler (The current version is 2.43)

OshonSoft PIC18 Basic Compiler (The current version is 4.43)

OshonSoft PIC10 Basic Compiler (The current version is 3.43)
 
This time I leave a function to measure distance with the HC-SR04 ultrasonic sensor.
The code is commented line by line.

Sensor:
View attachment 141663View attachment 141664View attachment 141665

Datasheet:

Code:
'**********************************************************************************
'Fucntion for distance measurement with the HC-SR04 sensor with TMR1
'PicEBasic board, By COS, 25/05/2023
'OshonSoft Pic Basic Compiler (PSI), v8.42, Pic16F88
'Pin Echo (in) SR04 = RB4
'Pin Trigger (out) SR04 = RB1
'Pin Led (out) reading indicator = RA4
'**********************************************************************************
Define CONF_WORD = 0x2f50  'Fuse settings.
Define CONF_WORD_2 = 0x3ffc
Define CLOCK_FREQUENCY = 8  '8Mhz oscillator.
Define SINGLE_DECIMAL_PLACES = 1
'Define SIMULATION_WAITMS_VALUE = 1  'Activate for simulation.
'----------------------------------------------------------------------------------
'Librarys
Include "_ProcSetUpEBasic.bas"
Include "_FuncionesPic16F88.bas"
'Include "_FuncionPromedio.bas"
'Include "_FuncionReadHC-SR04_TMR1.bas"
'----------------------------------------------------------------------------------
Call _setup_oscillator_mode_select_bit(_oscillator_mode_defined_by_fosc)
Call _setup_oscillator(_osc_8mhz)
'----------------------------------------------------------------------------------
Call _setupebasic()  'Initializes the PicEBasic header board.
Hseropen 4800  'Initializes the RS232 serial port.
Lcdinit  'Inicializa el LCD sin cursor
Lcdout "Test HC-SR04"  'Print the text on the LCD
Hserout CrLf, CrLf, "Test HC-SR04", CrLf  'Send the text to the RS232 port
Hserout "------------", CrLf  'Send the text to the RS232 port
'----------------------------------------------------------------------------------
main:
    Const Mhz = 8  'System clock in megahertz.
    Const Cm = 0  'Centimeters
    Const In = 1  'Inches
    Const Samples = 5  'Number of samples
    Call _SetUp_HCSR04()  'Set Timer1 for use with sensor
    Call _Promedio(_GetHCSR04(Mhz, Cm), 0)  'Initialize the function
    'Call _Promedio(_gethcsr04(Mhz, In), 0)  'Initialize the function

    Dim Distance As Single  'Read
    Distance = 0
    WaitMs 100  'Pause of 100mSec.

    'Loop
    While True
        'sensor reading
        _ledsr04 = True  'Led to On
        Distance = _Promedio(_GetHCSR04(Mhz, Cm), Samples)  'Average sensor reading
        'Distance = _Promedio(_gethcsr04(Mhz, In), Samples)'Average sensor reading
        _ledsr04 = False  'Led to Off

        'Shows reading
        Lcdcmdout LcdLine2Home  'Cursor to the beginning of line 2
        Lcdout "SR04:", #Distance, "Cm  "  'Display the distance on the LCD
        'Lcdout "SR04:", #Distance, "In.  "  'Display the distance on the LCD

        Hserout "HC-SR04: ", #Distance, "Cm  ", Cr  'Data output through the RR232
        'Hserout "HC-SR04: ", #Distance, "In.  ", Cr  'Data output through the RS232
   
    Wend
End                                          

'************************************************************************
'Include "_FunctionReadHC-SR04_TMR1.bas"
'Functions for the control of the HC-SR04 sensor
'PicEBasic board, By COS, 28/05/2023
'Basic Pic Simulator IDE (PSI), v8.42, Pic16F88
'Use Tmer1
'Pin Echo (In) of SR04 = RB4
'Pin Trigger (Out) of SR04 = RB1
'************************************************************************
'Configure Tmr1
Proc _SetUp_HCSR04()
    'configure Tmr1
    Call _setup_timer1(_tmr1_internal, _tmr1_div1)  'Set up timer1
    Call _timer1(_on)
End Proc                                    
'**************************************************************************
'Function for reading the HC-SR04 sensor, returns real value
'Returns a reading in Cm or In from the HC-SR04 sensor
'_GetHCSR04 = returns the sensor reading in Cm or In.
'_Mhz: system clock speed
'Read = _GetHCSR04(_Mhz)
'_GetHCSR04 > 0 read in Cm and if _GetHCSR04= 0 read error
Symbol _Pin_Trigger = RB1
Symbol _Pin_Echo = RB4
'Const Cm = 0
'Const In = 1
Function _GetHCSR04(_Mhz As Byte, _System As Byte) As Single

    ConfigPin _Pin_Trigger = Output  'Trigger pin as output
    ConfigPin _Pin_Echo = Input  'Echo pin as input
    Dim _Aux As Single
    Dim _Distance As Single
    Dim _Ramp As Single
    _Ramp = 1  'Cm
    If _System = 1 Then _Ramp = 0.4  'In.
    _Aux = 0
    _GetHCSR04 = 0  'Default read error is marked

    'MCU Request a reading from the sensor
    _Pin_Trigger = True  'Start Trigger pulse
    WaitUs 10  'Width of the trigger pulse in uSeg. (10uSec. or greater)
    _Pin_Trigger = False  'End of Trigger pulse

    'MCU waits until the sensor has the reading
    Call _clear_timer1()  'The Timer counter is cleared
    While _Pin_Echo = False And _tmr1if = 0  'Wait until the sensor finishes sending the 8 pulses
    Wend
    If _tmr1if = True Then Exit  'End if Timer overflows (read error).

    'MCU read sensor reading
    Call _clear_timer1()  'The Timer counter is cleared
    While _Pin_Echo = True And _tmr1if = 0  'Wait until the sensor finishes sending the reading
    Wend
    If _tmr1if = True Then Exit  'End if Timer overflows (read error).

    'Reading in Cm
    _GetHCSR04 = _get_timer1() / (_Mhz / 4) * 0.01715  'Cm
    'reading in inches
    If _System = 1 Then _GetHCSR04 = _GetHCSR04 * 0.39  'In.

    'Damper reading
    If _Distance > _GetHCSR04 Then _Aux = _Distance - _GetHCSR04
    If _Distance < _GetHCSR04 Then _Aux = _GetHCSR04 - _Distance
    If _Aux >= _Ramp Then
        _Distance = _GetHCSR04
    Else
        If _Distance < _GetHCSR04 Then
            _Distance = _Distance + 0.001
    Else
        If _Distance > _GetHCSR04 Then
            _Distance = _Distance - 0.001
    Endif
    Endif
    Endif

    'Return value
    _GetHCSR04 = _Distance

End Function                                

'**************************************************************************
'Include "_FuncionPromedio.bas"
'Arithmetic mean function.
'OshonSoft Pic Basic Compiler (PSI), v8.42, Pic16F88
'By COS, 25/05/2023
'**************************************************************************
'Returns a real number.
'_Sample: Input value.
'_Nsamples: Number of samples.
'Call _Promedio(x, 0)  'Initialize la function.
Function _Promedio(_Sample As Single, _Nsamples As Byte) As Single
    Dim _Media As Single  'Auxiliar variable
    Dim _Index As Byte  'Sample index
    'Initialize the function when the number of elemenst = 0 (_Nsamples = 0)
    If _Nsamples = 0 Then
        _Media = 0  'Initial value
        _Index = 0  'Initial value
        _Promedio = _Sample  'Initial value
        Exit  'Return
    Endif
    'Calculate mean
    _Media = _Media + _Sample  'Accumulate value samples
    _Index++  'Number of samples
    If _Index >= _Nsamples Then  'Control number of samples
        _Promedio = _Media / _Nsamples  'Return the arithmetic mean.
        _Index = 0  'Initial value
        _Media = 0  'Initial value
    Endif
End Function
Hi D,
An interesting thread.
I use Oshonsoft BASIC exclusively, and have some of those sensors.
I'm busy at the moment, but I'll be interested to see how it goes.
Camerart.
 

Latest threads

Back
Top