Important
This project was part of my coursework. Since the course has ended, it will no longer be updated.
A MicroPython SR04 Ultrasonic Distance Sensor driver.
SR04 not like other similar sensors, SR04 not use any bus protocols. SR04 use pulse time to return result. A typical SR04 Sensor like:
Create an SR04 object. Then use SR04.measure_centimeter()
to get data.
Before measuring, SR04 need to initialize. To initialize properly, SR04 must connect ground first and wait 1 second
after connecting the power. After initializing, give Trig
pin 10μs HIGH pulse to trigger measurement. Then Echo
pin
will set HIGH when received reflected ultrasonic. Measuring Echo
pin's HIGH pulse time to get ultrasonic reflected
back time.
To get distance Echo
pin's pulse time
For example, to get centimeter distance:
SR04's accuracy is 3mm and max measuring distance is 4 meter, so keeping one decimal place is necessary:
For the speed of sound
Here is relationship between speed of sound and common temperature:
Temperature(℃) | Speed of sound(m/s) |
---|---|
-30 | 313 |
-20 | 319 |
-10 | 325 |
0 | 332 |
10 | 338 |
15 | 340 |
20 | 344 |
30 | 349 |
100 | 386 |
To apply compensation, need to give speed of sound param when construct an object:
from library.SR04_driver.sr04_driver import SR04
from machine import Pin
driver = SR04(pin_trig=Pin(14),
pin_echo=Pin(2),
speed_of_sound=344)