UltrasonicmoduletestwithSTONElcd Module

Introduction: UltrasonicmoduletestwithSTONElcd Module

Ultrasonic module test with STONE lcd module

Introduction to ultrasonic module The principle of ultrasonic distance measurement Ultrasonic wave has strong directivity and the distance spread in the medium is far. Therefore, ultrasonic wave is often used for distance measurement, such as distance measuring instrument and object level measuring instrument, which can be realized through ultrasonic wave. Ultrasonic testing is often more rapid, convenient, simple calculation, easy to achieve real-time control, and in terms of measuring accuracy can meet the requirements of industrial practical, so in the development of mobile robot has been widely used. The principle of ultrasonic ranging is that the ultrasonic wave is emitted by the ultrasonic transmitter, which is based on the time difference when the receiver receives the ultrasonic wave, similar to the principle of radar ranging. Ultrasonic transmitter sends ultrasonic wave to a certain direction, begin timing at the same time when launching, ultrasonic wave spreads in air, encounter obstacle on the way to return immediately, ultrasonic receiver receives reflected wave to stop timing immediately. (the propagation speed of ultrasonic wave in the air is 340m/s. According to the time t (seconds) recorded by the timer, the distance (s) from the launching point to the obstacle can be calculated, that is: s=340t/2).

Step 1: Introduction to Ultrasonic Module

Step 2: HC-SR04 Introduction

Hc-sr04 is an ultrasonic transmitter and receiver module that electronics enthusiasts often use for their own experiments.

This module has stable performance, accurate measurement distance, high precision and small blind area. Main technical parameters:

  • Operating voltage: DC-- -5v
  • Static current: less than 2mA
  • Level output: high 5V
  • Level output: bottom 0V
  • Induction Angle: no more than 15 degrees
  • Detection range: 2cm-450cm
  • High precision: up to 0.2cm

Step 3: HC-SR04 Module Schematic

This picture is the schematic diagram of the hc-sr04 ultrasonic module. If you are interested in the principle of hardware, you can look it up on the Internet.

Wiring: VCC, trig (control terminal), echo (receiver terminal), GND Basic working principle:

(1) IO port TRIG is used to trigger the ranging and give a high-level signal of at least 10us;

(2) the module automatically sends 8 square waves of 40khz to automatically detect whether there is a signal return;

(3) a signal is returned and a high level is an output through the IO port ECHO. The duration of the high level is the time from the transmission to the return of the ultrasonic wave.Test distance =(high level time * sound velocity (340M/S))/2;

This module USES method is simple, the mouth sends a high level of more than ten us, you can receive the mouth for high-level output. To be able to open timer timing, an output when the mouth into low electricity at ordinary times the value of the timer can be read, this is for the distance of time can calculate the distance. So constantly cycle test, which can reach your measuring values.

Step 4: Application Scenarios

Robot obstacle avoidance

The object distance

Liquid level detection

The public security

Parking lot survey

Step 5: Test Hc-sr04 With MCU


STM32F103RCT6 is very powerful, the following is the basic parameters of this MCU:
Series: STM32F10X

Kernel: ARM - COTEX32 Speed: 72 MHZ Communication

interface: CAN, I2C, IrDA, LIN, SPI, UART/USART, USB

Peripheral equipment: DMA, motor control PWM, PDR, POR, PVD, PWM, temperature sensor, WDT

Program storage capacity: 256KB

Program memory type: FLASH

RAM capacity: 48K

Voltage - power supply (Vcc/Vdd) : 2 V ~ 3.6 V

Oscillator: internal

Operating temperature: -40°C ~ 85°C

Package/enclosure: 64-lqfp

In this project, I will use UART, GPIO, Watch Dog, Timer of STM32F103RCT6. The following is the code development record for this project. STM32 USES Keil MDK software development, about this software you must be familiar with, so I will not introduce the installation method of this software.

Step 6: ​STM32

STM32 can be simulated and debugged online through j-link or st-link and other simulation tools. The following picture is the development board of STM32 I use:

Step 7: Display Test Results Via Serial Assistant

The trig and echo ports are configured to low-level output, output of more than 10 us trig pin high level pulse (module automatically send out eight 40 k square wave), then wait, capture the output echo rise along, capture the rising edge at the same time, start the timer start timing, wait again to catch the falling edge of the echo, when capture the falling edge, read the timer time, this is the running time of ultrasound in the air, in accordance with the S = (high level of time * (340 m/S)) / 2 can compute the distance of ultrasonic to the obstacles.

With this in mind, we can write simple test code.

We can see the measurement results returned by the serial port.

Step 8: Introduction to STONE TFT-LCD Display Module

The LCD module is STONE STVC050WT-01, which is very convenient to use. Previously, we used a serial port to display the distance measured by ultrasonic wave, while the LCD module of STONE also receives data and displays it to the module through UART. This process only needs to do a simple protocol encapsulation in the MCU.


