Support »
Basic LEGO Robot

View document on multiple pages.
You can also view this document as a printable PDF.
- 1. Introduction
- 2. Materials and Tools
- 3. Hardware Construction
- 4. BASIC Stamp II Software
- 5. Results and Conclusion
1. Introduction
<table class="picture_with_caption center"><tr><td><img alt="" class="" data-gallery-pictures="[{"id":"0J305","caption":"\u003cp\u003eBASIC LEGO robot.\u003c/p\u003e","url_tiny":"https://a.pololu-files.com/picture/0J305.98x98.jpg?a1b3b7bc5bf5dbad203e2b34d72f09f5","url_medium":"https://a.pololu-files.com/picture/0J305.600x480.jpg?a1b3b7bc5bf5dbad203e2b34d72f09f5","url_full":"https://a.pololu-files.com/picture/0J305.1200.jpg?a1b3b7bc5bf5dbad203e2b34d72f09f5","longest_side":400}]" data-picture-id="0J305" data-picture-longest_side="400" src="https://a.pololu-files.com/picture/0J305.400.jpg?a1b3b7bc5bf5dbad203e2b34d72f09f5" /></td><p></tr></table></p>
<p>The BASIC Stamp from <a href="http://www.parallax.com/">Parallax</a> is a popular introductory miniature computer for learning the basics of electronics and computer programming. By themsleves, however, the BASIC Stamps cannot control motors, which are crucial to robotics and many other projects that require motion. <a href="http://www.lego.com/">LEGO</a> is famous for its construction blocks, and it has lately been introducing many robot-themed sets. In this project, we demonstrate how you can use the <a href="/product/410">Pololu micro dual serial motor controller</a> to control a simple LEGO robot with a BASIC Stamp.</p>
<p>In its initial configuration, this robot behaves very similarly to the <a href="/docs/0J2">obstacle-avoiding robot</a> from project 1: this robot drives forward until it hits an obstacle; when it does, it backs up, turns, and resumes its forward motion. In future projects, we will use this project as a foundation for more complex and interesting robots.</p>
<p>Because the details of the mechanical design are not particularly interesting, we will not cover the assembly of the LEGO chassis. Instead, we will focus on how to use the micro motor controller with the BASIC Stamp II. This sample project should, therefore, serve as a reference for anyone wanting to use a BASIC Stamp to control a small, differential-drive robot (a type of robot, which, like a bulldozer or tank, has independent drive wheels or treads on its two sides).</p>
2. Materials and Tools
<p>Here are the essential parts you will need if you want to build a similar robot. Except for the LEGO chassis, these items are available either from Pololu or from most electronic component distributors.</p>
<ul>
<li><a href="/product/410">Pololu micro dual serial motor controller</a></li>
<li>BASIC Stamp II from <a href="http://www.parallax.com/">Parallax</a></li>
<li>Small, differential-drive chassis</li>
<li>Two long-lever, snap-action switches for use as bumper switches</li>
<li>Small <a href="/product/350">solderless breadboard</a></li>
<li>Two 10k resistors for use as pull-ups for the bumper switches</li>
<li>9V battery and battery snap</li>
<li>Hook-up <a href="/product/354">wire</a></li>
<li>Power switch</li>
</ul>
<p>You might also need these basic tools to prepare you chassis:</p>
<ul>
<li><a href="/product/159">Diagonal cutters</a></li>
<li><a href="/product/161">Wire strippers</a></li>
<li><a href="/category/5/tools">Soldering iron</a> and solder</li>
<li>Hot glue gun</li>
</ul>
3. Hardware Construction
<table class="picture_with_caption right"><tr><td><img alt="" class="" data-gallery-pictures="[{"id":"0J319","caption":"\u003cp\u003eLEGO motor.\u003c/p\u003e","url_tiny":"https://a.pololu-files.com/picture/0J319.98x98.jpg?b9fd12e4e8d86bc9c6a8ca3968329502","url_medium":"https://a.pololu-files.com/picture/0J319.600x480.jpg?b9fd12e4e8d86bc9c6a8ca3968329502","url_full":"https://a.pololu-files.com/picture/0J319.1200.jpg?b9fd12e4e8d86bc9c6a8ca3968329502","longest_side":300}]" data-picture-id="0J319" data-picture-longest_side="300" src="https://a.pololu-files.com/picture/0J319.300.jpg?b9fd12e4e8d86bc9c6a8ca3968329502" /></td><p></tr></table></p>
<p>Although we do not wish to dwell on the mechanical aspects of this project, the LEGO motor is worth mentioning. Within the compact housing are both a small motor and a very efficient gearbox. The motor is fairly expensive, but you’ll immediately appreciate how much more quietly it runs than cheaper gearboxes. Because the motor draws only approximatly 100 mA, it is perfect for use with our micro motor controller.
<br clear="all"/></p>
<table class="picture_with_caption right"><tr><td><img alt="" class="" data-gallery-pictures="[{"id":"0J320","caption":"\u003cp\u003eLEGO motor connector.\u003c/p\u003e","url_tiny":"https://a.pololu-files.com/picture/0J320.98x98.jpg?6f44e21644befb46e33baff1558a7777","url_medium":"https://a.pololu-files.com/picture/0J320.600x480.jpg?6f44e21644befb46e33baff1558a7777","url_full":"https://a.pololu-files.com/picture/0J320.1200.jpg?6f44e21644befb46e33baff1558a7777","longest_side":300}]" data-picture-id="0J320" data-picture-longest_side="300" src="https://a.pololu-files.com/picture/0J320.300.jpg?6f44e21644befb46e33baff1558a7777" /></td><p></tr></table></p>
<p>To use the LEGO motors in you own project, you need to get leads that you can connect to your own electronics. One possibility is to cut a regular LEGO cable in half and solder on male header pins onto the cut wires. This will give you the two necessary connectors at the expense of one LEGO cable.
<br clear="all"/></p>
<table class="picture_with_caption right"><tr><td><img alt="" class="" data-gallery-pictures="[{"id":"0J321","caption":"\u003cp\u003eCircuit for the Basic LEGO robot.\u003c/p\u003e","url_tiny":"https://a.pololu-files.com/picture/0J321.98x98.jpg?ec2b4f7ad960de53a993e47b728f49dc","url_medium":"https://a.pololu-files.com/picture/0J321.600x480.jpg?ec2b4f7ad960de53a993e47b728f49dc","url_full":"https://a.pololu-files.com/picture/0J321.1200.jpg?ec2b4f7ad960de53a993e47b728f49dc","longest_side":300}]" data-picture-id="0J321" data-picture-longest_side="300" src="https://a.pololu-files.com/picture/0J321.300.jpg?ec2b4f7ad960de53a993e47b728f49dc" /></td><p></tr></table></p>
<p>We built the electronics on a small solderless breadboard. As you can see from the close-up picture to the right, there isn’t much to the circuit. The only components other than the BASIC Stamp II and the motor controller are the two pull-up resistors for the bumper switches. The schematic diagram for this circuit is shown below.
<br clear="all"/></p>
<table class="picture_with_caption right wide"><tr><td><img alt="" class="wide" data-gallery-pictures="[{"id":"0J322","caption":"\u003cp\u003eSchematic for the Basic LEGO robot.\u003c/p\u003e","url_tiny":"https://a.pololu-files.com/picture/0J322.98x98.jpg?0d0414854e2355bdba9361590be89f48","url_medium":"https://a.pololu-files.com/picture/0J322.600x480.jpg?0d0414854e2355bdba9361590be89f48","url_full":"https://a.pololu-files.com/picture/0J322.1200.gif?0d0414854e2355bdba9361590be89f48","longest_side":600}]" data-picture-id="0J322" data-picture-longest_side="600" src="https://a.pololu-files.com/picture/0J322.600.gif?0d0414854e2355bdba9361590be89f48" /></td><p></tr></table></p>
<p>Using the BASIC Stamp II is very convenient because all of its I/O lines are interchangeable; the only reason we used the particular pins indicated in the schematic is because it made the wiring convenient. The Stamp’s on-board voltage regulator allows it to run straight off of a 9-volt battery, and the LEGO motors are designed to run at 9V as well. The Stamp’s regulated 5V output is used for the motor controller’s logic power supply. The diagram does not show the Stamp’s 12 I/O lines available for expansion.
<br clear="all"/></p>
4. BASIC Stamp II Software
<p>(In this section, we go over the major features of the program.
For more details, <a href="/file/0J8/front_bumperbot.bs2">view the entire program</a> (3k bs2).
Note that the pin numbers used in the program correspond to the
schematic diagram in section 3, above.)</p>
<p>The BASIC Stamp makes serial I/O very straightforward with its <code>serout</code>
instruction. A typical use of the instruction is:</p>
<pre>

 serout MC_SOUT, 32, [$80, 0, LFWD, SPEED] 'Left motor forward at SPEED</pre>
