Sahaj Sarup
Published © MIT

Zephyr RTOS and HC-SR04 Ultrasonic Sensor

Running the HC-SR04 ultrasonic sensor on Zephyr RTOS.

IntermediateFull instructions provided30 minutes1,942
Zephyr RTOS and HC-SR04 Ultrasonic Sensor

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Through Hole Resistor, 2.1 kohm
Through Hole Resistor, 2.1 kohm
×1
Carbon
96Boards Carbon
×1

Software apps and online services

Zephyr RTOS
Zephyr Project Zephyr RTOS

Story

Read more

Code

Code snippet #1

Plain text
#include <zephyr.h>
#include <misc/printk.h>
#include <device.h>
#include <gpio.h>
#include <sys_clock.h>
#include <misc/util.h>
#include <limits.h>

#define GPIO_OUT_PIN		1
#define GPIO_INT_PIN		3
#define PORT		"GPIOA"

void main(void)
{
  uint32_t cycles_spent;
  uint32_t nanseconds_spent;
  uint32_t val;
  uint32_t cm;
  uint32_t stop_time;
  uint32_t start_time;
  struct device *dev;
  dev = device_get_binding(PORT);
  gpio_pin_configure(dev, GPIO_OUT_PIN, GPIO_DIR_OUT);
  gpio_pin_configure(dev, GPIO_INT_PIN, (GPIO_DIR_IN | GPIO_INT_EDGE| GPIO_INT_ACTIVE_HIGH | GPIO_INT_DEBOUNCE));

  while (1) {
    gpio_pin_write(dev, GPIO_OUT_PIN, 1);
    k_sleep(K_MSEC(10));
    gpio_pin_write(dev, GPIO_OUT_PIN, 0);
    do {
			gpio_pin_read(dev, GPIO_INT_PIN, &val);
		} while (val == 0);
		start_time = k_cycle_get_32();

		do {
			gpio_pin_read(dev, GPIO_INT_PIN, &val);
      stop_time = k_cycle_get_32();
      cycles_spent = stop_time - start_time;
      if (cycles_spent > 1266720) //260cm for 84MHz (((MAX_RANGE * 58000) / 1000000000) * (CLOCK * 1000000))
      {
        break;
      }
		} while (val == 1);
    nanseconds_spent = SYS_CLOCK_HW_CYCLES_TO_NS(cycles_spent);
    cm = nanseconds_spent / 58000;
    printk("%d\n", cm);
    k_sleep(100);
}
}

Code snippet #2

Plain text
#include <zephyr.h>
#include <misc/printk.h>
#include <device.h>
#include <gpio.h>
#include <sys_clock.h>
#include <misc/util.h>
#include <limits.h>

#define GPIO_OUT_PIN		1
#define GPIO_INT_PIN		3
#define PORT		"GPIOA"

void main(void)
{
  uint32_t cycles_spent;
  uint32_t nanseconds_spent;
  uint32_t val;
  uint32_t cm;
  uint32_t stop_time;
  uint32_t start_time;
  struct device *dev;
  dev = device_get_binding(PORT);
  gpio_pin_configure(dev, GPIO_OUT_PIN, GPIO_DIR_OUT);
  gpio_pin_configure(dev, GPIO_INT_PIN, (GPIO_DIR_IN | GPIO_INT_EDGE| GPIO_INT_ACTIVE_HIGH | GPIO_INT_DEBOUNCE));

  while (1) {
    gpio_pin_write(dev, GPIO_OUT_PIN, 1);
    k_sleep(K_MSEC(10));
    gpio_pin_write(dev, GPIO_OUT_PIN, 0);
    do {
			gpio_pin_read(dev, GPIO_INT_PIN, &val);
		} while (val == 0);
		start_time = k_cycle_get_32();

		do {
			gpio_pin_read(dev, GPIO_INT_PIN, &val);
      stop_time = k_cycle_get_32();
      cycles_spent = stop_time - start_time;
      if (cycles_spent > 1266720) //260cm for 84MHz (((MAX_RANGE * 58000) / 1000000000) * (CLOCK * 1000000))
      {
        break;
      }
		} while (val == 1);
    nanseconds_spent = SYS_CLOCK_HW_CYCLES_TO_NS(cycles_spent);
    cm = nanseconds_spent / 58000;
    printk("%d\n", cm);
    k_sleep(100);
}
}

Code snippet #3

Plain text
#include <zephyr.h>
#include <misc/printk.h>
#include <device.h>
#include <gpio.h>
#include <sys_clock.h>
#include <misc/util.h>
#include <limits.h>

Code snippet #4

Plain text
#include <zephyr.h>
#include <misc/printk.h>
#include <device.h>
#include <gpio.h>
#include <sys_clock.h>
#include <misc/util.h>
#include <limits.h>

Code snippet #7

Plain text
void main(void)
{
uint32_t cycles_spent;
uint32_t nanseconds_spent;
uint32_t val;
uint32_t cm;
uint32_t stop_time;
uint32_t start_time;

Code snippet #8

Plain text
void main(void)
{
uint32_t cycles_spent;
uint32_t nanseconds_spent;
uint32_t val;
uint32_t cm;
uint32_t stop_time;
uint32_t start_time;

Code snippet #15

Plain text
do {
  gpio_pin_read(dev, GPIO_INT_PIN, &val);
stop_time = k_cycle_get_32();
cycles_spent = stop_time - start_time;
if (cycles_spent > 1266720) //260cm for 84MHz (((MAX_RANGE * 58000) / 1000000000) * (CLOCK * 1000000))
{
break;
}
} while (val == 1);

Code snippet #16

Plain text
do {
  gpio_pin_read(dev, GPIO_INT_PIN, &val);
stop_time = k_cycle_get_32();
cycles_spent = stop_time - start_time;
if (cycles_spent > 1266720) //260cm for 84MHz (((MAX_RANGE * 58000) / 1000000000) * (CLOCK * 1000000))
{
break;
}
} while (val == 1);

Gist

https://gist.github.com/ric96/9accdeb6cab34b2557bcf10470a793c3

Credits

Sahaj Sarup

Sahaj Sarup

13 projects • 11 followers
96boards Evangelist

Comments