Step 9: 5 Inch 4:3 480*272 Tft-lcd Introduction

STVC050WT-01 is a TFT display and touch controller. It includes a processor, control program, driver, flash memory, RS232/RS485/TTL port, touch screen, power supply, etc. It is a powerful display system

The operating system is simple and can be controlled by any single-chip microcomputer.Stvc050wt-01 can be used to perform all basic functions, such as text display, image display, curve display, touch function, video and audio function, etc. The user interface can be much richer and more diverse. Flash memory can store your data, configuration files, pictures, etc.

  • built-in Cortex CPU and driver
  • can be controlled by any single-chip microcomputer
  • show pictures/text/curves
  • 65536 color TFT display
  • with/without a touch screen
  • RS232/ RS485/ TTL UART interface and USB port
  • wide voltage range
  • easy to use. Powerful features. Cost and time-saving.

Step 10: The Working Principle of STONE TFT-LCD

The intelligent tft-lcd module communicates with the customer's MCU through commands (hexadecimal code), and the MCU then controls the connected device to work according to the commands received.

Step 11: 3 Steps to Develop STONE TFT-LCD

Use STONE's tft-lcd module in only 3 steps:

1. Design a beautiful set of graphical user interfaces.

2. Connect directly to the customer's MCU through RS232, RS485 or TTL.Plug and play.

3. Write a simple program to control the tft-lcd module by MCU command.(hexadecimal code). TFT LCD module serial command frame consists of 5 data blocks, all serial command or data are expressed in hexadecimal format.The data transfer

In the MSB way.For example, for 0x1234, first send 0x12, then 0x34.

Step 12: 9 Common Application Scenarios of STONE TFT-LCD

Common application scenarios are described in STONE's official website:

Step 13: STONE Displays Are Widely Used in Various Industrial Fields

  • Medical cosmetology equipment
  • Construction machinery and vehicle equipment
  • Electronic instruments
  • Industrial control system
  • The power industry
  • Civil electronic equipment
  • Automation equipment
  • The traffic

Step 14: About the Use of STONE TOOL Software

At present, the latest version of TOOL software provided by STONE is TOOL2019. Open the software to create a new project, then import the previously designed UI display pictures, and then add your own buttons, text display boxes, etc. STONE's official website has a very complete tutorial on the use of this software. Interested friends can click on the following link:


Step 15: UI Graphic Design of LCD Module

You can use Photoshop or other image design software to set the UI interface. The interface I designed is as follows:

Step 16: Use TOOL2019 Software to Generate LCD Module Configuration Files

Click the button indicated by the arrow to generate the configuration file, and then download the configuration file into the display module to display the UI interface we designed.

This part of the content and tutorial I do not go into detail, you can go to STONE website to download a very detailed tutorial.

Step 17: STM32 LCD Project Implementation Process With UART TTL

The functions of this demo are relatively simple, mainly including the following two points:

Display the distance measured by the ultrasonic module Change the refresh time through the "+" and "-" buttons on the display

Reliability of LCD project hardware connections

Hardware wiring is very simple, you can refer to the following picture of the connection:

STM32 microcontroller program for Stone TFT-LCD
STM32 microcontroller drive hc-sr04 program can be downloaded on the network, here I also use the user provided demo routine to modify.

Step 18: Peripheral Resource Usage

MCU peripherals only use two GPIO pins (trig, echo) and a Timer.In addition, a UART is required to communicate with the STONE displayer.

Trig and echo ports are configured to low-level output, first of all to trig output at least 10 us high level pulse (module automatically send out eight 40 k square wave), then wait, capture the output echo rise along, capture the rising edge at the same time, open the timer and start timing, wait again to catch the falling edge of the echo, when capture the falling edge, read the timer time, this is the running time of ultrasound in the air, in accordance with the S = (high level of time * (340 m/S)) / 2 can compute the distance of ultrasonic to the obstacles.

Step 19: Hc-sr04 Algorithm Source Code

void EXTI9_5_IRQHandler(void) { static u8 flag_Sta = 0; if(EXTI_GetITStatus(EXTI_Line5) != RESET) { EXTI_ClearITPendingBit(EXTI_Line5); if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==1) { TIM_SetCounter(TIM6,0); flag_Sta=1; TIM_Cmd(TIM6, ENABLE); } else { TIM_Cmd(TIM6, DISABLE); if(flag_Sta) { Distance = TIM_GetCounter(TIM6); Distance = Distance /29; if(Distance > 300) Distance = 300; Done = 1; } flag_Sta=0; } } }

Step 20: This Is an External Interrupted Handler of GPIO. Finally, the Distance Measured by the Ultrasonic Module Can Be Obtained by Obtaining the Value of the Variable “distance”.

