Time of Flight Sensor Alerter

by VoidCreature in Circuits > Arduino

951 Views, 7 Favorites, 0 Comments

Time of Flight Sensor Alerter

arduino_allsixwires.jpg
IMG_20230502_041340601_HDR.jpg
IMG_20230502_041353769.jpg

This project was heavily inspired by a daily need to navigate in the pitch darkness of my home. I have, because of this practice, gotten very good at navigating my home and even unlocking and locking doors without the need of a light. This Arduino project is meant to be a bigger version of that. Think of it as a kind of sense; instead of navigating the dark through root memorization, you can navigate the dark more like a 6th sense.

Major shoutout to my teacher for helping me with my code.

Some disclaimers:

I did my soldering in my school's lab, which had been built for purposes such as this one. Some photos will reflect that as a result.

Be aware that this project involves soldering, which has a certain danger factor to it. I encourage you to learn soldering from a professional before working on this project if you are not already familiar. I will be going over some basic things to look out for, but remember I am not a professional solderer and I advise caution.

I had a physical teacher teach me how to solder, but if you have access to LinkedIn Learning I recommend Mark de Vinck's class titled "Learning Soldering for Electronics."

Supplies

IMG_20230413_084540751_HDR.jpg
IMG_20230411_090953031.jpg
  • Time of Flight sensor. The type I used is a VL53L0X
  • 6 M-F wires
  • Arduino Uno R3 with matching USB cable
  • Passive Buzzer
  • Soldering Iron
  • Soldering Iron stand
  • Brass sponge
  • Solder
  • Protective eye gear
  • A breadboard
  • If using breadboard, get something thin like a thin soldering iron cleaning sponge
  • A battery (I only had access to a 9V battery)
  • Snap on Connector

For the wearable part of the project:

  • A box (like a hard plastic casing, or a cardboard box of some kind) you don't mind cutting into. make sure it can fit your Arduino and the rest of the connected wires.
  • Tape
  • Craft wire or some kind of similar tie
  • A blade like an exactoknife
  • Wire cutter

Program

  • Arduino IDE

Soldering Safety

IMG_20230413_084855616.jpg
IMG_20230411_090953031.jpg

Since this project involves soldering your parts together, make sure the space you will be soldering in has these safety measures in place:

  1. Well ventilated. Make sure the fumes have somewhere to leave. A garage with a window and/or door open nearby, workshop with ventilation shaft, etc.
  2. Make sure your space has at least a flame extinguisher nearby
  3. When the soldering iron is plugged in and in use, or was very recently in use do not touch the tip of the soldering iron. Make sure it has had time to cool down before touching that end of the iron.
  4. When using the soldering iron, use a soldering iron stand instead of placing it onto your worktable.
  5. If you intend on leaving your work area while you are soldering, make sure to unplug your iron.
  6. Make sure the surface you are working on is at least fire resistant.

Go here for more specifics on soldering safety.

My setup:

I placed my Soldering Iron stand and my copper sponge within close reach. I was also within reach of a sink to quickly help mitigate potential burns from the soldering iron "spitting". If you have vision problems, I recommend getting a magnifying glass for this step. This is tiny work.

Soldering Your ToF Sensor

IMG_20230425_100039814.jpg
IMG_20230413_084540751_HDR.jpg
IMG_20230413_084925593_HDR.jpg
Soldering-TopView.jpg
IMG_20230411_091042812_HDR.jpg

Something I personally find helpful that might be helpful to you is taking some solder wire, and lightly wrapping it around two of my fingers a few times so that there is a flat piece I can keep a steady grip on. If you need to get the wire back to a comfortable length, safely set aside your iron and adjust.

Press the long end of the pins into breadboard so that they're all the way in and even. Then, place the sensor on top of the short end of the pins. They should be lightly poking out.

Place something underneath the sensor to keep it steady while you're working.

With the setup discussed in the previous step, get to soldering. Each dollop should be a shiny little mountain that properly connects the pin to the sensor. It does not have to look pretty, it just needs to work.

Make sure to "season" your soldering iron before working. Place the tip of your solder against the tip of the soldering iron so a dollop of solder sticks onto the iron then quickly wipe it off on the sponge. That should make the iron shiny. Do this regularly before, during, and after the process to maintain your tips.

