1. Home
  2. Docs
  3. VSDSquadron Mini DataShee...
  4. Projects
  5. Object Detector Using Ultrasonic Sensor

Object Detector Using Ultrasonic Sensor

1. Hardware

  • CH32V003 RISC-V processor
  • Ultrasonic sensor (HC-SR04)
  • LED
  • Power Supply
  • Breadboard
  • Jumper Wires

2. Software

  • VSCode
  • PlatformIO
  • TRIG PIN to PD2 on VSDSquadron Mini
  • ECHO PIN to PD4 on VSDSquadron Mini
  • GND to GND on VSDSquadron Mini
  • VCC to 3.3V on VSDSquadron Mini
  1. Install PlatformIO Core : Ensure PlatformIO Core is installed on your system. Follow the installation guide provided by PlatformIO.
  2. Build and Upload Commands :
    • Build the project: $ pio run
    • Upload the firmware: $ pio run –target upload
    • Clean build files: $ pio run –target clean
  • USART_Printf_Init() : Initializes the USART peripheral for debugging and output.
  • Delay_Ms() : Generates a millisecond delay, useful for timing and sensor control.
  • GPIO_ReadInputDataBit() : Reads the state of an input pin.
  • GPIO_WriteBit() : Sets or clears a specific output pin, used for controlling the LED and the ultrasonic sensor’s trigger.
#include <ch32v00x.h>

#include <debug.h>

void GPIO_Config(void) {

GPIO_InitTypeDef GPIO_InitStructure = {0};

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

// Configure echo and trigger pins

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_4;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_Init(GPIOD, &GPIO_InitStructure);

// Configure trigger and LED pins

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOD, &GPIO_InitStructure);

}

int main(void) {

uint8_t echo_status = 0;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

SystemCoreClockUpdate();

Delay_Init();

GPIO_Config();

while(1) {

// Trigger ultrasonic sensor

GPIO_WriteBit(GPIOD, GPIO_Pin_2, SET);

Delay_Ms(10); // Trigger pulse width

GPIO_WriteBit(GPIOD, GPIO_Pin_2, RESET);

echo_status = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_4);

if (echo_status == 1) {

// Object detected, blink LED

GPIO_WriteBit(GPIOD, GPIO_Pin_3, SET);

Delay_Ms(2000); // LED on duration

GPIO_WriteBit(GPIOD, GPIO_Pin_3, RESET);

Delay_Ms(2000); // LED off duration

}

}

}
  • Object Detection : Useful in parking assistance systems, robot obstacle avoidance, and proximity detection for home automation.
  • Security Systems : Employing ultrasonic sensors for motion detection in restricted areas.
  • Distance Measurement : Accurate distance measurements for assembly line spacing, liquid level monitoring, and height measurement of objects.
  • Liquid Level Detection : Using ultrasonic sensors to monitor the liquid level inside tanks, preventing unstable readings caused by wavy surfaces or bubbles.

https://drive.google.com/file/d/1cq8VzAWV3-8p111bpxCW_VXBy3182G0E/view?usp=sharing

https://github.com/Amanb17/somaiya-riscv/