#include "usart1.h" #include "Sonic.h" #include "SysTick.h" #define DISPLYER_ADDR 0x05 #define CHANGE_TIME_ADDR 0x10 u8 data_send[8]= {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x00, 0x00,0x00}; extern u16 Distance; extern u8 USART_RX_BUF[10]; extern u8 USART_RX_END; extern u16 USART_RX_STA; void UART1_Send_Array(u8 send_array[],unsigned char num) { u8 i=0; while(i=time_set) { time=0; data_send[5]=DISPLYER_ADDR; data_send[6] = Distance >> 8;//hight data_send[7] = Distance & 0x00ff;//low UART1_Send_Array(data_send,8); // printf("The Distance is: %d centimetre\r\n",Distance); } if(USART_RX_END) { // UART1_Send_Array(USART_RX_BUF,8); switch (USART_RX_BUF[5]) { case CHANGE_TIME_ADDR: time_set=USART_RX_BUF[7]*10; break; default: USART_RX_END=0; USART_RX_STA=0; break; } USART_RX_END=0; USART_RX_STA=0; } } } /******************* (C) COPYRIGHT 2017 *****END OF FILE************/

Step 21: Sonic.c

#include "Sonic.h" /******************************************************************************* * Sonic Init *******************************************************************************/ u32 Distance = 0; u8 Done; u32 __IO time_1ms = 0; void TIM6_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //NVIC_InitTypeDef NVIC_InitStructure; /* TIM6 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 0xFFFF; TIM_TimeBaseStructure.TIM_Prescaler = 142; //144£¬500K TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); TIM_ITConfig(TIM6, TIM_IT_Update, DISABLE); TIM_Cmd(TIM6, DISABLE); } void Sonic_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO,ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //PC4 Trig GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPD; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5; //PC5 Echo GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_WriteBit(GPIOC,GPIO_Pin_4,(BitAction)0); //trig //EXTI_DeInit(); EXTI_ClearITPendingBit(EXTI_Line5); GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource5); EXTI_InitStructure.EXTI_Line= EXTI_Line5; EXTI_InitStructure.EXTI_Mode= EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger= EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_LineCmd=ENABLE; EXTI_Init(&EXTI_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); Distance = 0; Done = 1; } void Sonic_Trig(void) { u16 i = 0; if((Done == 1)&&(time_1ms > 100)) { time_1ms = 0; GPIO_WriteBit(GPIOC,GPIO_Pin_4,(BitAction)1); for(i=0;i<0xf0;i++); GPIO_WriteBit(GPIOC,GPIO_Pin_4,(BitAction)0); Done = 0; } } void EXTI9_5_IRQHandler(void) { static u8 flag_Sta = 0; if(EXTI_GetITStatus(EXTI_Line5) != RESET) { EXTI_ClearITPendingBit(EXTI_Line5); if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==1) { TIM_SetCounter(TIM6,0); flag_Sta=1; TIM_Cmd(TIM6, ENABLE); } else { TIM_Cmd(TIM6, DISABLE); if(flag_Sta) { Distance = TIM_GetCounter(TIM6); Distance = Distance /29; if(Distance > 300) Distance = 300; Done = 1; } flag_Sta=0; } } } /******************* (C) COPYRIGHT 2017 *END OF FILE************/

Step 22: Uart

#include "usart1.h" void USART1_Config(uint32_t uBaud) { USART1_Configuration(uBaud); USART1_NVIC_Configuration(); } void USART1_Configuration(uint32_t uBaud) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); /* USART1 GPIO config */ /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART1 mode config */ USART_InitStructure.USART_BaudRate = uBaud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No ; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_Cmd(USART1, ENABLE); } void USART1_NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); /* Enable the USARTy Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } int fputc(int ch, FILE *f) { USART_SendData(USART1, (unsigned char) ch); while (!(USART1->SR & USART_FLAG_TXE)); //while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET); return (ch); } u16 USART_RX_STA=0; u8 USART_RX_END=0; u8 USART_RX_BUF[10]; void USART1_IRQHandler(void) { u8 Res; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { Res =USART_ReceiveData(USART1); //printf("%x",USART_ReceiveData(USART1)); // USART_SendData(USART1,Res); if(USART_RX_END==0) { USART_RX_BUF[USART_RX_STA]=Res ; USART_RX_STA++; if(USART_RX_STA>=8) { USART_RX_END=1; } } } }

Step 23: Sys_tick

void SysTick_Handler(void) { time_1ms++; time_120ms++; if(time_120ms>=80) { Sonic_Trig(); //50ms Trig Sonic time_120ms=0; } TimingDelay_Decrement(); }

Step 24: STONE Touch Screen Operation Effect

In the case of no error in hardware connection, download the code into the STM32 development board, and the distance measured by the ultrasonic module can be seen in the STONE display screen. In addition, the distance refresh time can be adjusted through the "+" and "-" buttons above the motor display module.

The article source:


Be the First to Share


    • Furniture Contest

      Furniture Contest
    • Arduino Contest

      Arduino Contest
    • Make it Real Student Design Challenge #3

      Make it Real Student Design Challenge #3