Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
Dear sir/madam,
I am quite excited to present to you my final submission for this module! This project is what took me probably the longest due to the multiple moving pieces involved, but it was also probably the most interesting and fun for me. I have setup my simulation like so:
- Trigger the HCSR04 with an activation pulse and while its getting its reading in, setup Timer 1's input capture mode to generate an interrupt on the rising edge (ECHO pulled high).
- Once the rising edge interrupt is triggered, switch to the falling edge (because we need a complete ECHO high -> ECHO low read to accurately understand our object's distance) and reset the timer counter (the reason for this is explained in the code; basically it's easier to handle).
- Once the falling edge interrupt is triggered, directly manipulate the contents of the ICR and print the calculated distance to an attached LCD.
I had multiple O3 sessions for this project specifically due to the synchronisation required between the LCD, the microcontroller clock and the sensor generating interrupts, but thanks to the tremendous efforts of the TSEs, I was able to get my simulation working as accurately as I would like! I would like to bring something to your attention though:
While the sensor readings increase linearly with the distance between the observed sensor waveform, going below a certain threshold causes the readings to loop in on themselves. This is especially visible if you increase the potentiometer from lowest to the highest, then back to the lowest - the first quadrant, when re-entered from the maximal edge, should cause an errant loop starting from the maximum distance value back to the minimum. I am unsure of why this occurs, but the majority of the algorithm and circuit is as accurate as I could have possibly made it.
Please note that I have also used the following directory structure for my submission with Makefiles:
Project2/project2-hcsr04.simu
Project2/code/Makefile (the Makefile should have a first line comment indicating its expected location)
Project2/code/project2-hcsr04.c
Project2/code/project2-hcsr04.hex
I am quite eager to hear your feedback,
Best regards,
Rahul Singh