How to use ultrasonic sensor with WICED studio and CYW943907AEVAL1F?

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

cross mob
Anonymous
Not applicable

Hey everyone,

I am trying to use ultrasonic sensor, HC-SR04, with the microcontroller. The problem that I am having is using HC-SR04 with WICED studio. I have connected them to the board, echo and trig are connected to J12.4 and J12.3. Voltage is connected to J9.5 and GND is connected to J9.6.

I am trying to send a trigger for 10useconds but it is not possible. I have the echo connected to Salaea logic analyzer to collect the duty cycle output. But the trigger is not being sent. The logic analyzer displays "Waiting for trigger".

My trigger is sent when a button on the board is clicked. Here is the code:

#include "wiced.h"

#define TRIG_HIGH_TIME (0.01) //0.01 mSec = 10 uSec

volatile wiced_bool_t newPress = WICED_FALSE;

void echo_measure(void* arg) {

}

/* Interrupt service routine for the button */

void button_isr(void* arg) {

/* let trigger pin settle */

wiced_gpio_output_low(WICED_PWM_2);

wiced_rtos_delay_milliseconds(10);

/* Send trigger from button click */

wiced_gpio_output_high(WICED_PWM_2);

wiced_rtos_delay_milliseconds(0.01);

wiced_gpio_output_low(WICED_PWM_2);

newPress = WICED_TRUE;

}

void application_start( )

{

uint8_t pressCount = 0;

char    printChar;

wiced_init();  

/* Initialize the WICED device */

wiced_gpio_input_irq_enable(WICED_BUTTON2, IRQ_TRIGGER_FALLING_EDGE, button_isr, NULL);

/* Setup interrupt */

const wiced_uart_config_t uart_config = {

     .baud_rate    = 115200,

     .data_width   = DATA_WIDTH_8BIT,

     .parity       = NO_PARITY,

     .stop_bits    = STOP_BITS_1,

     .flow_control = FLOW_CONTROL_DISABLED,

};

      wiced_uart_init( WICED_UART_1, &uart_config, NULL);

     /* Setup UART */

     while (1 ) {

          if(newPress) {

               pressCount ++;    

               /* Increment counter */

               if(pressCount > 9) {

                    pressCount = 0;

               }

               printChar = pressCount + 0x30;

               wiced_uart_transmit_bytes(WICED_UART_1, &printChar , 1);

               newPress = WICED_FALSE; /* Reset for next press */

         }

     }

}   

0 Likes
1 Solution

You shouldn't have this line commented out:

//wiced_gpio_input_irq_enable(WICED_GPIO_31, IRQ_TRIGGER_FALLING_EDGE, echo_isr, duration);

The problem is that this "if" statement is not a blocking call:

if(wiced_gpio_input_get(WICED_GPIO_31) == WICED_FALSE) {

Meaning, calling wiced_gpio_input_get takes a snapshot of the pin state at that precise moment in time, it isn't going to wait for the pin state to change. So, unless you are incredibly lucky or choose to poll for the input to change in a tight (no other code contained) while loop, you are going to miss the pin state transition. Triggering an interrupt on the pin state changing and then taking the echo_high_end_time in the ISR is the better implementation.

View solution in original post

3 Replies
DavidC_76
Employee
Employee
First like received

Please replace:

wiced_rtos_delay_milliseconds(0.01);

with:

wiced_rtos_delay_microseconds(10);

You can't pass a floating point number to a function which take uint32_t as an argument, this should have generated a compiler warning.

Anonymous
Not applicable

Thanks for the response.

I have updated my code to send a trigger and get the amount of time the echo signal is high.

I am following this arduino code: Ultrasonic Sensor HC-SR04 and Arduino Tutorial

However, I am failing to replicate the functionality of pulseIn present in the Arduino (Arduino Reference: pulseIn).

Below is the code that I have written. I am only getting the "echo is starting outside" output on UART.

#include <stdlib.h>

#include "wiced.h"

#define TRIG_HIGH_TIME (0.01) //0.01 mSec = 10 uSec

volatile wiced_bool_t newPress = WICED_FALSE;

unsigned long duration;

void echo_isr(void* arg) {

     // not being used for now

}

void application_start( ) {

    wiced_init();   /* Initialize the WICED device */

    //wiced_gpio_input_irq_enable(WICED_BUTTON2, IRQ_TRIGGER_FALLING_EDGE, button_isr, NULL); /* Setup interrupt */

    //wiced_gpio_input_irq_enable(WICED_GPIO_31, IRQ_TRIGGER_FALLING_EDGE, echo_isr, duration);

    while (1 )

    {

        // DURATION VARIABLES

        wiced_time_t echo_high_start_time;

        wiced_time_t echo_high_end_time;

        // TRIGGER CODE

        /* let trigger pin settle */

        wiced_gpio_output_low(WICED_GPIO_7);

        wiced_rtos_delay_microseconds(3);

        /* Send trigger from button click */

        wiced_gpio_output_high(WICED_GPIO_7);

        wiced_rtos_delay_microseconds(10);

        wiced_gpio_output_low(WICED_GPIO_7);

        /* Start echo time */

        WPRINT_APP_INFO(("\necho is starting outside"));

        if(wiced_gpio_input_get(WICED_GPIO_31) == WICED_FALSE) {

            WPRINT_APP_INFO(("\necho is starting"));

            wiced_time_get_time(&echo_high_start_time);

        }

        if(wiced_gpio_input_get(WICED_GPIO_31) == WICED_FALSE) {

            WPRINT_APP_INFO(("\necho has ended"));

            wiced_time_get_time(&echo_high_end_time);

            WPRINT_APP_INFO(("\necho signal lasted for %lu microseconds \n", (unsigned long)(echo_high_end_time - echo_high_start_time)));

        }

        wiced_rtos_delay_milliseconds(1000);

    }

}

0 Likes

You shouldn't have this line commented out:

//wiced_gpio_input_irq_enable(WICED_GPIO_31, IRQ_TRIGGER_FALLING_EDGE, echo_isr, duration);

The problem is that this "if" statement is not a blocking call:

if(wiced_gpio_input_get(WICED_GPIO_31) == WICED_FALSE) {

Meaning, calling wiced_gpio_input_get takes a snapshot of the pin state at that precise moment in time, it isn't going to wait for the pin state to change. So, unless you are incredibly lucky or choose to poll for the input to change in a tight (no other code contained) while loop, you are going to miss the pin state transition. Triggering an interrupt on the pin state changing and then taking the echo_high_end_time in the ISR is the better implementation.