This is a guest contribution by Jay Weeks.
Howdy! My name is Jay Weeks. I work for Digilent, a small hobby/educational electronics company, writing tutorials for their projects on Instructables. I have a passion for robotics, and I want to make robots not only easy to approach for beginners, but also cheap enough that anybody can get involved. Because of this, I started the For Cheap Robots series on Instructables, where I post small, modular tutorials on how to make individual parts that can be applied to any robotic project.
Somehow my Instructables must have given Calin the impression that I can write a tutorial, because he’s extended an invitation for me to write a tutorial for his fantastic blog! I’m very excited to show you folks how you can put together a simple and inexpensive line-following robot of your very own!
This tutorial is drawing from several of my smaller tutorials, and I’ll link to those tutorials when I use them. I hope that this helps illustrate how I intend the ‘For Cheap Robots series’ to be used.
Let’s get started!
This simple line-following robot is based off two even simpler circuits, a pair of line sensors, and a pair of motor controllers, and it uses the DP32 from Digilent. In order to make this tutorial as simple as possible, I’m going to first show you how to make your motor controllers, and then show you how to make the slightly more complex line sensors.
For this part of the tutorial, you’re going to need:
- Two N-FETs. I’d recommend the NTE2380 from Radioshack. I’ll be using a couple of old ZVN2110As though, which are shaped a little different and don’t technically have a high-enough max current for this, but will work fine for demonstration.
- Two diodes. I’ve got a couple 1N4001 diodes that I had on hand, and most any diode will do. If you’ve got a choice, pick one with a current of at least 1 A.
- A couple of hobby motors, either bought or salvaged from some old electronics like mine were. I show you how to make these wheels on step 18 of my Motors and Wheels for Cheap Robots tutorial on Instructables, but there are several different motor and wheel types in that particular tutorial, and you can use either the gearbox motors or the direct drive motors with this tutorial.
- Not pictured are the two, two-pin male and two, two-pin female headers that I used to connect the motors to the board with. These are a little harder to track down, but they’re well worth it because they let you swap the connections of your motors, in case you connect them backwards the first time. I would recommend getting some from Adafruit (male, female) or Sparkfun (male, female). (You can also check out my tutorial on connecting header pins to motors on Instructables.)
- Finally, you’ll need a DP32 from Digilent! Any microcontroller board will do, but the DP32 has a prototyping area on board, which makes building this circuit that much easier!
You’re also going to need a simple, cardboard robot chassis. I have three tutorials up on Instructables that can show you how to make three different styles of robot chassis. You should pick one that you think will work best for your motors. As you can see below, I chose a Boardbot style chassis because of its simplicity and how well it works with duct-tape wheels.
You can think of motor controllers like a sort of light switch for your DP32. Instead of trying to power a motor itself, your board can simply “switch on” the motor controller, and let that power the motor! You can use a couple different components for this job, but what we’re going to use is a simple N-FET style transistor!
I’m not going to go into how N-FETs work here, but if you want to learn more you can check out my friend Brandon Marcum’s MOSFET tutorial on Instructables.
For now, I’m going to focus on what they do and how you can use them.
N-FETs have three leads, drain, source, and gate. Drain connects to the higher voltage point, while source wants to as low a voltage as possible, usually ground. Gate, meanwhile, is used as the “switch” that allows the current to flow through the N-FET from drain to source. With N-Fets, this switch is “turned on” when the gate pin is brought to a high voltage by one of your microcontroller’s pins, and “turned off” when that pin is brought back down to zero volts again.
This is the circuit we’ll use to control our motors. If you notice, we have a diode connected backwards across our motor terminals. This is called a “flyback diode” and it protects our transistors from the energy stored in the motor when the transistor “shuts off”.
You can think of motors like a sort of electrical flywheel: once they start spinning, they don’t want to stop. This applies not just to the mechanical components of the motor, but to the current in the motor’s coil as well. More so in fact! When the transistor cuts off the current flow, all of a sudden it has to deal with all that stored energy trying to be released. Instead of making our transistor endure the brunt of that surge of energy, we redirect it back to the positive motor terminal through our flyback diode!
This is how our motor controller will be laid out using N-FETs like the NTE2380 from Radioshack. You need to be careful and choose a transistor that can handle the maximum current draw of your motors, and the NTE2380 can handle up to 2.5 amps, well above what my little hobby motors can draw. Picking a diode is less important, but generally you should pick one with a max amperage similar to your motors.
Just a quick note on the above image: Digilent hasn’t put out a fritzing file for the DP32 just yet, so I did my best to emulate the board style on a regular breadboard. You can see the built-in connections on the board above, and any solder bridges are shown using white wire.
Now, before you get soldering, I have a tutorial for making this motor controller up on Instructables. You may want to check that out for a step-by-step instruction on how to put this circuit together, complete with pictures! (NOTE: As of my writing this, the tutorial is out of date and doesn’t include the flyback diodes, but should be brought up to date v )
This is the picture of my DP32 after I soldered in all my components. As you can probably see already, it’s different than the schematic above. That’s because the N-FETs I used were ZVN2110A, an older transistor type that I had easy access to. Technically the maximum current that these can take is lower than the current my motors need, but for demonstration purposes, these work well enough.
Once you have your board soldered, you can attach it to your chassis with a bit of rolled-up duct tape. Now you should test the motors, by uploading the provided test code. If you are not familiar with how to use MPIDE with the DP32, please check out my Getting Started with the DP32 tutorial.
If everything is working properly, your motors should alternate on and off, causing your robot to do a sort of shuffle forward!
For this part of the tutorial, you’ll need:
- A couple of trimmer potentiometers. These ones are 10k Ohms (and a little gnarled after I salvaged them from a previous project).
- Two LEDs. These can be any color, but for this project I’m using red.
- Two photocells.
- Two more LEDs. These are not required, but they make the circuit look and work much better.
- Two or four resistors, 220 Ohms. You should have one resistor for each LED.
You should also still have your DP32 from the previous part, as well as the motor controller circuit still soldered on.
Light sensors act very similarly to a potentiometer. Instead of changing their resistance with a mechanical component, however, our photocells change their resistance based off the amount of light they receive. More light makes them less resistant, and less light makes them more resistant.
We can use a simple voltage divider, shown above, and a connection to the analog inputs on our DP32, to tell how much light the sensors are receiving. More light: higher voltage. Less light: lower voltage.
The reason we’re using a potentiometer as our second resistor in the voltage divider is a matter of tuning. By using a trimmer pot here, we can manually adjust the range of voltages our microcontroller reads from both our photocells so we don’t have to change our code as much between our two sensors. I’ve found that a 10 kOhm or so potentiometer works well here.
This is the circuit diagram for our complete line sensor circuit. As you can see, it’s really just six very simple circuits running in parallel. In the center, there are two LED circuits connected to our regulated 3.3V pin. These LEDs will provide a little regulated illumination for our light sensors to pick up, so they don’t have to rely on the quality of the overhead lighting. Then there are our two photocell voltage dividers, connected to their analog pins. Finally, we have two more LEDs on the outside connected to two digital pins on our DP32. These will be used for feedback, telling us when each sensor reads “light” and “dark”. These outside LEDs are not required, because you can use the onboard LEDs, but they look much better!
Next, I’ve split up the layout into two parts, one to show you where all the components are placed, and one to show how they’re wired together.
Here, you can see how our photocells are placed close to the innermost LEDs. Right next to those are our potentiometers, and finally our indicator LEDs. There are also four 220 Ohm resistors connected to our ground rail.
Wired up, this circuit looks much more complicated than it is, I assure you. It gets much simpler once you break it up into sections:
- RED: These red wires connect our photocells and both of the inside LEDs to our 3.3 V power rail on the left.
- BLACK: These are our normal ground lines. They connect the wiper pin of our trimmer pots to
- GREEN: These are our resistor ground lines. They connect all of our LEDs to ground through a 220 Ohm resistor.
- BLUE: These are our signal lines. They connect our two outside LEDs to pins 11 and 14 (RB0 and RB3 respectively), and our photoresistors to pins 9 and 10 (RA0 and RA1 respectively). Pins 11 and 14 will be used to turn our LEDs on and off. Meanwhile, pins 9 and 10 are analog inputs, which will allow us to read the voltage of our line sensors!
- WHITE: Once again, the white wires represent solder bridges on the underside of the breadboard.
As with the motor controller, I have a tutorial for making line sensors up on Instructables as well. This includes a ton of very useful tips and tricks for making this circuit, as well as pictures and explanations of each step of the process. I highly recommend it!
This is what my board looked like after installing the line sensor circuit. You can see how I have the yellow, outside LEDs angled away from the rest of the board. I did this entirely because they look like little glowing eyes, but there’s a useful offshoot as well because it makes them much easier to see.
You probably also noticed that I have a special way of wrapping my red, inside LEDs and photocells with electrical tape. This is one of the things I cover in detail in my tutorial, and they help out a lot! Wrapping your LEDs like this helps shield your light sensors from their glare, which would otherwise give you an artificially high light reading.
Programming and Tuning
Before we start programming our robot, I need to introduce you to MPIDE, and teach you a little about the DP32.
MPIDE is the programming environment used for chipKIT boards like the DP32, and you can get it at chipKIT.net. If you’re familiar with the Arduino programming environment, then you’re probably thinking that MPIDE looks similar. In fact, it works pretty much exactly like Arduino, but has custom libraries that allow you to program chipKIT boards. It’s worth noting, that MPIDE can program Arduino boards as well, but Arduino cannot program chipKIT boards.
The DP32 is an extremely minimal board. It’s so minimal, that it doesn’t carry the hardware that most boards use to communicate over USB with the computer! This means that in order to program it, you’ll need to boot your DP32 into bootloader mode.
To do this, start by plugging your DP32 into your computer with a micro-USB cable. Now press and hold the “RESET” button on the board (it’s the one right below the PIC32 in the picture above). While holding RESET, press button 2, that’ll be the lower button of the two buttons on the right of the picture above. Now release RESET, and then release button 2.
If everything went correctly, your DP32 should start blinking LED1, which is the illuminated LED in the picture above.
Now, in MPIDE, you should see the COM port for your DP32 show up in the Serial Port menu. If you’re not sure which COM port is your DP32, just hold RESTART again, and see which COM port disappears.
After you’ve selected your COM port, you’ll need to select your hardware, just like you would in Arduino. You can check to see if your DP32 is connected properly by opening up the Blink example code in File > Examples > 1.Basics > Blink.
Keep in mind that when the DP32 starts up from reset, it’ll automatically boot to whatever was programmed into it last. If you want to re-program your DP32, you’ll have to restart it into bootloader mode again.
Now that you’ve gotten both the line-sensor and motor controller circuits added to your board, and you’ve tested out how to program it with the Blink example, it’s time to program and tune your robot! Start by re-attaching your DP32 to your chassis, and laying out a simple track of black tape on a smooth white or lightly colored surface.
Upload the light sensor test code to your robot, and place it over a light portion of your surface. Then adjust the trimmer pots on both sensors until your indicator light turns off.
Now, turn your trimmer pot the opposite direction until your indicator light just barely turns on again. Your light should be steady and not flicker while over your light surface, but it should immediately turn off as soon as your sensor passes over your dark tape. You should do this for both sensors.
Now that your sensors are tuned, you can upload the line follower code. Place your robot back on your test track, with both sensors just to the left of the line, or with the right sensor resting directly on the line.
If everything went smoothly, your robot should bounce back and forth across the left edge of your line.
If a turn is too sharp, and your robot’s right sensor crosses over to the right side of the line, the left sensor should soon catch this, and turn the robot back until it is back on track.
Hopefully this tutorial has helped you jump-start an interest in robotics! I really want to make robotics easily approachable for everybody by making it cheap to get into and quick to start up. No hundred-dollar kits. No specialty items to order. I believe that there’s a surprising amount that you can do with cheap, everyday materials like cardboard, duct-tape, and hot glue. Let’s see what you can do!