Landing : Athabascau University
  • Blogs
  • How To Calibrate an SR04 Sonar Distance Sensor with an Arduino

How To Calibrate an SR04 Sonar Distance Sensor with an Arduino

How To Calibrate an SR04 Sonar Distance Sensor with an Arduino

These are the cheapest sonar units on the market -- I got mine for $3.59, but I see them on ebay for half that -- and the Arduino libraries make them a kinch to use. I'm using SparkFun's RedBoard from their Inventor's Kit, pretty much the same thing as the Arduino Uno.

image

  1. Connect the four pins to the Arduino as follows: GND to GND, Echo to #12, Trig to #13, and Vcc to 5V.
  2. Upload the linked code to your board: GitHub.com - sonar_SR04_calibration.ino.
  3. Lay down a ruler so that the zero roughly lines up with the sonar transceivers, and get a book or something similarly flat and wide to use as a reference distance marker. (The inaudible sonar chirps need a wall-like surface to bounce off of.)
  4. Run the code and open the serial monitor. While watching the serial output, place the book at specific distances from the sonar transceiver. Get about 10 readings for each position. Don't spend much time between measurement points as those readings will later all have to be identified and rejected. I went from 5 cm to 20 cm in 5 cm increments because it made my life easier, but you might want to go further. Note that this will require a larger backdrop, as the sonar beam grows with distance.
  5. Import the data into Excel, filter out those readings that came from distances in between your chosen distances. You can overwrite the 'bad' data with #N/A ("=NA()") to stop graphs from reading them (blank cells are interpreted as zeroes). Graph the distances vs. measured delays in a scatter plot and add a linear trendline with the formula showing. That's your calibration formula -- you're done! Here's a sample Excel file: [link].

image

Comments

  • Susanne Cardwell September 20, 2017 - 8:22pm

    Nice work, Tyler. Great data set.  What Excel formula or function yielded the vertical line and the "y=0.146x-2.3843" function?  Or did you enter the data into an Excel chart and automatically yielded the line and function?  Thanks very much.

  • Tyler Lucas September 20, 2017 - 9:39pm

    Hey Susanna. The data was already linear so I just used Excel's 'show trendline' feature in its graphs. Automatic, like you said :)