stm32f103+HC-SR04+ssd1306 realizes ultrasonic ranging

The homepage of Akinayama coder
oi retired player, dabbled in Java, big data, microcontrollers, and IoT, loves technology, technology is guilt-free
Welcome to followLikeCollectLeave a message
Get the source code and add WX

Directory

  • Preface
  • HC-SR04 explanation
  • code programming
  • at last

Foreword

If you often make some embedded devices, you should be familiar with HC-SR04, a cheap and simple ultrasonic ranging device that can be used in smart cars to measure obstacles, model aircraft, etc. to determine the height. This article explains it briefly and uses an example to illustrate it.

Hardware module:

  1. stm32f103
  2. HC-SR04
  3. oled 0.96 inch display

HC-SR04 explanation


working principle:

  1. Transmitter: The transmitter of the HC-SR04 module emits a series of ultrasonic pulse signals.
  2. Receiver: When an ultrasonic signal encounters an object and is reflected back, the HC-SR04 module’s receiver receives the reflected ultrasonic signal.
  3. Time Measurement: The HC-SR04 module calculates distance by measuring the time difference from sending to receiving the ultrasonic signal.

basic structure:

  1. Transmitter: emits high-frequency ultrasonic pulse signals.
  2. Receiver: Receives the reflected ultrasonic signal.
  3. Control Circuit: Controls the timing of transmission and reception, and calculates distance.
  4. Ultrasonic Sensor: includes transmitter and receiver.

Steps for usage:

  1. Set the pins: Connect the Trig pin of the HC-SR04 module to a GPIO output pin of the microcontroller, and connect the Echo pin to a GPIO input pin of the microcontroller.
  2. Transmit signal: Trigger the module to transmit an ultrasonic signal by setting the Trig pin high for 10 microseconds and then pulling it low again.
  3. Receive signal: Start timing, wait for the Echo pin to change from low level to high level, and record the time.
  4. Calculate distance: Calculate the measured distance based on the relationship between time difference and sound speed.

interface:
VCC supplies 5V power supply, GND is the ground wire, TRIG trigger control signal input, and ECHO echo signal

Timing trigger diagram:

It is not difficult to see that the wake-up is triggered first, and then it automatically transmits 8 40kHz pulses. Use a timer to get the round-trip time, and divide it by 2 to get the distance.

Code programming

#include "stm32f1xx_hal.h"
#include "ssd1306.h"

#define TRIG_PIN GPIO_PIN_0
#define ECHO_PIN GPIO_PIN_1
#define TRIG_PORT GPIOA
#define ECHO_PORT GPIOA

uint32_t distance = 0;
char display_buffer[16];

void SystemClock_Config(void);
void GPIO_Init(void);
void I2C_Init(void);
void Ultrasonic_Init(void);
void Ultrasonic_MeasureDistance(void);
void OLED_DisplayDistance(void);
void HAL_SYSTICK_Callback(void);

int main(void)
{<!-- -->
    HAL_Init();
    SystemClock_Config();
    GPIO_Init();
    I2C_Init();
    Ultrasonic_Init();
    ssd1306_Init();

    while (1)
    {<!-- -->
        Ultrasonic_MeasureDistance();
        OLED_DisplayDistance();
        HAL_Delay(1000);
    }
}

void SystemClock_Config(void)
{<!-- -->
    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_ClkInitTypeDef RCC_ClkInitStruct;

    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    if (HAL_RCC_OscConfig( & amp;RCC_OscInitStruct) != HAL_OK)
    {<!-- -->
        Error_Handler();
    }

    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    if (HAL_RCC_ClockConfig( & amp;RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
    {<!-- -->
        Error_Handler();
    }

    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

    HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

void GPIO_Init(void)
{<!-- -->
    GPIO_InitTypeDef GPIO_InitStruct;

    __HAL_RCC_GPIOA_CLK_ENABLE();

    GPIO_InitStruct.Pin = TRIG_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(TRIG_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = ECHO_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    HAL_GPIO_Init(ECHO_PORT, &GPIO_InitStruct);
}

void I2C_Init(void)
{<!-- -->
    GPIO_InitTypeDef GPIO_InitStruct;

    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_AFIO_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    __HAL_AFIO_REMAP_I2C1_ENABLE();

    __HAL_RCC_I2C1_CLK_ENABLE();

    I2C_HandleTypeDef hi2c1;
    hi2c1.Instance = I2C1;
    hi2c1.Init.ClockSpeed = 400000;
    hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
    hi2c1.Init.OwnAddress1 = 0;
    hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c1.Init.OwnAddress2 = 0;
    hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    if (HAL_I2C_Init( & amp;hi2c1) != HAL_OK)
    {<!-- -->
        Error_Handler();
    }

    __HAL_RCC_GPIOB_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

void Ultrasonic_Init(void)
{<!-- -->
    // Configure ultrasonic sensor
    GPIO_InitTypeDef GPIO_InitStruct;

    __HAL_RCC_GPIOA_CLK_ENABLE();

    GPIO_InitStruct.Pin = TRIG_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(TRIG_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = ECHO_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    HAL_GPIO_Init(ECHO_PORT, &GPIO_InitStruct);
}

void Ultrasonic_MeasureDistance(void)
{<!-- -->
    HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_SET);
    HAL_Delay(10);
    HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET);

    while (HAL_GPIO_ReadPin(ECHO_PORT, ECHO_PIN) == GPIO_PIN_RESET);
    uint32_t start = HAL_GetTick();
    while (HAL_GPIO_ReadPin(ECHO_PORT, ECHO_PIN) == GPIO_PIN_SET);
    uint32_t end = HAL_GetTick();

    distance = (end - start) * 0.034 / 2;
}

void OLED_DisplayDistance(void)
{<!-- -->
    sprintf(display_buffer, "%d cm", distance);

    ssd1306_Fill(Black);
    ssd1306_SetCursor(25, 25);
    ssd1306_WriteString(display_buffer, Font_11x18, White);
    ssd1306_UpdateScreen();
}

void HAL_SYSTICK_Callback(void)
{<!-- -->
    HAL_IncTick();
}

Finally

If this article is helpful to you, please support the blogger three times!
Please add image description