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 info from Digilent on the Digilent Makerspace, check out the Digilent blog!
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: What You'll Need
For this project, you will need the following hardware:
- The DP32 from Digilent. Most any microcontroller will work, but I'm using the DP32 because its built in breadboard makes mounting sensors like these much easier.
- A USB to micro-USB cable. I point out the difference between micro-USB cables and mini-USB cables in my Getting Started with the DP32 Instructable. Make sure you get the right one!
- A battery pack with bare connector wires. I like to use four AA or AAA batteries for my microcontrollers. It's right in the range of tolerance for most boards, and it's big enough to provide plenty of long-lasting power without being too heavy.
- Some electrical tape. You can also use duct-tape, but for this project, electrical tape will work better due to it's opacity.
- A small, flathead screwdriver. This will be used both for adjusting your potentiometers, and for screwing your battery terminals into your board.
- Pliers. A thinner nose will work better for this, but my Leatherman pliers served me just fine.
- Wire cutters.
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:
- Four resistors. 220 Ohms each (red, red, brown resistor code). It's important to point out that you'll only need two resistors if you only use the first set of LEDs.
- Two LEDs. I like red, but it's really up to you.
- (Optional) Two more LEDs. I picked yellow because I liked how they looked, and these will be almost purely aesthetic once you're done.
- Two phototransistors. I used 5k to 20k Ohm photocells.
- Two potentiometers. Mine were 10k Ohm.
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:
- A soldering iron.
- (Optional) A hot glue gun. I think this is the first For Cheap Robots Instructable where the hot glue gun is optional.
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.
- Use your scissors to cut a strip of electrical tape that's about 1 inch (or 3.5 cm) long. I prefer to cut my tape for this step instead of tearing it because it leaves a much cleaner edge.
- Wrap your LED in electrical tape so it completely covers the sides of your LED, but leaves the tip open. Picture 3 shows about how far the tape should stick out past the tip of the LED. Try to wrap the LED neatly to minimize wrinkles.
- Do this for both LEDs
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.
- Cut another strip of electrical tape about 1 1/2 inches (or 3.5 cm) long. I opted to cut this strip in half lengthwise, because it makes taping my photocell to my LED easier. However, if you don't want to deal with that hassle, you can just use two 1.5 inch strips. This will, however, make later steps more difficult because your leads won't be as long.
- Take note of which side you will stick your photocell to. This is very important because the photocell needs to be on the side of your LED with the short lead (the cathode). Picture 4 shows what I mean.
- It's a pain to try maneuvering the tape, photocell, and LED at the same time, so I usually stick my photocell to one end of my tape first.
- Tape your photocell to your LED so that the photocell sticks out a little bit past the LED. Pictures 6 and 7 show what I mean.
- My photocell crowded my LED a little bit, so picture 8 shows me adjusting my photocell's position a little bit with my pliers. This is where those bends we put in come in handy!
- Make sure none of your leads are touching. Photo 9 shows how I splayed my photocell's leads outward a bit to keep them away from my LED's leads.
- Do this with both pairs of 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.
- Pictures 2 through 4 show how I use my pliers to thread the sensor leads through the holes on the DP32's breadboard. Notice how the photocell is on the right of the LED, next to the LED's cathode.
- Taking care to keep your leads in the holes of the breadboard, tilt your sensor to the side (towards the photocell) like in picture 5. You shouldn't need to bend the leads to do this. Picture 6 shows how I kept the leads just barely sticking through the underside of the breadboard. We want to make these leads as long as possible.
- I find it difficult to hold my parts like this while I'm soldering, so I very carefully added two beads of hot glue to my leads to hold my sensor in place. I kept the glue to the outside edge of the board, so as not to plug up any of the holes.
- Picture 8 shows my leads after soldering.
- I was worried that the leads of my photocell would touch. In picture 9, you can see how close together they are, so I bent them apart a little with my pliers, and added a bead of hot-glue between them which you can see in picture 11.
- Of course you're going to want to do this for both of your sensors. Make sure to mirror everything you did with the first sensor, by placing the photocell on the outside, and tilting away from your first sensor (like in the last two pictures).
Step 8: Add Your Potentiometers
This step uses your DP32, the potentiometers, and your soldering iron.
- Start by bending your sensors down flat like in pictures 2 and 3. Make sure you don't let the leads touch, and keep those photocells on the outside.
- Slip your potentiometers into your DP32's breadboard, next to your photocells.
- To keep the pots in, you can bend the leads like I have shown in picture 5. This will also allow you to connect certain components. This is somewhat hard to describe, so please reference picture 5, but I'll explain what's going on and why here. We want to connect one of our leads to our photocell, that's the inside lead. The center lead will eventually be connected to ground, so we'll bend it down. The third, outside lead doesn't get connected to anything, so I bend it up and away from the board.
- Picture six shows my board after I've soldered the pot in and trimmed that last lead off. Notice how the inside lead of the pot has been soldered to the outside lead of the photocell.
- Do this for both potentiometers.
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.
- Put the tips of your LED's leads through the breadboard, on the outside of one of your pots. Keep the cathode (the shorter lead) to the outside, and tilt the LED so that both leads just barely stick through the board.
- I used my hot-glue gun o carefully add a bead of solder to my leads, to hold my LED in place.
- Solder both LEDs on like this, making sure to mirror what you did with one onto the other.
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.
- Start by bending your resistor leads down like I've shown in the second picture.
- Thread the leads through the DP32's breadboard. One of the leads of each resistor should go through the grounding line of the breadboard like I've shown in the third picture.
- Bend the leads outward to keep the resistors in place.
- The fifth picture in this set shows my resistor leads after I soldered and trimmed them. The leads that go through ground should be trimmed, but the leads that don't should be kept long, because we're going to use them later to connect more components together.
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.
- Pictures 1 and 2 show where your power wires should connect. They all start on the power line to the side of the DP32's breadboard. One connects to the anodes of your sensor LEDs, and the other two connect to the inside leads of your photocells.
- As a note on cutting wires for this part. Always cut your wires longer than you think you need them. Once your wires are stripped, you can trim them shorter so they have the length you want, but it's much harder to trim a wire longer.
- It's also a good idea to give the wires a little bit of an ark. It make get a little confusing later on, but having this slack in your wire allows you to bend it out of the way of any wires you might want to fit in later. You don't want to have too much ark, however, because you're going to need to ark other wires over these later on too.
- Finally, make sure the leads you strip off your wires are nice and long. I made mine a little short here, which made it a little harder to connect them to the component leads I wanted.
- Bend your leads to hold your wires in place. The leads connecting to power I simply bent out and away from the board. The wires connecting to my components, however, needed to bend up to touch those components so I could solder the leads to them.
- Pictures 5 and 6 show my leads after I soldered them. Notice how the leads in picture 6 are soldered to the components they connect to. It's especially important to make sure you solder a power wire to both of your sensor LED's anodes.
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.
- Pictures 1 through 5 show where your wires should connect to. Basically all you're doing is connecting the cathode (the short lead) of each LED to a resistor. It doesn't matter which resistor connects to which LED, but you should try to make it neat and organized so you don't tangle up your wires.
- Bend your wire leads so your wires don't slip out. In picture 6, I show you how I'm touching the leads of my wires to the leads of my resistors, and in picture 7 I show you how I touch my wire leads to the cathod of my LEDs.
- Picture 8 shows everything soldered and trimmed neatly.
Step 13: Ground Your Potentiometers
Once again, you're going to use your black (or white) wire, wire cutters, strippers, and soldering iron.
- Picture 1 shows how I connect my potentiometers' middle leads to ground with a couple of white wires.
- Bend your leads to touch the component they need to connect to. I decided to bend my two ground leads together to touch, but you could simply connect both wires to ground line through the breadboard.
- Picture 3 shows everything soldered and trimmed.
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.
- Pictures 1 and 2 show where I connected my signal wires. Your right LED needs to connect to pin 11 (labeled RB0 on the board) and your right photocell needs to connect to pin 9 (labeled RA0).
- These pins are important because they both do specific jobs. Pin 9 is an analogue pin, which can sense the voltage applied to it, and pin 11 is connected to the onboard LED4 built into the board. (It's because of these built-in LEDs that the outer most LEDs are only aesthetic.)
- Picture 3 shows how I bent my wire leads to connect to the components. I must have forgotten to take a picture of them soldered, but by now I'm sure you understand what needs to be done.
Step 15: Add Your Left-hand Signal Wires
For my right-hand signal wires, I used blue wires.
- Pictures 1 through 3 show you where these wires should connect. One will connect the anode of your left LED to pin 14 (labeled RB3) and the other will connect your photocell to pin 10 (labeled RA1).
- Picture 4 shows the leads soldered and trimmed.
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!