Detecting Objects From A Distance
Distance sensor
While robots can use touch sensors to detect walls or objects, sometimes more flexibility is needed. What if your robot need to avoid walls or objects in its path, running into them with a touch sensor isn't always the best solution. To detect objects and know how far away the objects are robots can use a distance sensor. Distance Sensors, sometimes called ultrasonic sensors, use sound waves to tell the robot how far away the robot is from the wall. The sensor sends a pulse of ultrasonic sound, and then times how long it takes for the echo to bounce back to the sensor. The Arduino is then able to use the pulses to calculate how far away an object is.
This may seem like the perfect sensor to use everywhere, but it does have its limitations. The sensor is sound based, meaning that anything that effects sound can affect the sensor readings. For example, the sensor works best when facing a hard, flat surface. If the sensor is pointed at something angled or round the sound waves can bounce off into different directions and never return to the sensor. The sound waves can also be absorbed into soft material like foam and again not make it back to the sensor. When the sensor does not receive the echo it can not calculate the distance from the object.
Mini Project Control Motor Using A Sensor (Distance Sensor)
In this project, you will use an Ultrasonic Sensor to control when a motor runs. The motor will run when something is near the sensor and will stop when nothing is in front of it.
Find the following components in your kit:
- Previous Limit Switch Circuit
- Arduino Uno
- USB A-to-B cord
- Computer with USB ports
- Distance Sensor
- 22 AWG pin wire
- Header Pins
- Header Pin Housings
- Header Pin Crimper
To connect the Arduino to the Distance Sensor you will need 3 wires. Start by seperating and stripping a red, black and white 22 AWG wires.
Take the red, black, and white wires, and attach male header pins to the end of them.
Take the other end of the black, red, and white wires, and attach female header pins to the ends of them. Since the sensor has all of its pins together you can use either all single pin housings or use a 3x1 pin housing. Click below if you want to learn how to use the multi pin housing.
Use this module as a refresher on attaching header pins.
To begin attaching and crimping spade connectors, you will need the following:
- 22 AWG Wire
- Wire Cutter/Stripper
- Header Pin Crimper
- Male or Female Header Crimps
- Header pin housings or shells
- Needle nose pliers or Wire Cutter / Stripper
You may want to have extra crimps and wires. These crimps are small, require a special crimping tool, precise techniques and practice to create a proper crimp.
Header pin crimps are made up of three distinct portions: The mating pin zone is the portion of the crimp that interfaces with the component that you are connecting to. The other two portions interface with the wire. There is the conductor crimp zone which is the portion of the cimp that forms around the conductor and makes the electrical connection. The final part is the insulation crimp zone, where this part of the crimp forms around the wire’s insulation. This further securies the wire and creates a strain release. Both male and female header pins are crimped using the same process.
Cut a piece of the wire to the needed length and separate the number of wire you need from the .
Strip the ends of the wire so that 0.10 in or 2.5 mm of wire is exposed. It may be easier to strip the wires longer and trim the exposed wires to 0.10 in or 2.5 mm.
Cut off the male or female pin off of their connector strips. Keep the metal tab connected to the crimp, it will be used to align the crimp into the header pin crimper.
Place the wire into the crimp. Make sure the exposed wire is not in the mating pin zone.
To give your crimp a better chance to be successful use your wire cutters or needle nose pliers to start to fold over the tabs on the insulation crimp zone.
The recommended crimper has 2 different crimping areas the larger anvil crimps the insulation zone while the smaller anvil crimps the conductor zone. Make sure to line up the zones correctly or the crimp will not crimp properly.
Use the crimping tool to crimp the conductor crimp zone and the insulation crimp zone. Make sure the Mating pin zone is extended completely out of the crimping tool. If you crimp this part it will deform and will not function
After crimping, if you inspect the connection and see that the crimped parts are not fully compressed onto the wire you may want to crimp the connection a second time.
Once the crimp is successfully attached to the wire you can remove the metal carrier strip by bending or cutting it off. After removing that, the crimp is ready to be placed into the plastic housing.
The plastic housing, or shields as they are sometimes called, add structure and support to make the crimp more durable. The plastic housings come in various sizes that can hold multiple connections in one block. Some projects will have all the connections next to each other which make the multi-connection block very convenient to use. Using single pin housing is easier to learn on and creates wires that have more flexibility in how they can be connected.
Both the male and female header pins have a small square or rectangle of metal in the pin mating zone. This metal “tab” will lock itself into the square opening or “slot” in the housing. Make sure the tab and the slot are facing the same direction and push the crimp into the larger opening in the housing. If done successfully the crimp will lock itself in the housing.
Perform a pull test to confirm that the crimp will not slip out of the housing. If the housing does not easly slip off you have successfully made a crimp!
- Attach the newly created wires to the Distance Sensor. You will need to attach the female header pin of the black wire to the GND port of the Distance Sensor, the female header pin of the red wire to the 5V port of the Distance Sensor, and the female header pin of the white wire to the SIG port of the Distance Sensor.
- The 5V and GND connections are responsible for powering the board and allowing it to send and receive sound waves. The SIG or signal connection is that connection that will relay the timing information generated by the sensor.
- Attach the male header pin end of the black wire to a GND pin of the Arduino.
- Attach the male header pin end of the red wire to the 5V pin on the Arduino. Unplug the red 5V wire from the Motor Control Board to make this connection.
- Attach the male header pin end of the white wire to digital pin 7 on the Arduino.
Connect the Arduino to your computer using the USB A-to-B cable.
- Download the following program: Distance-Sensor-Motor-Control.ino
- Open the Arduino software and open the "Distance-Sensor-Motor-Control" program.
Click on the Upload button to upload the program to the Arduino Uno. The program should immediately run after it is done uploading.
What happens?
The motor should move only when an object is within 10 cm of the distance sensor! Use your hand to place it in front of the sensor at different distances.
Explanation
The Distance Sensor is connected to the Arduino and is powered by using the 5V and GND pins. The Digital pin 7 is used to both instruct the sensor to send the sound waves as well as listen for the sound to bounce back.
If you look in the program, you will see that the program has commands to send two quick pulses. These quick pulses ensure that the sensor is only looking for new sound pulses. This is required because when the sensor is initially powered up it starts sending sound waves out. The set of quick pulses allow the sound waves time to dissipate allowing the sensor to have a clean input.
The next few lines then set that pin as an INPUT and waits until it hears the sound. The program then records the time it took to hear the sound.
Then, once it does, it uses the amount of time to determine how far away the object is from the sensor. This program calculates distance in both metric and imperial units
To calculate the distances the program divides the time it took for the sensor to hear an echo by the speed of sound. That number is then divided by 2 to calculate the distance since the sound waves are traveling to the object and returning to the sensor.
The code then uses this measurement to determine when the motor turns on or off.