Connecting It to the Arduino

TOF_GndPwr.jpg
IMG_20230425_092833600_HDR.jpg
TOF_WireDisplay.jpg

The wires I used and what they connect to:

  1. Red = VIN
  2. Black = GND
  3. Green = SCL
  4. Brown = SDA

VIN connects to the power supply, which will be 5V in this case.

GND refers to power ground, which should be right next to the 5V pin on the Arduino.

SCL refers to Serial Clock and connects to the SCL pin on the Arduino

SDA refers to Serial Data and connects to the SDA pin on the Arduino, right next to the SDA pin.


Take four Male-to-Female wires and connect the sensor's pins to the Arduino's pins. Choose whatever colors that make sense to you.

The Passive Buzzer

arduino_buzzerwires.jpg
IMG_20230418_091413371.jpg
arduino_allsixwires.jpg
IMG_20230418_092108904_HDR.jpg

The wires I used and what they connect to:

  1. Blue = GND
  2. Yellow = Pin 8

Connect a pair of your Male-to-Female wires and connect them to your buzzer and your Arduino.

At this point your Arduino should look like the third and fourth photos. (Your wires may not be as curved as mine, though).

Coding: Getting Started

Arduino-Libraries.png
Adafruit-Ref2.png
Adafruit-examples1.png
Adafruit-examples2.png
SelectBoard.png
Arduino_Verify.png
Arduino_Upload.png
MonitorBaud.png
AdafruitTest1.png
AdafruitTest2.png

I have attached the code I am using to provide ease on your end but I will explain my process to you in case you wish to make your own and want a reference on what you can do.

I installed the Adafruit_VL53L0X library that can be found in the Arduino IDE program.

For the buzzer side of things, I had originally used Elegoo's Passive Buzzer code provided as part of a lesson kit as a reference. The issue with this code was that it did not really work with what I was intending to do. The way that code functioned was that it was trying to play a melody using a for loop. What I needed was something along the lines of an if/else statement or (what I ended up using) a "map()".

Reference this when making your code.

To start, go to the icon in the bar at the left that looks like books on a bookshelf. That's the Library Manager. In the search bar, look up "Adafruit VL53L0X" or just "VL53L0X". Either should get you to where you need to go. Click the "Install" button.

Once installed, go to File > Examples > Adafruit_VL53L0X > VL53L0X. That will open the Adafruit code for the VL53L0X type Time of Flight sensor.

First off, plug in your Arduino R3 into your computer to test out your sensor. Select your board and upload the code to your Arduino. Then, select on the Serial Monitor button on the top right of the program screen. Since Adafruit's code on line 6 specifies the number "115200", make sure to go to the rightmost tab in the Serial Monitor and select 115200 baud. Having it on any other setting will result in errors.

The serial monitor should then be printing out lines shown in the final two images. If this test is successful and you do not notice any issues or inconsistencies with your sensor, continue on.

Downloads

Coding: Figuring It Out

Now that you have tested your sensor, this is where we mash our codes together. (This step is simply explaining how I got my first draft of my code, hopefully the logic can help you figure out your code).

Attached is a version of my code that works but has not been fully fleshed out because I am not using for this project. This is the if/then version I mentioned trying out. I should still work as an example.

As stated in the prior step, the second code I originally used did not match with what I needed very well. I will explain my logic in this step to hopefully better inform you of what could help you.


Open up a new file, name it whatever works for you, and copy lines 1 - 24 in Adafruit's code directly. The parts in lines 13 - 19 will be good for monitoring issues with the sensor.

Hypothetically, if you were to find a Passive Buzzer code that fit with what you needed, all you would have to do is copy and paste that into your file correctly and do some minor adjustments. That's not what happened with me, but hopefully you have better luck than me.


Copy line 27 in Adafruit's code and move "Serial.println(measure.RangeMilliMeter);" from line 30 to either directly above or directly below the copied line 27. That will be replacing Adafruit's "Serial.print("Reading a measurement... ");" from line 26. Changing this detail anchors this part of the program to the measurements being made by the sensor.

Then, copy lines 29 to 35, making sure to remove "Serial.println(measure.RangeMilliMeter);" from line 30 since you won't be needing that there.

