Categories
TUTORIALS

Surfacing Mapping with Ultrasonic Sensors (and R)

Purpose

This tutorial walks through using a set of ultrasonic sensors to develop a surface map (full disclosure: the final graphic mapping is done in R). This tutorial assumes a gridded layout of sensors. We will use four ultrasonic sensors in the example, with assumed modularity (additional notes below). Let us know if you’ve expanded the system, and what additional modifications you made!

Components

  • 1 Arduino Uno
  • 4 Ultrasonic Sensors (HC-SR04)
  • 19 M-M wires
  • 1 Breadboard

Overview

The HC-SR04 ultrasonic sensor can detect objects with a distance range of 2-400cm, using ultrasonic transmitters, receiver, and a control circuit. You’ll see in the user manual and on your sensor four pins: power (VCC), ground (GND), echo, and trigger (trig). From the user manual (link here): “to start measurement, Trig of SR04 must receive a pulse of high (5V) for at least 10 [microseconds], this will initiate the sensor will transmit out 8 cycle of ultrasonic burst at 40kHz and wait for the reflected ultrasonic burst. When the sensor detected ultrasonic from receiver, it will set the Echo pin to high (5V) and delay for a period (width) which proportion to distance. To obtain the distance, measure the width (Ton) of Echo pin.”

We will use the HCSR04 library (link here) in our code, which includes functions for calculating distance. This will keep our code clean, and allow us to focus instead on comparing distance readings across sensors.

Hardware Setup

Each sensor requires both a 5V and ground connection. To keep wires clean, we’ll connect all four sensors to the same grounding, power, and trigger pin, but each sensor will need a separate echo pin.

Wiring schematic

Code

In the first version, we just want to make sure everything is wired correctly, and we’ve imported the library correctly. Let’s run intit_DistanceSensors. This code is an adaptation of code provided as an example with the HCSR04 library.

Once that’s up and running, we can see that there’s a lot of noise in the readings – even if we test it against a flat and unmoving sensor. There’s some variation in the readings – the specs for the ultrasonic sensors gives a range of 0.3cm. To deal with this, let’s add a moving average distance across five readings. Let’s run moveAvg_DistanceSensors.

In our last version of the code for the Arduino, we’re going to clean things up a little to allow for post-processing in R. Here, we’re modifying our outputs so the serial monitor print-out can easily be saved as a csv. Our columns will be: x-axis sensor position; y-axis sensor position; value (distance from sensor); and units (always a good practice!). Let’s run serialToCSV_DistanceSensors.

Bonus

To plot surfaces in R, you can pull the csv you save from the serial monitor output. We used plotly in our sample!

Sources

  • HCRS04 Library: https://github.com/gamegine/HCSR04-ultrasonic-sensor-lib
  • Elegoo Super Starter Kit for UNO: Lesson 10 Ultrasonic Sensor Module
  • Plotly: https://plotly.com/r/

Leave a Reply

Your email address will not be published. Required fields are marked *