PSoC 5LP HC-SR04 Distance measurement/Duty Cycle measeurement

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
CaDu_3933941
Level 4
Level 4
50 replies posted 25 replies posted 10 replies posted

Hi All,

I am currently trying to implement a HC-SR04 ultrasonic distance sensor with a PSoC 5LP. I understand that I am supposed to determine the pulse width of the Echo signal, however I just cannot seem to understand how to do this in a PSoC 5LP.

I see many designs that use a counter and an ISR but do not say much as of where the information is stored.

Thanks

0 Likes
1 Solution
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

CaDu,

direct search on Cypress community forums returns multiple HC-SR04 projects, e.g.

Distancing Measuring with HC-SR04 For CY8CKIT-059 PSoC® 5LP

which can serve as a starting point

/odissey1

View solution in original post

0 Likes
3 Replies
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

CaDu,

direct search on Cypress community forums returns multiple HC-SR04 projects, e.g.

Distancing Measuring with HC-SR04 For CY8CKIT-059 PSoC® 5LP

which can serve as a starting point

/odissey1

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Although I could write a sample for PSoC 4, PSoC 5 LP version was not as easy for me.

Mainly because I tried to use hardware trigger and isr this time.

Anyway, following is my first working version of HC-SR04 sample for CY8CKIT-059.

000-Schematic.JPG

main.c

=========================

/* ========================================

* HC-SR04 test program for CY8CKIT-059

* For this appliation, VDD must be set to 5V

* ========================================

*/

#include "project.h"

#include <stdio.h>

/* https://keisan.casio.jp/exec/system/1231998943 */

#define MACH    346.513     /* Mach 1.0 at 25.0 */

volatile uint16_t duration = 0 ;

volatile int      echo_flag = 0 ;

char str[128] ; /* print buffer */

CY_ISR(echo_isr)

{

    isr_echo_int_ClearPending() ;

    isr_echo_int_Disable() ;

    duration = Counter_ReadCounter() ;

    echo_flag = 1 ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    Trigger_Write(0) ;

    Clock_24MHz_Start() ;

    UART_Start() ;

    isr_echo_int_ClearPending() ;

    isr_echo_int_StartEx(echo_isr) ;

    Counter_Init() ;

}

/**

* pulse_trigger

* Generate 10us high pulse from trigger pin

*/

void pulse_trigger(void)

{

    Counter_Reset_Write(1) ; // reset counter

    CyDelayUs(10) ;

    Counter_Reset_Write(0) ; // release reset counter

    isr_echo_int_ClearPending() ;

    isr_echo_int_Enable() ;

   

    Trigger_Write(1) ;  // trigger pulse HIGH

    CyDelayUs(10) ;

    Trigger_Write(0) ; // trigger pulse LOW

   

    Counter_Enable() ;

}

/**

* print_value

* Calculate distance from the duration

* Since the duration include both way of the trip

* to get the distance, the real duration is the half

* of the duration.

*

* distance = duration * MACH(m/s) * 100(cm) / (2 * 12000000(Hz)) ;

*

*/

void print_value(uint32_t duration)

{

    double distance = 0.0 ;

   

    distance = (double)(duration) * MACH / 240000.0 ;

    sprintf(str, "%d.%02dcm\n", (int)distance, (int)(100 * distance)%100) ;

    UART_PutString(str) ;

}

void splash(void)

{

    sprintf(str, "HC-SR04 Test CY8CKIT-059 (%s %s)\n", __DATE__, __TIME__) ;

    UART_PutString(str) ;

}

int main(void)

{

    uint16_t timeout_count = 0 ;

   

    init_hardware() ;

   

    splash() ;

    for(;;) {

        timeout_count = 0 ;

        pulse_trigger() ;

        while((echo_flag == 0)&&(timeout_count < 1000)) {

            timeout_count++ ;

            CyDelay(1) ;

        }

        if (echo_flag) {

            echo_flag = 0 ;

            print_value(duration) ;

        } else {

//            uncomment following line to report timeout           

//            UART_PutString("Timeout\n") ;

        }

        CyDelay(1000) ;

    }

}

=========================

Tera Term Log

001-TeraTerm-log.JPG

moto

0 Likes
song_285461
Level 3
Level 3
First like received

Hello you, I have a project about Psoc 5LP and SR04 (SR05). It run very well, fast and smooth. Not use delay, I use a timer counter and interrupt GPIO both: rising and falling for measure lever hight on Echo Pin return from SR04 (SR05).
I hope it will help you.

Hướng dẫn học lập trình Psoc 5lp và ứng dụng 5: SR05 - YouTube