<p>The first value,
<code>MC_SOUT</code>, specifies the serial output pin to use, which is P10 in our example.
The second argument, 32, specifies the settings for the serial output; we use
32 because it specifies the baud rate to be 19,200, which is the maximum rate at
which the motor controller can receive. The four values in square brackets
are the values sent out over the serial line.</p>
<p>The first two values sent to the motor controller are
always hex 80 (128 in decimal) and 0, which let the motor controller know that
it is being issued a command. The third value specifies the motor number
and direction. To help prevent mistakes with this third parameter, we
defined constants for forward and backward for our two motors at the beginning
of our program:</p>
<pre>
 LFWD con 0 
 LBAK con 1 
 RFWD con 2 
 RBAK con 3</pre>
<p>(Of course, which constant gets assigned which number depends on how your
robot is wired up and what you call forward and reverse or left and right.)
The final parameter in the 4-byte sequence is the speed at which the motor
should run, where 0 stops the motor and 127 (7F hex) is full speed.</p>
<table class="warning"><tr><td>
<strong>Note:</strong>
At the beginning of the program, it is important to reset the motor
controller. Make sure the serial line is <strong>high
<em>before</em></strong> you reset the motor controller:
<pre>
 high MC_SOUT ’serial line idle state
 low MC_RESET ’reset motor controller
 high MC_RESET</pre></td><p></tr></table></p>