On line 29, change "if (measure.RangeStatus != 4) {" to  "if (measure.RangeMilliMeter <= 300) {". This way, the if statement matches the measurement listed in the new serial.println, Then, changing the "!=4" to "<=300" provides the program with a range of 300 millimeters to look out for. You can change the numbers on these measurement ranges as you see fit.

Here, lines 30 and on are very different from the original code. It focuses on providing an adequate range for the program to look out for, then telling it what to do during those circumstances, including if the reading is outside the stated range.


Downloads

Coding: What I Ended Up Doing

Reference the code from step 5 to follow along with me.

The difference between this code, and the code I explained to you last step, is that this program is simpler while still acting out what I need of it.

Here, lines 1 through 30 are exactly the same.

The if statement on line 32 is changed to " if (measure.RangeMilliMeter <= 1800) {". My needs for this project do not require that much distance to be detected, and the sensor I got seems to be most accurate around that range. Adjust this number to fit your needs, as this number will act as the maximum distance measured by the program for this entire if/else statement.

On the line after this statement is the phrase " int dist = measure.RangeMilliMeter;". This declares an integer "dist" and assigns the variable, the previously used variable that has been measuring the distance for us.

Line 34 connects that integer dist to a "map()". Map syntax according to this reference:

map(value, fromLow, fromHigh, toLow, toHigh)


Here, because of integer dist, value is connected to "measure.RangeMilliMeter". With the map function, we can take our decided range of distance and have it "translated" into a range of tonal values. So, the syntax would look like this for us:

map(dist, DistanceLow, DIstanceHigh, ToneLow, ToneHigh)

For me, the numbers I ended up using were:

"dist = map(dist, 100, 1500, 50, 1000);"

But you are free to adjust those numbers as you see fit.

Then, "tone(buzzer_pin, 1000 - dist);" is the final thing making that relationship between the map and the noise you want to make. Without it, the map is just the first two variables: "fromLow" and "fromHigh".

Making It Usable

IMG_20230502_030826277.jpg
IMG_20230502_030857632.jpg
IMG_20230502_033550419.jpg
IMG_20230502_035641012_HDR.jpg
IMG_20230502_035701444.jpg
IMG_20230502_035904119.jpg
IMG_20230502_040129783.jpg
IMG_20230502_040138507.jpg
IMG_20230502_040400769.jpg
IMG_20230502_041353769.jpg
IMG_20230502_041340601_HDR.jpg

Time to finally make this a usable accessory.

(Disclaimer: a couple of my M-F wires got bent on the ends during the process of putting this all together so I had to replace them, which is why the color of the wires changed. The wires are still where they need to be).

For this step, it's best to have the sensor disconnected from the rest of the Arduino, but at this point you are free to plug in your snap on battery connector.

The box I managed to find was an old box of 8 packs of extra gum. I found that this was the best for what I needed because it did not uncomfortably bend the wires and I was able to unfold it and tape it back together during the process.

Using your wire cutter, cut off a piece of wire a couple of inches long, have it on standby.

The easiest way to go about this is to carefully separate the sides of the box.

Using your exacto knife and referencing your sensor, cut a hole in your box big enough that the box won't impact any data.

Place the sensor on the inside part of the box so the "seeing" part is facing out of the hold you made. Mark where the screw holes align in relation to the hole you made. Set aside your sensor.

Using your exacto knife, carefully poke a hole in each mark you made. If you mess up and accidentally cut too much (like I did, don't worry) you can use tape to keep the parts together and use the exacto knife to create a smaller hole.

After each hole has been made, place the sensor in the position it was in before. Continue to make sure the seeing part is aligned correctly

Take your wire from earlier and bend it into a symmetrical U-shape. On the inside of the box, have the wires facing out. The ends of the wires should poke through the holes you made, and the flat part of the "U" should be carefully helping keep the sensor in place.

Once in place, reconnect your wires to the Arduino (including the passive buzzer) and position the Arduino so that it fits with the proportions of the box you picked out. Keeping that position in mind, you can try using tape to help keep certain sets of wire in place so that it cannot move around while in use.

While being mindful of the wires and the Arduino, put your box back together.

You should now have a usable Time of Flight sensor alert system!

The Sensor in Action