Sonntag, 9. Dezember 2012

URS - Ultrasonic Ranging Sensor - Calibration

In order to get more familiar with the HR-SR04 ultrasonic ranging shield (URS) I tried a calibration today, also examining the maximum range. I used the same setup as during my First Experiences work except for a longer metering rule.
For the calibration I put the sensor in various distances from a 25cm wide target (standard physics book) and started recording data. The farthest point I reached was about 420cm, which is about 80 cm less than what the manufacturers manual says. Probably the reason for this is, that my target was too narrow.
I wrote a little python script for data acquisition and took 50 measurement for each position. A ROOT-based macro did the fitting job and here is what it looks like:
This plot shows the distance measured by URS against the actual position determined with the metering rule. The calibration factors now result from a simple linear fit with
$f(x) = p0 + p1 \cdot x$
The actual values are shown in the box within the diagram. As you can see, there is an offset of about 0.6 cm, which may simply grow from the fact that I have no idea where the manufacturer set the point of origin. These calibration factors must now be used within the Arduino program to improve the measurement

Donnerstag, 6. Dezember 2012

URS - Ultrasonic Ranging Sensor

First Experiences


Today I tried to set up a HC-SR04 ranging shield with my Arduino board. The main principle is easy: The sensor emits short ultrasonic pulses at 40 kHz, that are reflected by the obstacle. These reflections are detected by the receiver and the elapsed time is returned. With the knowledge of the sound velocity one can easily calculate the distance between the sensor and the obstacle.


In more detail, the sensor needs a 5V DC power supply to operate. To start a measurement one has to apply a 10us TTL signal to the trigger pin. Then the transmitter generates 8 ultrasonic pulses at 40kHz. After the reciever detectes the echoed pulses, the elapsed time is returned as the pulse width applied to the signal pin.



For my first tries I used the following setup:

The HC-SR04 is supplied with the 5V DC voltage from the Arduino board. The sensors trigger and signal pins are connected to the Arduinos digital pins 13 and 12 respectively. Since this setup is very simple, the Arduino program is rather self-explanatory and is shown below.



Source Code:

 // Define calibration parameters  
 float v = 0.0343;   // velocity of sound at 20 deg celsius in cm / us  
 float c0 = 0.0;     // constant calibration parameter  
 float c1 = 1.0;     // linear calibration parameter  
    
 // Define Pins  
 int pinPing = 13;  
 int pinSignal = 12;  
   
 void setup() {  
  // Setting Pin-Modes  
  pinMode(pinPing, OUTPUT);  
  pinMode(pinSignal, INPUT);  

  // Initialize serial interface at 9600 bauds  
  Serial.begin(9600);  
   
  // Set initial pin states   
  digitalWrite(pinPing, LOW);  
 }  
    
 void loop() {  
  // Declare local fields  
  long duration;  
    
  // Send Ping  
  delayMicroseconds(2);  
  digitalWrite(pinPing, HIGH);  
  delayMicroseconds(10);  
  digitalWrite(pinPing, LOW);  
    
  duration = pulseIn(pinSignal, HIGH);  
  Serial.println(signalToDistance(duration), DEC);  
  delay(1000);  
 }  
   
 float signalToDistance(long duration) {  
  float uncalDist = duration * v / 2.0;  
  float calDist = c1 * uncalDist + c0;  
  return calDist;  
 }  
   


I used a simple book as an obstacle and a metering rule to compare the the actual distance with the one calculated from the board. The boards response is printed to the serial output and can be observed by the Serial Monitor of the Arduino IDE or any application that may read from serial ports. Although the measurement process was pretty stable, it seems that the built-in calibration is not really accurate. For example the book placed in a distance of 50cm (metering rule) generates an output of 40cm. So the next step will be to recalibrate the measurement and implement calibration parameters. The source code above includes this flexibility already.