Wednesday, July 28, 2021
Home » Bonus Content » PICAXE Part 4


Note: This is supplemental material for the article in ROBOT magazine, Sept/Oct 2012, by Eric Ostendorff. The subject is adding sensors to a PicAxe 20M2 mobile robot. FYI, a companion article about using a RoboVoice speech module was deleted from the magazine. Some of the code listed here runs at 16 MHz in order to support the 9600 baud serial communication that module requires.

Sensor mounting: Here’s a video showing how the three different sensors are physically mounted on the robot:

Batteries & Voltage Regulator: The upgraded power schematic is shown in Photo 1. Five NiMH AA batteries put out nearly 7 volts when fully charged. Photo 2 shows a simple battery trickle charger schematic (125 mA) made from a wall wart and a LM317T in constant current mode. The low-dropout LM2940 voltage regulator shown in Photo 3 will supply regulated 5.0V to our board until the battery voltage drops down to ~5.5V or less. In comparison, a standard LM7805 regulator would require over 7 volts input to retain 5.0V output. The 10K trimpot across the batteries is a voltage divider for our battery monitor. With batteries fully and freshly charged, adjust trimpot for 4.0V into Picaxe pin 3 (C.7 ADC input). Run this code:

fvrsetup FVR4096 ; set Fixed Voltage Reference to 4.096 V

adcconfig %011 ; use FVR as ADC Vref+, 0V Vref-

readadc10 C.7,W0 ; read battery voltage on pin C.7, save as W0

w0=w0/10 ; scale max 1024 value down to 100 percent battery

w3=w0*341/50 ; experimental calibration to real voltage, 627=6.27V

Variable W0 will contain the percentage of battery full charge and W3 can be experimentally calibrated to show real battery voltage. Your code can periodically check the battery voltage and indicate if a recharge is needed.

Code: readsharp.bas

Analog sensors: That same code above can read any analog voltage (5V or less) on any of the 20M2’s eleven ADC-capable input pins. Sensors with analog outputs under 5 volts can be hooked directly to one of those ADC pins. Variable resistance sensors such as photocells and thermistors can be read in a voltage divider configuration. It’s handy to use the LED readout to display the relative analog voltage. This video shows the robot reading a Sharp GP2D120 distance sensor and controlling the LED display, while also displaying serial data through the programming editor:

Code: readsharp.bas

In a pinch, you can use an analog sensor as a digital sensor. Using that same Sharp IR sensor and the same wiring hookup, you can simply read the sensor as high or low. The PicAxe’s TTL input pins act like a comparator: anything below ~1.3V reads low, above 1.3V reads high. Let’s say you had 8 Sharp proximity sensors all hooked up to port B (pins B.0 to B.7), you could check all 8 sensors with one ultrafast look at PINSB, which would tell you instantly if and which sensors were triggered.

Read All Sensors: Displays readings from Sharp IR module, SRF-04 ultrasonic module, battery voltage, and counts from left & right IR receivers.

Code: readallsensors.bas

Wall following: Uses Sharp IR distance measuring module to measure sideways distance to wall, and a SRF-04 ultrasonic module looking forward to avoid hitting walls and obstacles.

Code: wallfollow.bas

Beacon tracking: Thus far we’ve only used one IR receiver for simple IR control using a Sony TV remote. Now will track to different LM556 beacons, schematic shown in Photo 4. Since these beacons are cheap and simple, we could have dozens of them all over a house for a true free-range robot. No lenses are used, just standard 5 mm IR LEDs and compatible IR receivers in optical blinders. Photo 5 shows a top view of the IR blinders and three IR beacons. In a nutshell, these beacons send modulated pulses of 38 kHz carrier which the robot’s IR receivers detect. By counting pulses, the robot’s twin differential receivers can locate and steer the robot to any given beacon:

Code: trackIR.bas

Many things IR operate near 38 kHz; you may experience some overlap. To reduce potential interference, I use the lowest power for my beacons and make them very directional. Detectability and range will vary with conditions. I get 20+ ft range using high power 940 nm IR LEDs from Junun and Radio Shack, using a 560-ohm series resistor. Lower resistance will increase signal & range, but you may run into interference and signal reflection problems. You can add more LEDs in parallel (each with its own series resistor) to make several directional IR beams from each beacon.
940 nm IR LED:

The sample code listed here and demonstrated in the video does not distinguish between beacons, but the robot can easily detect at least four different modulation frequencies if desired. In between auto-generated servopos pulsouts, IR pulses are counted on each receiver for 7 ms (14 ms total) and the count identifies each beacon. The fixed resistor between pins 1 and 2 of the LM556 sets the modulation frequency. The extremes are:

5.6K resistance=1500 Hz modulation, 10-11 counts/7 ms

16K resistance=550 Hz modulation, 4-5 counts/7 ms

Since there is some sampling and rounding error in counting, beacon frequencies must be spaced far enough apart for reliable reading. I made 4 unique beacons that could be reliably distinguished by the robot: 4-5, 6-7, 8-9 and 10-11 pulse counts per 7 ms interval. By alternating frequencies, you could create a large network of beacons for household navigation.

Of a hundred various 38 kHz IR receiver modules which can receive a Sony TV code, only a few are “continuous signal acceptable” for use with our simple LM556 beacons. The best and most sensitive I’ve found is Vishay’s TSOP4038, downright cheap at 70-odd cents: and

Radio Shack’s 276-640 IR receiver ($4.19) works ONLY if it has an integral silver metal bracket with leads pre-bent at 90 degrees. They also sell an all-black version with straight leads under the same P/N which does NOT work.

The Goldmine has these Rohm units which also work:

Alternatively, you could make a beacon system which operates at 56 kHz to reduce 38 kHz noise and potential interference to other IR systems in your house. That same 38 kHz beacon circuit can be adjusted to output a 56 kHz carrier signal. These 56 kHz IR receiver modules from Pololu are continuous signal acceptable:

They will still detect a standard 38 kHz TV remote, which blasts an IR signal so strong that it overwhelms any IR receiver in range.

Words by Eric Ostendorff