Metro Board Ultrasonic Range Finder

Project Requirements

Design and Fabrication

  • Design and fabricate a case using 3D printing, the laser cutters, or whatever else you prefer. (Training for 3D Printing – Basic Additive and above and Laser cutter – Fab Lab South)
  • The case should include openings for the ultrasonic sensor, LEDs, and USB cable access

Arduino File for Ultrasonic Range Finder

  • Download and unzip the provided Arduino file for the Ultrasonic Range Finder.
  • The file contains essential code to control the ultrasonic sensor and LEDs.
  • Once unzipped, open the file in the Arduino IDE and verify the code for errors.
  • Make adjustments if required for specific project needs or equipment variations.

Click on each section below to learn about this project. Happy building!

Colorful thread.

Introduction

 This project will demonstrate how to use an HC-SR04 ultrasonic sensor with an Adafruit Metro microcontroller to create a distance measurement tool that provides feedback through LEDs. Initially conceived for a smart walking stick, this setup replaces an auditory feedback system (buzzer) with visual feedback. This setup is especially suited for scenarios requiring quiet operation or for visual learners.

Step 1: Gathering Materials

Before starting, gather all items listed below.

  • Arduino Uno R3: The main processing unit. Adafruit Metro microcontroller can be used as an alternative.
  • USB-A to Micro USB Cable: Used to connect the Metro to a computer for uploading code and serial monitoring.
  • Small Breadboard: For prototyping and connecting components.
  • HC-SR04 Ultrasonic Sensor: Measures distance using sound waves.
  • 4 LEDs (Red, Yellow, Green, Blue): Indicate different distance ranges.
  • 4 Current-Limiting Resistors (220 Ohm, ¼ W rating, 5% tolerance): To prevent LEDs from drawing excessive current.
  • 16 Jumper Wires: Used for making connections between the components on the breadboard and the Metro.
  • Access to Arduino Software (Arduino IDE): Required for programming the Metro.

 

Verify that each component is functional:

  • Adafruit Metro Test Code: Provide a simple “Blink” sketch to confirm the Metro connects and executes code properly.
  • Connect the Metro to the computer with the USB-A to Micro USB cable.
  • Open the Arduino IDE, select the correct board and port under Tools > Board and Tools > Port, and upload the code.
  • The LED on the Metro should blink if the upload is successful, confirming the Metro board works.
  • Here’s the recommended code:

  • HC-SR04 Ultrasonic Sensor Test Code: Provide the following code to test the sensor independently by observing distance readings on the Serial Monitor.
  • Connect the sensor’s VCC to 5V, GND to GND, Trigger pin to Pin 9, and Echo pin to Pin 10 on the Metro.
  • Upload the code, open the Serial Monitor (baud rate 9600), and observe the distance readings as objects are placed at various distances in front of the sensor.
  • Here’s the recommended code:

Step 2: Familiarize Yourself with Certain Electronic Components

It is important to familiarize yourself with electronic components for successful assembly of this project. If you have taken the EE/IoT Advanced Training, you may skip over this section. The below points go over the specifics of certain components:

  • HC-SR04 Ultrasonic Sensor:
  • Operates by emitting sound waves and timing the echo return.
  • The Trigger Pin sends out the pulse, while the Echo Pin listens for the return pulse.
  • The sensor measures distance up to approximately 400 cm with reasonable accuracy.
  • The formula used is based on the speed of sound: Distance = (pulse duration / 2) / 29.1.
  • LEDs:
  • Each LED will represent a specific distance range detected by the sensor. Distances closer to the sensor will light up more LEDs.
  • LEDcolor can aid in differentiating proximity visually.
  • Resistor: These limit the amount of current going through a circuit. Current is the flow of electrons through a circuit, like water flowing through a pipe. Resistors are necessary to protect components from receiving too much current, like LEDs.
  • LEDs: Light-emitting diodes, or LEDs, emit light when current is passed through them. They are DIRECTIONAL, meaning that the way they are inserted into a circuit matters. LEDs have two metal connectors on the bottom known as leads. The longer lead is connected to the positive-most portion of the circuit (like the positive side of a battery). The shorter lead is connected to the negative-most portion. As mentioned, LEDs need resistors to limit the amount of current going through them. Too much current will burn out the LED.
  • Breadboard: A prototyping board, or a breadboard, is used to build circuits without the need for soldering (that is, binding components to a board through a metal compound known as solder). On the breadboard, there are two sets of side rails. The side rails connect all components in the same vertical column. The center rails connect all components in the same horizontal row. Components in the same row, but on opposite sides of the central gap, are NOT connected.

Below is a view of the breadboard used in the project and then the completed circuit for reference.