<p>The main loop of the program is rather simple since the robot
does not do much. For simplicity, we use the <code>pause</code>
instruction to determine the time that the robot backs up or turns,
but the BASIC Stamp could potentially be doing something more useful
during that time.</p>
<pre>
loop: 'Go forward till bump something
 serout MC_SOUT, 32, [$80, 0, LFWD, SPEED] 'Left and right motors forward at SPEED
 serout MC_SOUT, 32, [$80, 0, RFWD, SPEED] '32 indicates 8 bits, no parity, non-inverted, 
 ' buad rate 19200
 if (RBUMP = 0) then rbumped 'If bumped, turn backward in appropo direction
 if (LBUMP = 0) then lbumped
 goto loop 


rbumped: 'Turn backward right, then spin left in place for a random time
 serout MC_SOUT, 32, [$80, 0, LBAK, SPEED] 'Turn backward for 1 sec
 serout MC_SOUT, 32, [$80, 0, RBAK, SLOWSPEED]
 pause 1000

 serout MC_SOUT, 32, [$80, 0, LBAK, SPEED] 'Spin in place for random time, TURNTIME
 serout MC_SOUT, 32, [$80, 0, RFWD, SPEED]
 random TURNTIME
 pause (TURNTIME*5) + 250 'pause between 0.25 and 1.5 seconds

 goto loop


lbumped: 'Turn backward left, then spin right in place for a random time
 serout MC_SOUT, 32, [$80, 0, LBAK, SLOWSPEED] 
 serout MC_SOUT, 32, [$80, 0, RBAK, SPEED]
 pause 1000

 serout MC_SOUT, 32, [$80, 0, LFWD, SPEED]
 serout MC_SOUT, 32, [$80, 0, RBAK, SPEED]
 random TURNTIME
 pause (TURNTIME*5) + 250

 goto loop</pre>
<p>For more details, <a href="/file/0J8/front_bumperbot.bs2">view the entire program</a> (3k bs2).
Note that the pin numbers used in the program correspond to the
schematic diagram above.</p>
5. Results and Conclusion
<p>This project shows how easy it is to use the <a href="/product/410">Pololu micro motor controller</a> with a BASIC Stamp II. While the robot we built is certainly very simple, it is still a fun project that is especially appropriate as a first robot. Since the motor controller only requires two I/O lines, there is plenty of room for expansion. If you are ready to tackle a new project, you might try adding on our <a href="/product/701">IR beacon</a> to allow the robot to run away from another robot.</p>