LAB: Input Capture - Ultrasonic
Date: 2023-09-26
Author/Partner:
Github: repository link
Demo Video: Youtube link
PDF version:
Introduction
In this lab, you are required to create a simple program that uses input capture mode to measure the distance using an ultrasonic distance sensor. The sensor also needs trigger pulses that can be generated by using the timer output.
You must submit
LAB Report (*.pdf & *.md)
Zip source files(main*.c, ecRCC.h, ecGPIO.h, ecSysTick.c etc...).
Only the source files. Do not submit project files
Requirement
Hardware
MCU
NUCLEO-F411RE
Actuator/Sensor/Others:
HC-SR04
breadboard
Software
Keil uVision, CMSIS, EC_HAL library
Problem 1: Create HAL library
Create HAL library
Declare and Define the following functions in your library. You must update your header files located in the directory EC \lib\
.
ecTIM.h
Problem 2: Ultrasonic Distance Sensor (HC-SR04)
The HC-SR04 ultrasonic distance sensor. This economical sensor provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver and a control circuit.
The HC-SR04 Ultrasonic Range Sensor Features:
Input Voltage: 5V
Current Draw: 20mA (Max)
Digital Output: 5V
Digital Output: 0V (Low)
Sensing Angle: 30° Cone
Angle of Effect: 15° Cone
Ultrasonic Frequency: 40kHz
Range: 2cm - 400cm
Procedure
1. Create a new project under the directory \repos\EC\LAB\LAB_Timer_InputCaputre_Ultrasonic
The project name is “LAB_Timer_InputCaputre_Ultrasonic”.
Create a new source file named as “LAB_Timer_InputCaputre_Ultrasonic.c”
You MUST write your name on the source file inside the comment section.
2. Include your updated library in \repos\EC\lib\
to your project.
ecGPIO.h, ecGPIO.c
ecRCC.h, ecRCC.c
ecTIM.h, ecTIM.c
ecPWM.h, ecPWM.c
ecSysTick.h, ecSysTick.c
ecUART_simple.h, ecUART_simple.c
3. Connect the HC-SR04 ultrasonic distance sensor to MCU pins(PA6 - trigger, PB6 - echo), VCC and GND
Measurement of Distance
The program needs to
Generate a trigger pulse as PWM to the sensor.
Receive echo pulses from the ultrasonic sensor
Measure the distance by calculating pulse-width of the echo pulse.
Display measured distance in [cm] on serial monitor of Tera-Term for
(a) 10mm (b) 50mm (c) 100mm
Configuration
System Clock | PWM | Input Capture |
---|---|---|
PLL (84MHz) | PA6 (TIM3_CH1) | PB6 (TIM4_CH1) |
AF, Push-Pull, No Pull-up Pull-down, Fast | AF, No Pull-up Pull-down | |
PWM period: 50msec pulse width: 10usec | Counter Clock : 0.1MHz (10us) TI4 -> IC1 (rising edge) TI4 -> IC2 (falling edge) |
Circuit Diagram
You need to include the circuit diagram
Discussion
There can be an over-capture case, when a new capture interrupt occurs before reading the CCR value. When does it occur and how can you calculate the time span accurately between two captures?
Answer discussion questions
In the tutorial, what is the accuracy when measuring the period of 1Hz square wave? Show your result.
Answer discussion questions
Code
Your code goes here: ADD Code LINK such as github
Explain your source code with necessary comments.
Example Code
Results
Experiment images and results
Show experiment images /results
Add demo video link
Reference
Complete list of all references used (github, blog, paper, etc)
Troubleshooting
(Option) You can write Troubleshooting section
Last updated