Working with Hc-sr04 Ultrasonic Distance Sensor
Ultrasonic is a high frequency sound (typically 40KHz is used). A short burst of wave ( often 8 cycles) is sent out by the speaker. The microphone listens for an echo. The time taken is recorded. Using calculation L = C X T/2
L : Length
C: Speed of sound in air (344m/s at 20 degrees celcius)
T : time taken is divided by 2
The distance is determined.
Product features:
Ultrasonic ranging module HC - SR04 provides 2cm - 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The modules includes ultrasonic transmitters, receiver and control circuit. The basic principle of work:
(1) Using IO trigger for at least 10us high level signal
(2) The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back.
(3) IF the signal back, through high level , time of high output IO duration is the time from sending ultrasonic to returning.
Test distance = (high level time×velocity of sound (340M/S) / 2
Wire connecting direct as following:
5V Supply
Trigger Pulse Input
Echo Pulse Output
0V Ground
Download
NewPing Library
Put the "NewPing" folder in "libraries\".
In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sktech->Import Library->NewPing".
Download the SerialMonitor lib for serial monitorhttps://www.arduino.cc/en/Reference/Serial
Program code - Arduino
#include
#define TRIGGER_PIN 12;
#define ECHO_PIN 11;
#define MAX_DISTANCE 200;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(115200);
}
void loop() {
delay(50);
unsigned int uS = sonar.ping();
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println("cm");
}
2. Plug in FTDI and check Serial Monitor in Arduino IDE
To connect the Sonar I bent the pins so they won't connect to the routing behind it.
After programming the Attiny45 succesully, for some reason the arduino code did not work, the serial monitor did not recieve input, only a strange character appeared..
Went on to to try the process using the classes C code and Make file.
Installing AVRDude and running the Python to visualize
To visualize the input an additional Python code (from the class site) was used. To execute the Python code, used the shell
python hello.HC-SR04.py /dev/tty.usbserial-AL01TQBL
Decided to make an 'Hello Arduino' board, thinking its a good investment to try different sensors withour re-milling and re-soldering at every problem
Designed a board based on the classes Hello Arduino Board (embedded programming week page)
This new board is using the ATMega
The architecture of this microcontroller is alot more condensed. It has many “free” pins to be used for connecting sensors and motors, for my project. The only problem with this is that soldering is going to be exceedingly difficult because of the super-fine routes.
Milling was also difficult. To mill with the modela I had to experiment with the offset and tool width.
Changes the definitions to 1 offset of the 1/64 mill, and 0.03 tool width, until the paths seemed to be OK in the preview image..