Speed Detector Using Arduino Uno
by JohnMelton20 in Circuits > Arduino
91 Views, 0 Favorites, 0 Comments
Speed Detector Using Arduino Uno
The speed detector does exactly as the name implies. It detects the speed that objects travel by detecting a change in distance traveled in a given time as it passes by the IR proximity sensors one before the other. This is not completely accurate due to the assumption of constant velocity, but because of the short distance between the two sensors it tends to be pretty close. These same kinds of sensors can be used for things like drag racing times and testing muzzle velocity of firearms. We will also display our velocity on a small OLCD screen. This will allow us to display given velocity times in order to function in a small package, as long as it is provided with a power source. Velocity is something that is not super easy to just measure either, so being able to get the velocity of something in SI units is also helpful when trying to calculate other things like acceleration and kinetic energy.
Supplies
Things you will need:
1x Arduino Uno
11x Jumper Wire (you may need more or less then I did depending on your connection set up and whether or not you solder to a perf board.)
1x 9v Battery (if you'd like to run it independently to your computer)
Why This Specifically
I wanted to make something that is portable and functional, because I want to build skills by doing things that could be directly useful to me. Making something that allows me to measure values I can't measure normally seemed like it could be helpful later in life. This project was also a good way to get myself more acquainted with Arduino code (something I am admittingly not an expert on). I was going to do another 555 timer project similar to the Morse Keyer that I made before. However, I decided I'd rather focus on working on a new skill. I also like all things that move fast. I am a bit of a geek when it comes to fire arm construction, and I've been to my fair share of drag races (for cars). So finding new ways to measure velocity of something, and to understand how some of the machines used to measure the velocity of something that fast work, was immediately interesting to me.
Assembly
- All lines are separate wires that do not intersect if they cross.
- All lines that go to the same power source or ground, will most likely need to be connected by either soldering them together and attaching them to the same port, or by using a breadboard to attach them and then use a third wire to connect them that way.
- All red lines go to a power source, all blue/black lines go to ground, and the rest go to their corresponding "A" port.
- If you attach this to a bread board like I did, just be aware of what attaches to what. Each line on a breadboard is numbered and ports that are directly next to one another, and are in the same numbered row, are attached to one another so anything that is connected to that row will allow current to flow through.
- If you decide to solder this to a perf board, and make it a permanent more portable tool you will need to be able to melt the solder to the perf board and let it harden on your wire in each hole. Once you have all of your wires set you can attach more solder between each hole to connect your circuit. Be careful to not allow solder to connect anything you don't want connected. If this accidentally happens then you can swipe your hot iron on the solder you want to remove to "cut" it. While you are melting solder also try to move your soldering wire out of the way before your hot iron so nothing gets stuck. Go here for a video by Electronhacks if you need visualization or more in depth help.
Assembly Instructions:
- First attach your OLCD screen.
- VCC on the screen to 5V on the Arduino uno
- GND on the screen to GND on the Arduino uno
- SCL on the screen to A5 on the Arduino uno
- SDA on the screen to A4 on the Arduino uno
- (Note multiple components will have to attach to ground and 5V you will have to attach the wires together with solder or by using a breadboard to be able to attach both components to the same port.)
- Attach your Proximity sensors to the Arduino uno
- Attach the digital output pins (the left most pin on the sensors, while facing the side with the sensor) to A3 and A1 on the Arduino uno
- (Keep track which is which because this will determine your sensor1/sensor2 once you get to the coding)
- Attach the Ground (middle pins on the sensors) to ground on the Arduino uno.
- (all three components will need to go to ground so this is when you have to attach it indirectly)
- Finally Attach your VCC pins (right most pins on the sensors) to your 5V/3.3V on the Arduino uno
- (all three components will need to go to a voltage source, so this is also when you have to attach it indirectly)
- Attach the digital output pins (the left most pin on the sensors, while facing the side with the sensor) to A3 and A1 on the Arduino uno
- THATS IT!!
Code
- Make sure you start by selecting your "Arduino Uno" for your board in the tools tab.
- You may have to install and include U8g2 libraries for your OLCD to allow this specific OLCD screen to be able to communicate to the Arduino properly. You can download this by going to manage libraries in the tools tab and search for U8g2 and download the most recent version.
- You will also have to include the line
"U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* reset=*/ 8);"
this identifies the screen type so dimensions and capabilities of the screen are defined. (look to examples for more information about other options if needed).
- Next we have
- Sen1 is sensor 1
- Sen2 is sensor 2, both of these are dependent on which sensors you attached to A1 and A3
- velocity is measured velocity(cm/ms)
- "velocity_real" is the converted velocity (in m/s in this case)
- "timeFirst" is the time sensor 1 was hit
- "timeScnd" is time sensor 2 was hit
- "diff" is the difference between the two times
- "speedconstant" is distance between sensors
- The next section is void setup(). This will set time to zero before the loop, define serial monitor dimensions, start OLCD, and define input pins.
- Then we have the meat of the code. In the loop we will have three major parts
- The first will set sensor ones time once it is hit.
-
The first part detects that sensor one has not been hit immediately before which prevents overlap on time readings which would lead to inaccurate values (could shorted measured velocity if the sensor is hit more then once). It also detects when something passes the sensor so it can collect a time one.
-
- The second does the same for the second sensor but also finds a velocity value, convert into appropriate units, and display velocity to the serial monitor. (sending the velocity to the serial monitor is not required once you have the OLCD working but is recommended when you're setting up to make sure your sensors are working properly.)
-
The second part of the code collects a "time two" or the finish time as well as making sure the first time has been recorded. This also prevents the second sensor from collecting a new time after getting hit more then one time in a row. If we don't include this, it may give us lengthened time values which would give a bad velocity as well.
-
This second part of the code is also where the calculations and conversions are done. First, it takes time 2 - time 1, which gives us the time difference (diff) between each sensor getting hit. Then we take our distance between sensors (Speed Constant) and divide by our newly found (diff). This will give us a velocity in cm/ms (given that the Speed Constant you measured is also in (cm). To convert into more meaningful data we multiply by 10 and we will have our velocity in m/s (velocity_real).
-
- The third section will display velocity to your OLCD
- Finally, we display our newly found velocity onto our OLCD. Our screen does not like to display our values directly so we have to convert our (velocity_real) into a string with a defined number of significant figures. This is what the char:velstr[6] is doing for us. The other section prints text to the OLCD screen and in the parenthesis we define (height, width, and text) in that order.
- The first will set sensor ones time once it is hit.