Ultrasonic Radar With RaayanMini
Ultrasonic Radar With RaayanMini
This project will allow the radar to sweep over a range of angles using the
servo motor, measure distances using the ultrasonic sensor, and display the data on the
16x2 LCD. Let's break the project down into hardware connections and software
development.
Project Overview
Hardware Connections
2. Servo Motor
Software Implementation
We'll use STM32CubeIDE for firmware development. The software needs to handle
several tasks:
SOURCE CODE:
The 16x2 LCD is connected in parallel mode, so you need to initialize the LCD for
8-bit or 4-bit mode (depending on how you connect the data lines). Here’s a basic
initialization for the LCD:
void LCD_Init(void) {
// Set LCD pins as output
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
We will use a simple GPIO toggle to trigger the ultrasonic sensor, followed by a
delay.
void trigger_ultrasonic() {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET); // Set Trigger high
HAL_Delay(10); // 10ms pulse
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET); // Set Trigger low
}
We will use Input Capture mode on PA1 to measure the time it takes for the echo
to return. This method is much more accurate than polling.
uint32_t echo_time = 0;
To control the servo motor, use PWM. We will use PA8 for PWM output.
The servo motor will rotate from 0° to 180° to simulate radar scanning. At each
position, the ultrasonic sensor will take a reading, and the distance will be
displayed on the LCD.
int main(void) {
HAL_Init();
SystemClock_Config();
LCD_Init();
HAL_TIM_Base_Start(&htim1); // Start the timer for PWM and Input Capture
while (1) {
for (int angle = 0; angle <= 180; angle += 15) {
set_servo_angle(angle); // Sweep the servo
trigger_ultrasonic(); // Trigger ultrasonic sensor
HAL_Delay(50); // Wait for sensor to settle
● Verify connections: Double-check the connections between the Raayan Mini, HC-
SR04, servo, and LCD.
● Check power supply: Ensure you have separate power supplies for the servo and
the STM32F401RBT6.
● PWM signal: Adjust the PWM range for the servo to ensure it rotates within the
desired range (0° to 180°).
● LCD display: If the LCD doesn't display correctly, ensure the wiring is proper,
especially the data pins.
Conclusion
Optionally, you can add Wi-Fi functionality using the ESP module (via UART), enabling
you to transmit the radar data remotely or visualize it on a web interface. This is a great
project for learning how to interface with sensors, motors, and displays using STM32.