Step 3: Assemble the Circuit

  1. Connect the HC-SR04 Sensor: Connect the Trigger Pin to Pin 10 on the Adafruit Metro and the Echo Pin to Pin 11
  2.  Set up the LEDs: Connect each LED to a unique pin on the Adafruit Metro, specifically Pins 5, 6, 7, and 8. Connect a 220 Ω resistor in series with each LED to prevent excessive current.
  3. Complete the Connections: Ensure all components are securely connected to the breadboard and that jumpers are correctly placed for consistent power flow

All components will be connected to the breadboard. The Adafruit Metro will be the “brains” as it will provide power to the circuit, take in information from the ultrasonic sensor, and output data in the form of turning on the LEDs. Below is a schematic of all connections in the circuit:

It is recommended to use the side rails as the 5V power (usually to red) and ground (usually to blue/black) as this is common practice in the electrical engineering world. The ground in the above schematic (labeled as three horizontal lines) is connected to the ground of the Adafruit Metro board (labeled as GND).

Step 4: Programing the Circuit

Programming Tips

  • Pin Configuration: Define the pins for the Trigger and Echo connections for the sensor, as well as each LED.
  • setup() Function: Configure each pin as either input or output and initialize serial communication for debugging.
  • loop() Function: Continuously send a pulse from the Trigger Pin, measure the return time from the Echo Pin, calculate the distance, and determine which LEDs to light up based on the calculated range.
  • Testing and Debugging: Use the Serial Monitor in the Arduino IDE to observe real-time distance measurements for debugging

Now that the physical assembly has been completed, we can program our Adafruit Metro board to function how we want it to. Follow these steps to set up and implement the program:

  1. Connect the Adafruit Metro to your computer using a USB-A to Micro USB cable. This connection will allow you to upload code and monitor the Serial output.
  2. Find and launch the Arduino IDE. This is where we will do all our programming.
  3. Once launched, it should open to a new file. If not, create a new file.
  4. Unfortunately, the default setup of the Arduino IDE is for the Arduino Uno and NOT for the Adafruit Metro. Steps 5 through 12 will go over the setup.
  5. Go to File -> Preferences
  6. Input this URL in the small text box near the bottom of the pop-up window: https://adafruit.github.io/arduino-board-index/package_adafruit_index.json. Click OK/Done
  7. Go to Tools -> Board: ” ” -> Boards Manager
  8. Go to Type “All” -> Type “Contributed” (dropdown menu)
  9. Install “Adafruit AVR Boards.”
  10. If the Metro is unresponsive or LEDs do not behave as expected after uploading the code, a restart may be necessary. To restart, unplug the Metro’s USB cable, wait a few seconds, and reconnect it to the computer.
  11. Go back to Tools -> Board: ” ” -> Adafruit Boards -> Adafruit Metro
  12. Now, go to Tools -> Port -> COMxx (your device should already be connected to the computer) (it is usually not COM1). Click the COMxx
  13. The setup is complete! Rather than copying and pasting the code, open the provided code file in the Arduino IDE by navigating to File > Open and selecting the file. This reduces potential errors.
  14. Upload the code to the Adafruit Metro board by selecting the correct board and port in the Arduino IDE, then clicking the Upload button. Once uploaded, the Metro will begin executing the code, activating the proximity detection and LED indicators.

Remember, you should be using Arduino IDE for all programming steps, from opening the code file to uploading it to the board.

Your program should now be working with your circuit! You can test this by putting your hand in front of the ultrasonic sensor and slowly moving it closer or farther from the sensor. As you do, you should see some lights turn on or off depending on the distance! If you encounter any issues, check your connections, and check your code. Sometimes there will be a small error that could cause the circuit not to work!

Reference Pictures

 

 

 

 

 

Testing and Calibration

  1. Distance Calibration: Use a ruler to validate the sensor’s distance readings. Adjust the code if necessary for accuracy.
  2. LED Threshold Testing: Confirm each LED lights up at the designated range. If any LEDs light incorrectly, adjust the distance thresholds in the code.
  3. Serial Output Verification: Check for accurate distance values on the Serial Monitor. Troubleshoot any fluctuations or inconsistencies by reviewing connections or code.

Conclusion and References

This project combines an ultrasonic sensor and LEDs to provide a visual proximity indicator. Its modular nature allows for easy adaptation into applications such as robotics, obstacle detection, and assistive devices. By modifying the code and using alternative sensors or outputs, this setup can be extended for diverse practical applications.

References

  1.  SparkFun Electronics, “HC-SR04 Ultrasonic Sensor Datasheet”
  2. Arduino, “pinMode() Reference”
  3.  Naufuu, “Smart Arduino Walking Stick for the Visually Impaired,” Instructables