By now we have already encountered a switch or two in building robots. One cool thing you can do with switches is sense whether or not it is closed by using an Arduino. By combining this principal with a lever switch, we can make a rather effective collission sensor. This allows the robot to know whether or not it has crashed into something. Using this technique, your robot can very easily tell something is in its way and it needs to stop driving and/or change direction.
For the collision switches you will need:
(x2) SPST lever switch
(x2) 8" zip ties
(x2) 3/16" x 2" shrink tube
(x2) 10K resistor
(x1) 1/4" fender washer
(x1) 1" round PCB (or similar)***
** For attaching the switches to the underside of the robot.
*** This is to be used as a drilling guide for measuring mounting holes. The board itself will be used in the next lesson.
As mentioned, a switch makes or breaks an electrical connection. By building a particular circuit, we are able to use the Arduino digital input pins to tell whether or not a switch is making or breaking a connection.
To build this circuit all we need is a switch and a 10K resistor.
We first connect the 10K resistor between the 5V power source and the input pin. By doing this, we are essentially connecting the digital input pin to power, and the Arduino reads this pin as being "HIGH" or 5V.
The magic comes in when we connect the switch between the digital input pin and ground. When the switch is not being pressed, it is like it is basically not there. However, when the switch is pressed, the pin no longer is connected to 5V, but is connected to ground instead.
You might be confused as to why, but this has to with the path of least resistance. The switch has no resistance, and offers electricity the easiest path to flow. So, electricity flows between the digital pin and ground, and bypasses the path that leads from the resistor to power. This configuration lets the switch toggle between power and ground without shorting any wires.
We can see whether or not the switch is being pressed by using the Arduino's Serial Monitor.
The Serial Monitor allows the Arduino to "speak" to your computer and report back its current state. It can tell you the value of a variable at any given time or let you know what part of the code you are looking at. In our case, we will be using it to tell whether or not a button is pressed.
To test it out, select the "Serial Monitor" icon (the little magnifying glass) from the bottom left sidebar and then upload the following code:
When it is pressed, the code will return "PRESSED" in the Serial Monitor. When not pressed the code will return "RELEASED"
Align the outer edge of the PCB with the outer edge of the CD. The axis the PCB's mounting holes are on should be running parallel to an imaginary horizontal line drawn through the center of the CD (or in our case an actual horizontal band). Mark the PCB's mounting holes on the surface of the CD.
Align the switches mounting holes with the two markings such that they are positioned with their levers pointing outwards (away from each other). The switches should also be rotated slightly inwards, such that they are not perfectly horizontally aligned.
Mark the remaining outer mounting hole on each of the switches on the CD.
Drill the four marking with a 1/8" drill bit.
Using the holes drilled in the CD, firmly zip tie the switches in place. Clean up the loose zip tie ends.
Solder a resistor to the end of a red 12" solid core wire.
Trim away any excess wire leads.
This will be used to connect the digital input pin to power.
Find the two tabs on the switch that point inwards towards each other. These are the ones that should be practically touching already.
Pass the free end of the 10K resistor between the tabs of each respective switch. Solder the resistor to the tabs.
Next, wrap the end of a green 12" solid core wire around these tabs. Solder this wire in place as well.
This is the wire that will be used to connect to the digital input pin on the Arduino.
Find the two tabs on the underside of each switch.
Connect them together with a short black wire. Solder this wire securely in place.
Select either one of the tabs connected to the black wire and solder a longer 12" black solid core wire to it.
In an ideal world, the opening in the CD will align perfectly with with the slider's bolt. However, like me, you probably don't live in an ideal world. Instead, you are going to need to drill a new hole.
To begin, remove the slider by unthreading its nut.
Position the CD such that the switches are at the edge of the lid, and make a mark to indicate on the CD where it aligns with the hole for the slider.
Drill through this mark with a 1/4" drill bit.
Drill a 1/4" hole in the lid near the servo motors.
The exact position is not remarkably important. This hole is simply going to be used to pass the wires from the sensors up into the plastic box.
Thread a 1/4-20 nut to the bottom of the slider's bolt, and then place a 1/4" fender washer onto the bolt.
Next, insert the bolt through the 1/4" hole in the CD.
Lastly, pass the bolt up through the hole in the lid, and thread a 1/4-20 nut on such that the bolt protrudes about 1/8" out of the top.
Once all of that is complete, insert the wires from the switch up through the 1/4" hole in the lid.
Plug the red switch wire in the 5V socket, the black wire into one of the ground sockets, and the green wire into digital input pin 2.
Program the Arduino with the following code:
The robot should now drive forwards until hits something. When it does, it should backup, turn slighly and drive forwards again.
Once programmed, fasten the lid back on, and tighten to the nut on the slider to lock the CD in place.
The zip tie antennae do more for your robot than make it look ridiculously cute. They are actually accomplishing two very useful things.
First off, they extend the length of the switch's levers. This allows the levers to cover the the entire front end of the robot and prevents any unexpected surprises and helps alleviate a corner getting snagged.
Secondly, by extending the length of the lever, they are also making it easier for the lever to be pressed down. This is just basic physics. The longer of a lever you use, the easier it is to exert a force. In other words, it makes the switch much more sensitive. Thus, you don't need to crash into things as hard to press the lever down and this is all-around a positive thing.
To make antennae, simply take a long zip tie and connect the tail end to the switch's lever using heat shrink tube.
Easy as that!
Power it up, let it loose, and see what happens.
Don't be afraid to experiment.
Share a photo of your finished project with the class!
Nice work! You've completed the class project