Welcome back to everybody who's following along with my For Cheap Robots series! For those of you who're not so familiar with what I've been doing here, For Cheap Robots is a series I'm doing on Instructables all about how you can put together a remarkably sophisticated robot for extremely cheap using parts and supplies you can find around the house.
In my previous Instructables, I've shown you how to make cheap wheels and motorized chassis. Now I'm going to build on that by showing you how to add a simple set of light sensors to the DP32 that will allow you to track a line.
Let's get started!
For more Instructables on building cheap robots check out the For Cheap Robots collection, or for more things that I've done, check out my profile page!
For more info from Digilent on the Digilent Makerspace, check out the Digilent blog!
Step 1: What You'll Need
For this project, you will need the following hardware:
It's very important to note that you want the leads of your LEDs and photocells to be as long as possible, because we will use these as structural components.
and the following electrical components:
You'll also need at least two types of colored wire and a good set of wire strippers. I prefer solid-core wire. Normally the accepted standard is to use black for grounded wire and red for wire connected to power, but I'm running low on black wire, so I'll be using white for ground instead. It also helps to have at least two other colors of wire for this project, which will represent your right and left sensor connections, but that's not completely necessary.
Finally, you're going need the following tools:
Step 2: How to Solder
This Instructable assumes that you already know how to solder. For those of you who don't know, noahw gives a very thorough soldering walkthrough here. If you're interested, y compatriot JColvin91 also has a soldering Instructable, where he explains how to use a bunch of different soldering tips. I would also encourage you to pick up a Learn to Solder kit from the Maker Shed Store, Radioshack, or Fry's.
Step 3: Circuit Diagram
I've received several requests for a circuit diagram for this and my motor controller Instructable, and it's always been my intent to provide one. Now that I've finally found time to delve into Fritzing, I can finally provide a proper schematic and board layout! Yay!
I've provided two pictures for the board layout, one with wires and one without, because it's harder trying to figure out exactly where everything is placed with all that spaghetti getting in the way. I'd also like to point out that while Fritzing allows you to apply surface wiring to breadboards, it doesn't have a good way of showing solder connections on the other side of the breadboard. With that in mind, I've added white wires to my board layout to show how things are connected on the other side of the board. Finally, for clarity, I've colored all my signal wires blue, any ground wires that go through a resistor are green, and then the direct power and ground lines are red and black respectively.
For each sensor, we actually have three almost completely separate circuits running in parallel (and of course we have two sensors, so two sets of three circuits). First there's the circuit that I connected my red LEDs to, then the one my photocells are connected to, and finally the circuit my yellow LEDs are connected to.
I'll start with the red LED's circuit, as it is the simplest. That's simply connected from the regulated 3 Volt line down to ground, through a 220 Ohm resistor to keep it from burning out.
Next, I'll explain the yellow LED's circuit. This one is very similar to the red LED's circuit, as it's just an LED connected to ground through a resistor, but instead of starting at the 3 Volt line, we connect it to one of our board's IO pins. By setting this pin to "high" it acts as a 3 Volt source, and by setting the pin to "low" it acts as a ground. That's how we can turn that LED on and off.
Finally, let me explain how the photocell's circuit works. This too starts with a connection to our regulated 3 Volt line. The photocell itself acts as a resistor, with variable resistance based on how much light hits it. The more light, the less the resistance. Then, instead of connecting it to ground through a normal resistor, we run it through another variable resistor called a potentiometer, or pot for short. By turning the pot's dial, we can change its resistance. With these two resistances in series (the photocell and the pot) we vary what voltage is being applied to pin 9. Pin 9 is special (same with pin 10) because it can be used to measure the voltage that's being applied to it. That's how our board can tell if it's looking at something light or something dark! Neat, right?
Step 4: Wrap Your LEDs
This first step uses your main LEDs (red, in my case), some electrical tape, and the scissors.
I also want to point out, a while ago, member walshlg suggested that heat shrink tubing might work better for this step, and he's absolutely right! I didn't use it, however, because I felt that most beginners won't have a supply of shrink wrap just sitting around like I do. That said, for those of you interested in getting some of this, I would definitely recommend getting the Heat Shrink Tubing Kit from Fry's. It's what I've got and it's served me very well!
Step 5: Bend your photocells
This step uses your pliers and the two photocells.
This step is difficult to describe, so I'll just say that you want to bend your photocells like I have in the pictures. These bends will make it much easier to adjust your photocells later, so make sure you make each bend at least 1/8 inch, or 3 mm long. You don't want to make them too long, however, because we want to keep our leads long.
Once again, do this with both photocells.
Step 6: Make Your Line Sensors
This step uses the pliers, scissors, electrical tape, and both LEDs and photocells.
Step 7: Solder Your LEDs to Your Board
This step uses your newly made sensors, your DP32, the pliers, and your soldering iron. The hot glue gun is optional, but helpful.
Step 8: Add Your Potentiometers
This step uses your DP32, the potentiometers, and your soldering iron.
Step 9: Add Your Second Set of LEDs
This step is optional because these LEDs are purely aesthetic. It will use your second set of LEDs, your DP32, and your soldering iron. You can also use your hot glue gun here to help hold components in place.
Step 10: Add Your Resistors
This next step will use your 220 Ohm resistors, your DP32, and your soldering iron. I'm going to point out again that if you're only using the two sensor LEDs, and didn't opt to include the aesthetic LEDs, you only need two 220 Ohm resistors.
Step 11: Add Wires to Power
Now that we've gotten all our components soldered in, we'll start wiring everything together by adding our wires connecting to power.
Step 12: Ground Your LEDs
This step will use your black wire (or white in my case), your wire cutters, your wire strippers, and your soldering iron.
Step 13: Ground your Potentiometers
Once again, you're going to use your black (or white) wire, wire cutters, strippers, and soldering iron.
Step 14: Add Right-hand Signal Wires
Signal wires, to my mind, are any wire that's not providing constant power or grounding to a component, but a variable signal. These wires will connect to our photocells to give our board information about what they sense, and they will connect to our aesthetic LEDs to turn them on and off based off what the photocells "see".
I used yellow wire for my right side signals.
Step 15: Add Your Left-hand Signal Wires
For my right-hand signal wires, I used blue wires.
Step 16: Adding Some Final Touches
Now that everything has been soldered in, we can bend our sensors downwards, like in pictures 2 and 3. Take care, when you bend your sensors, that you don't let any of the leads touch.
Finally, you can screw add the battery pack by screwing the battery leads into the screw terminals. Make sure that you connect the leads to the correct terminals, as labeled on your board, otherwise you may damage your board. Picture 5 shows the position your jumpers should be in to use the power provided from these screw terminals.
Step 17: Programming, Testing and Tuning Your Board
Firstly, if you're unfamiliar with how to program the DP32 with MPIDE, you should check out my Getting Started with the DP32 tutorial, which takes you through the process of installing a simple LED blinking program.
I've attached the code you'll need to use these sensors. This code simply turns on the right or left LED, if the right or left line sensor sees light (or more accurately a white, reflective surface). When you download it, however, it may not work straight away because some of your components may differ from mine, so you'll have to tune yours.
There are two ways to tune these sensors, in the code or using the potentiometers. I'll show you how to use both.
First, with your board connected to your computer, open up the Serial Monitor by pressing the button in the top right-hand corner of the MPIDE window (it's outlined in picture 1). You should start to see something like picture 2. These values represent how much light your right and left sensors "see".
Place your board on a white, reflective surface. Watch what happens when you fiddle with the potentiometers on the front of your board. Depending on how you turn them, you can make the values go up or down. Now move your board over a dark spot, like a strip of electrical tape. Watch how the values go down.
If either of the right or left values rise above the threshold values set at the beginning of the MPIDE sketch, then the corresponding LED turns on. When it dips below this value, the LED turns off. By adjusting your potentiometer, you can find a setting where the values you get for light surfaces is significantly higher than the range for dark surfaces. Then you can set your threshold value in the code to sit somewhere between these two ranges.
Try it yourself!
I've gotten a lot of questions regarding why I chose visible spectrum LEDs for this, instead of infra-red LEDs. From what I recall, infra-red LEDs do work better, but the improved performance isn't really noticeable in my opinion.
Using visible spectrum LEDs, however, does two things for us. First, these LEDs are much easier to get a hold of in large amounts because they're more prevalent in electronics, and they're sold in large amounts more often. The main reason why I did this, however, is practicality. Having done a lot of projects with IR LEDs, I can tell you that it's a pain to make sure they're actually working. Beginners (like I was, and still am from time to time) can and probably will burn out an LED or two, and with IR LEDs there's no way to tell if it's working unless you pull out a camera. With visible spectrum LEDs, you don't have that problem.
Plus they look cooler with the lights off.
Step 18: Experiment!
Now that you've gotten your light sensors set up, try different settings for your potentiometers and threshold values! What happens when the room is darker or lighter? Can you use other colors of tape, like blue or brown?
As always I'd love to hear any feedback you can give me about my tutorial. What did you think was helpful, or what could have been explained a little better? I'd also love to see what you do with these light sensors!