loading

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:

  • 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.
  • Scissors.
  • 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.

  1. 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.
  2. 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.
  3. 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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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!
  6. 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.
  7. 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.

  1. 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.
  2. 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.
  3. 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.
  4. Picture 8 shows my leads after soldering.
  5. 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.
  6. 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.

  1. 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.
  2. Slip your potentiometers into your DP32's breadboard, next to your photocells.
  3. 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.
  4. 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.
  5. 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.

  1. 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.
  2. I used my hot-glue gun o carefully add a bead of solder to my leads, to hold my LED in place.
  3. 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.

  1. Start by bending your resistor leads down like I've shown in the second picture.
  2. 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.
  3. Bend the leads outward to keep the resistors in place.
  4. 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.

  1. 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.
  2. 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.
  1. 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.

  1. 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.
  2. 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.
  3. 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.

  1. Picture 1 shows how I connect my potentiometers' middle leads to ground with a couple of white wires.
  2. 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.
  3. 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.

  1. 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.)
  2. 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.

  1. 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).
  2. 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!

Note:

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!

Good luck!

<p>Great site!!</p>
<p>Thank you! I work hard on these!</p>
<p>These instructions are great, clear and easy to follow. It's a great project and introduction to sensors. Keep up the great work, I can't wait for more.</p>
<p>Thank you very much! I'm working hard to keep everything as simple and clear as possible!</p>
<p>good</p>
<p>Hi , why not just use the infrared method ? Wouldn't that be easier ?</p>
<p>Infrared is simple but hard to find. and as this is about Cheap robots and easy to find stuff the leds are used. But i agree IR is easier</p>
<p>You're partially correct. It's true that I picked regular LEDs because they're easier to get (in fact I didn't have any IR LEDs at the time), but that's not the main reason I did it.</p><p>My Instructables are partially aimed at younger learners, and in all of my projects with IR LEDs, I've found that they're singularly painful for novices (like myself) to confirm that they're actually working! I've always had to pull out a camera of some sort just to be able to see how bright they were, and I don't have the experience necessary to just plug and play. It's extremely convenient to be able to see what's going on directly, and I feel it's much more understandable for younger learners as well.</p>
<p>oh i now understand. IR is invisible so we may not know a problem lying right under our noses. Thanks.I will try it out.</p>
<p>Exactly! Sorry if that wasn't clear before. I learned about this annoying little problem with IR in high school when I was fiddling around with the Wiimote, and had to deal with it again in college during a robotics class.</p>
<p>thanks for the quick reply.</p>
<p>Most people will have an ancient IR remote control from some old equipment lying around (or you can get a random one for pennies at a Goodwill or a pawn shop). Also you can test the LED is working by holding it up to a TV or any device that shows a red light when it is receiving an IR signal (even if it isn't coded properly to actually make it do anything)</p>
<p>I wish Instructables would show circuit diagrams (schematics) of their projects circuits. I hate trying to figure it out from the photoes. We are big boys and can understand schematics.</p>
<p>Haha! Yeah, that's my bad. I'm going to see if I can throw something in here about the circuit and what's actually going on, but this time I just wanted to get this up and posted.</p><p>Thanks for the feedback!</p>
Just a quick rough hand sketch would be nice. The lines don't have to be straight.
<p>Well, I'm actually hoping to find time to learn to use Fritzing for this, because I'm going to want it for some of the upcoming Instructibles I have planned. Push come to it, yeah I'll at least post a hand-sketch.</p>
<p>Well that is pretty darned cool! I've gotta try this.</p><p>May I suggest: Black tape gets so sticky and gooey that you won't want it to touch anything and then falls apart eventually. May I suggest a short length of heat shrink tubing instead?</p>
<p>Oh I definitely agree! I chose to use electrical tape because it's more likely that your average novice will have some dark tape of some sort lying around than heat-shrink.</p><p>I'll have to point out this option though, thanks!</p>
<p>I've never thought about it but I bet there's something we could find in trash that could be used as heatshrink, along with a hair dryer. Plastic drinking straws? Old bike inner tubes? Let's experiment whenever we find anything tubical and see if it will shrink!</p>
<p>that is keeping it cheap and simple. Instead of using rare-to-find ir leds and receivers</p><p>just a photocell. Pretty smart.</p>
<p>Thanks! I guess that's a handy symptom of having so little time to work on these! I'm forced to use what's easily accessible!</p>
<p>Yay cheap robots! Thanks for sharing!</p>
A pleasure as always!

About This Instructable

23,463views

208favorites

License:

Bio: I build robots out of boxes! I love teaching what I've learned and seeing people add their own ideas to what they've learned ... More »
More by JayWeeks:Speed PID using Digilent Gear-Motors and Encoders! Getting Started With the Pmod HB5: Part 1 Resistive Touch Screen on the DP32 
Add instructable to: