Sweet! An m&m’s Candy Bot That Follows Lines

Front view of Sweet m&m line-following robot

Front view of Sweet m&m line-following robot

For the holidays, my local grocery store sold m&m’s candy containers in the shape of a racecar. The containers are painted metal (probably steel), with a thin plastic coating on the inside. A dinky package of plain m&m’s came inside; not peanut, as the yellow-color driver suggests.

About this same time, the ChiBots robot club began discussing holding a robot line-following contest. Naturally, the colorful candy tin seemed like a perfect body to motorize and drive around the course.

Gearhead motors with LEGO wheels

Gearhead motors with LEGO wheels

Motors and Wheels:

I had two Buehler gearhead motors (1.61.013.326, All Electronics CAT# DCM-147) lying around after their weight rejected them from inclusion in an early Bugdozer mini-Sumo prototype. They fit nicely and provide enormous torque at low speeds - exactly matching the needs for meticulous floor examination.

A brass tube (with an embedded plastic LEGO bar) couples the D-shape motor shaft to a LEGO wheel. Because the LEGO bar connects with friction to all standard compatible LEGO wheels, a wide selection of high-quality, inexpensive, production-precision tires are available and quickly interchangeable. A screw hole is drilled and tapped (threaded) to lock the brass to the gearhead shaft.

Since the shaft is off-center on the motor, the motor bodies are purposely misaligned with each other, so that the wheels actually line up.

Clamping wood to the metal frame Drilling holes with tape protection

Left: Clamping wood to the metal frame. Right: Drilling holes with tape protection

Masking tape (blue) protects the paint at the drill sites. Clamp a piece of scrap wood against the thin metal body to prevent vibration and tearing during drilling.

Center punch - always wear safety goggles

Center punch - always wear safety goggles

Be sure to center punch holes before drilling! A center punch looks like a solid metal pencil with a hardened pointy tip. Aim the tip against the location for the hole and then lightly strike the punch with a hammer. The indentation created prevents the drill bit from walking away from the desired hole location.

Try it! It makes hole drilling accurate and easy.

Print outs of circuits taped in place to guide center punching Wood to level the underside during LED and sensor hole drilling

Left: Print outs of circuits taped in place to guide center punching. Right: Wood to level the underside during LED and sensor hole drilling

Sensor and Circuit Board Holes

The printed circuit boards will be attached to the body with 4-40 size screws. To determine where the boards fit and where the holes should be drilled, I printed the CAD circuit drawings and taped them to the body. The paper would shift and tear-up during drilling, so I just used it to center punch each of the holes and to determine drill bit sizes.

The car body has wheel axles formed into the base. A piece of scrap wood is taped in place to keep the candy container flat during drilling.

Bare metal exposed with protruding burrs LED and sensor holes. Bar graph LED’s front is covered with wax paper and tape during gluing

Left: Bare metal exposed with protruding burrs. Right: LED and sensor holes. Bar graph LED’s front is covered with wax paper and tape during gluing

Most of the holes drilled cleanly, although many have burrs and rough protrusions. An unfortunate consequence of the drilling is the exposure of the raw metal of the body.

Beneath the front of the robot, two rows of eight holes each are for phototransistors and one middle row of seven holes are for the ultra-bright, high-efficiency, low-current, red LEDs (All Electronics CAT# LED-50). The LEDs provide a consistent and controllable source of light, to which the phototransistors sense the light reflecting off of the floor surface below.

(Aside: To save money and to avoid multiplexing inputs, only six phototransistors are currently installed.)

A 10-segment LED bar-graph fits into the front grill. Wax paper is taped against the front to prevent hot glue from spilling through the cracks during bar-graph mounting. Yellow color hot glue was used to fill the cracks, although clear or black would have been fine.

Metal shards collected on the motor

Metal shards collected on the motor

Don’t forget to remove the motors when drilling nearby metal. The shards and scrap collect on the motors due to magnetic attraction.

The above picture contains a more detailed shot of the brass coupler. (Complete instructions on making a motor shaft to LEGO wheel coupler are detailed in the book Robot Building for Beginners, or make couplers on a lathe.) Also in the above picture, the rear of the opposite motor reveals the off-center shaft.

Black paint to insulate phototransistors Reddish-pink latex mask peeled off after painting

Left: Black paint to insulate phototransistors. Right: Reddish-pink latex mask peeled off after painting

Short Circuit #1

Remember earlier that the holes drilled in the metal body unfortunately exposed the bare metal? With all the circuit boards installed, the robot refused to operate. A quick voltmeter check revealed the motor power (10 to 18 volts) had mixed with the logic power (5 volts).

But how?

A motor power pin on a Molex connector on the power supply circuit board had burrowed through the thin plastic liner and contacted the body. I had even added a transparency sheet between the board and the body, “just in case”, but that had been pierced as well. Now, nylon washers add space between the body and the board to correct that situation and provide an air channel for cooling.

But how did the motor power in the frame reach the logic line?

The phototransistors at the front of the robot have metal cases. The metal cases touched the exposed metal in the burred holes. The metal cases also happen to connect to the logic power.

I used a hole reamer to somewhat deburr and slightly enlarge the holes. Then I painted the exposed metal on both the holes and the sensors. Not only does the paint protect the exposed metal from corrosion, but should provide enough electrical insulation in case the phototransistors accidentally touch the sides of the holes again.

The metal body proved to be a liability. Plastic frames are lighter, easier to work with, and self-insulate. On the other hand, steel is durable, strong, and feels less cheap.

Before painting the sensors, I dabbed their lenses with reddish-pink latex mask. After the paint dried, the masks were just peeled off.

Notice it does not say Insulates! on the tube?

Notice it doesn’t say “Insulates!” on the tube?

Short Circuit #2 - Microcontroller Death

This latex mask is great stuff! I poured it over all the backs of the printed circuit boards as further electrical insulation. No need to dry beyond tacky. Plug the boards in and *POOF*.

Guess what? Liquid latex is a weak conductor. A splotch on paper and an ohmmeter demonstrates 150 kilohms of resistance, rather than multiple megohms.

The 10-volt (and up) motor power killed all microcontroller pins sinking current. All inputs and +5 V outputs were undamaged. Showing some signs of intelligence, I socket my DIPs. With a quick replacement of the MC68HC908GP32CP microcontroller ($7) and the peeling and removal of all latex masking, Sweet was back in business.

All other chips and components escaped without apparent permanent injury.

Robot guts

Robot guts

Open the Lid

The container has plenty of room for motors, batteries, and three homemade printed-circuit boards. With the exception of two pieces glued to the frame, everything disconnects either through screws or Molex connectors.

An LCD provides for debugging, but doesn’t fit when the racing car cover snaps on. The display screens are nearly identical to Bugdozer’s LCD since all the core code for this robot came straight from Bugdozer.

Plenty of current

Plenty of current

Power Source

Either a pair of 9-V batteries or a pair of four AA cells supply power. In either case, the batteries are center tapped to provide half power to the Maxim voltage regulator for the logic circuits. This saves on voltage drop, while still providing a full kick to the motors.

From: David
Subject: Re: Sweet

You’re right. A simple ohmmeter check would have revealed the short before applying power. I didn’t expect it and therefore overlooked this money-saving step. : )

In center-tapping, one battery does indeed drain faster than the other. Both batteries supply full power to the motors, but one takes on the additional load of the logic chips and LEDs. This means there isn’t any gain in running time before a battery swap, and there isn’t any gain in peak or maximum current draw.

There’s a slight advantage to center-tapping, in that I can swap out the heavily-loaded battery when it’s drained, but still have about half the life left in the remaining battery. In essence, three fresh batteries can perform the work that would otherwise require four. (The actual ratio depends on how much idle logic time vs. motor driving time.)

A couple of other related advantages:

  • Less heat is generated in the voltage regulator (because it doesn’t need to discard twice the voltage).
  • I could choose a logic voltage regulator that has a maximum rating less than the motor power (18 V).
  • I can tap three AA packs at 6 V and 18 V, getting additional power savings.

Regardless, you make an important point. This particular center-tapping arrangement only provides power savings, not current savings and therefore not extended running time.

By the way, inductor and capacitor-based switching regulators are much more efficient for most applications.

The power connects to the power supply / motor driver board via a three-pin (GND, +5, +10 to +18) Molex connector. This allows for rapid replenishment or a change in weight (9 V batteries are obviously lighter).

Using two alkaline 9-volts tops the motor supply at 18 volts. But, they don’t provide much peak current nor do they last very long. These are a good choice only for competition runs.

At the low end of the scale, using eight 1.2-volt NiMH rechargeable cells provides only 9.6 volts to the motors. Although in practice they seem to be capable of a little more than 10 volts. Tapping that provides half the rated value, 4.8 volts, which is below the desired 5 volts to run the logic chips. But since the chips are all high-speed CMOS or better, they operate just fine down to 3.3 volts.

The AA packs last a lot longer than 9-volt batteries. They are also capable of delivering more peak current. To counteract the added weight, the packs are balanced in front of and behind the wheel shafts.

Tailpipe Switch

Tailpipe Switch

Cool Tailpipe Switch

The tailpipe is actually a switch. By having a covert switch, no electronics mess with the racing car decorum when the lid is on. Pressing the switch provides an audible and tactile “click”. (Years later, the tailpipe switch needed repairs.)

When the switch is pressed:

  1. The bar-graph changes to an entertaining mode, reminiscent of talking car from a certain 1980s television show. Click to see a movie of the bar-graph pulsing back and forth just for fun
  2. Five of the red floor LEDs are enabled in the front of the robot.
  3. The floor sensors are all examined.
  4. If the center floor sensors see more light than the side floor sensors, the robot must be on a lighter (possibly white) line.
  5. If the center floor sensors see less light than the side floor sensors, the robot must be on a darker (possibly black) line. In this case, future phototransistor values are EORed (bits inverted) until the robot is stopped. The same code can process the black line as if it were white, since the brightness values are now reversed earlier in the code.
  6. The robot drives around, centering the analog brightness values by applying fewer or greater power pulses (pulse-width modulation) to the respective motors. This allows for very smooth driving, since the inputs are nearly analog and the outputs are nearly analog.

During driving, if the switch is pressed again:

  1. The robot stops.
  2. The red floor LEDs are disabled to save power.
  3. The bar-graph changes back to line-indicator mode to help position the robot for the next start.
Click to see a movie of a strip of paper being moved underneath the sensors and the bar-graph LED displaying the robots sense of following

Click to see a movie of a strip of paper being moved underneath the sensors and the bar-graph LED displaying the robots sense of following



Power supply and motor driver board

Power supply and motor driver board

Power Supply and Motor Driver Board

This is a single-sided homemade printed circuit board.

A Maxim MAX603 regulates logic voltage. Notice the liberal use of capacitors on both the regulated and unregulated paths to make it through current surges without glitches.

A pair of 4424 MOSFETs control power to the motors via pulse-width modulated signals from the microcontroller. Unlike Bugdozer’s circuit, these H-bridges are properly protected with Schottky diodes in case the motor coils induce voltage above maximum or below ground.

A green LED connects to the microcontroller board hinged above the power board. The green LED blinks every second under microcontroller control. This indicates that not only is there power, but that the microcontroller board is connected and that the chip isn’t hung.

In hindsight, an additional, independent power LED would have been nice to indicate that power was good even if the microcontroller wasn’t working. Also, the power switch is too difficult to reach.

DIP Brains

DIP Brains

Microcontroller Board

This is a double-sided homemade printed circuit board.

The 68HC08 microcontroller provides plenty of horsepower at 8 MHz. It is actually capable of variable speed since it uses a 38-kHz watch crystal PLL circuit. This is the first time my robot’s program has taken so much memory that all of the globals didn’t fit in zero page.

The pair of 74HC251 ICs provide another 16 external logic-level inputs. In future, I'll switch to serial-based chips instead of address-based.

External buttons (the blue-wrapped breadboard on the far-right) control the LCD paging.

The eight DIP switches provide manual override in case of trouble:

  1. Bar-graph mode zeros bit.
  2. Bar-graph mode ones bit.
  3. Bar-graph mode twos bit. Combined, the modes include: disable, K.I.T.T., line indication, auto, and binary counter. Click to see a movie of the bar-graph counting up in binary every 1/128 of a second or so
  4. Line-follow zeros bit.
  5. Line-follow ones bit. Combined, the modes include: dark line, light line, calibrate phototransistors, and auto.
  6. LEDs forced on. This helps when debugging phototransistor ranges or for showing off.
  7. Auto-stop disable (the robot usually stops when a line ends). This setting also forces the robot to start line seeking immediately, without pressing the tailpipe, just in case the processor resets during competition.
  8. LCD attached. The robot won’t waste time trying to communicate or write to the LCD if this setting is disabled.
The eyes from above and behind

The eyes from above and behind

Emitter and Detector Board

This is a double-sided homemade printed circuit board with surface-mounted components.

A pair of 74AC299 SOICs drive over 100 mA worth of current to the 10-segment red bar-graph LED and the seven floor emitters. Visible-light emitters and detectors are superior to infrared for line detection. What looks like a line to a human eye may be almost transparent (masking tape) or completely opaque (certain clear plastics) to infrared. Since humans are building the courses, the robot should see using the same spectrum!

The daisy-chained 299 chips accept serial input from the microcontroller. Unfortunately, the serial register is internally connected directly to the output latches, so you can’t retain the old outputs when shifting in new data. Unless the 299 output is disabled during shifting, all the LEDs show a tiny bit of light.

Although there’s room for up to sixteen sensors, only six phototransistors are currently installed. They all connect back at the ADC inputs on the microcontroller board via a ribbon cable.

Because phototransistors vary considerably, Sweet’s software includes some prescaling. That is, the highest and lowest values of each phototransistor are recorded in manual calibration, and those values are used to multiply the incoming data to a common range. After being calibrated once in the labs, all the phototransistors provide similar data for similar lighting.

However, at the track, the ambient lighting as well as the material used to construct the course needs to be accounted for. The auto-contrast algorithm scans for the highest and lowest data from all the phototransistors at any particular moment and scales all of their data to a range of $00 to $FF. Even tiny variations in contrast (beige carpet with off-white masking tape) can be seen. This has a secondary advantage of allowing the rest of the software to expect a certain value from the sensors regardless of conditions.

A short line-following course; from start to finish

(Click the above picture to see the movie of a short line-following course; from start to finish)

Light Line Following

In the above movie, Sweet is started via the tailpipe switch.

The robot automatically recognizes it’s on a light-colored line (ordinary masking tape) on a less-light-colored background (mottled tan carpet). The infrared translucent tape and poor contrast carpet aren’t a problem.

The robot executes a 90-degree turn. The two rows of detectors help here. In reality, reliable sharp-turn performance hasn’t been programmed yet.

The robot stops when the line ends, as it recognizes that both brightness and contrast decrease.

Speed is fairly slow with small, knobby tires and 10 volts of AA power. With larger LEGO tires and 18 volts, Sweet’s speed more than doubles.

Maybe the robot should honk at intersections?

(Click the above picture to see the movie)

Dark Line Following

In the above movie, Sweet passes through an intersection.

The robot automatically recognizes it’s on a dark-colored line (blue masking tape) on a lighter-colored background (crosshatch patterned linoleum). Note that the line doesn’t need to be black, just darker than the remainder of the surface. If started without a line, Sweet can somewhat follow the crosshatch pattern.

Coming at 'cha in 3D

(Click the above picture to see the movie)

Split Path

In the above movie, Sweet passes through the prior intersection from a different direction. The split path is slightly more confusing, but the robot ultimately avoids getting split down the middle.

This is a nice shot of the bar-graph and the floor LEDs.

Sweet waves goodbye

(Click the above picture to see the movie)

Drive Bye

I’m very happy with how well Sweet has turned out.

The auto-contrast, auto-detect line-following software worked better than expected. The printed circuit boards and surface-mounted chips raise my electrical engineering standards. In the end, the bar-graph display and candy container made the project a whole lot of fun.

Complete instructions on making a line-following robot (Sandwich) appear in the book Robot Building for Beginners.