Silicon ChipPICAXE In Schools, Pt.4 - September 2005 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Publisher's Letter: Do-it-yourself seismograph a great project for schools
  4. Feature: Adaptive Cruise Control by Julian Edgar
  5. Review: iBurst Mobile Broadband by Ross Tester
  6. Project: Build Your Own Seismograph by Dave Dobeson
  7. Project: Bilge Sniffer by John Clarke
  8. Project: VoIP Analog Phone Adaptor by Ross Tester
  9. Project: The Mudlark A205 Valve Stereo Amplifier, Pt.2 by David Whitby
  10. Feature: PICAXE In Schools, Pt.4 by Clive Seager
  11. Salvage It: Scrounging & using shortwave radios by Julian Edgar
  12. Book Store
  13. Vintage Radio: The Sprague 500 multi-band receiver by Rodney Champness
  14. Advertising Index
  15. Outer Back Cover

This is only a preview of the September 2005 issue of Silicon Chip.

You can view 36 of the 112 pages in the full issue, including the advertisments.

For full access, purchase the issue for $10.00 or subscribe for access to the latest issues.

Items relevant to "Build Your Own Seismograph":
  • Seismograph PCB pattern (PDF download) [04109051] (Free)
Items relevant to "Bilge Sniffer":
  • Bilge Sniffer/Alcohol Meter PCB [05109051] (AUD $10.00)
  • RBT BAC Checker/Bilge Sniffer PCB pattern (PDF download) [05109051] (Free)
  • Bilge Sniffer front panel artwork (PDF download) (Free)
Items relevant to "VoIP Analog Phone Adaptor":
  • VoIP Analog Telephone Adaptor PCB pattern (PDF download) [03109051] (Free)
  • VoIP Analog Telephone Adaptor front panel artwork (PDF download) (Free)
Articles in this series:
  • The Mudlark A205 Valve Stereo Amplifier (August 2005)
  • The Mudlark A205 Valve Stereo Amplifier (August 2005)
  • The Mudlark A205 Valve Stereo Amplifier, Pt.2 (September 2005)
  • The Mudlark A205 Valve Stereo Amplifier, Pt.2 (September 2005)
Items relevant to "PICAXE In Schools, Pt.4":
  • PICAXE-08M BASIC source code for "PICAXE in Schools", part 4 (Software, Free)
Articles in this series:
  • What’s this? Free PC Boards for Schools? (May 2005)
  • What’s this? Free PC Boards for Schools? (May 2005)
  • PICAXE In Schools, Pt.2 (June 2005)
  • PICAXE In Schools, Pt.2 (June 2005)
  • PICAXE In Schools, Pt.3 (July 2005)
  • PICAXE In Schools, Pt.3 (July 2005)
  • PICAXE In Schools, Pt.4 (September 2005)
  • PICAXE In Schools, Pt.4 (September 2005)
  • PICAXE In Schools; Pt.5 (November 2005)
  • PICAXE In Schools; Pt.5 (November 2005)

Purchase a printed copy of this issue for $10.00.

PICAXE Part 4: Making Things Move By Clive Seager* In Part 3, we used our Schools Experimenter board to generate sound and measure temperature. This month in Part 4, we’ll show you how to control motors, solenoids and even R/C servos! IN THIS ARTICLE, you will learn: • how to interface a motor, solenoid and servo; • how to use PWM to control a motor’s speed; • how to reverse a motor; • how to control a servo. Before we begin, be aware that you must not connect a motor (or solenoid) directly to the PICAXE output pins. The motor will draw more current than the PICAXE can safely supply (20mA) and so will permanently damage it! The easiest way to interface a small DC motor or solenoid to a PICAXE is with the use of a Darlington transistor and diode (see Fig.1). As indicated by its circuit symbol, a Darlington * About the author: Clive Seager is the Technical Director of Revolution Education Ltd, the developers of the PICAXE system. 88  Silicon Chip transistor is actually two transistors in a single package. This configuration produces a very high gain, allowing control of a large collector current with a relatively small base current. Small 1.5V DC motors typically draw little current, so they can be driven with low-power Darlington transistors such as the BCX38C. Examples of this type of motor are the DSE P-8980 “solar” motor and the Jaycar YM-2705 “hobby” motor. For 3V DC motors, the mediumpower BD681 Darlington transistor is a better choice. In this application, the BD681 can pass up to about 300mA without a heatsink. For example, you could drive a low-speed, low-torque “toy” motor such as the DSE P-9000 3V DC motor without problems. For larger motors such as the DSE P-9002, you will need to fit a heatsink to the BD681 – otherwise, it is likely to overheat. A small “U” type heatsink would be sufficient for motor currents up to about 1A. Note that higher voltage motors or solenoids (eg, 12V models) will work with this system, given the appropriate power supply. Of course, the PICAXE chip must still be powered from a 4.5V battery or regulated 5V DC supply but more on that later. As shown in Fig.1, a 1N4001 (or 1N4004, etc.) diode must be connected across the motor to limit back-EMF generated voltage spikes. Without this protection diode, the spikes might damage Q1 and otherwise interfere with normal circuit operation. Another potential problem is with electrical noise, generated by the windings as the motor turns. A 220nF polyester capacitor soldered directly siliconchip.com.au across the motor’s terminals will effectively reduce this noise. In the example given in Fig.1, the motor circuit is connected to output 1 (PIN 1) of the PICAXE micro. The program in Listing 1 shows how to switch the motor on and off every five seconds. Task – connect a small DC motor to your experimenter board, using the breadboard wiring layout in Fig.2. Enter the simple program in Listing 1 to prove that it works. Motor speed control There are two ways of controlling the speed of a DC motor. The first is simply to vary the voltage applied to the motor. For example, if 2V were applied to a small DC motor it will rotate at a lower speed than if 3V were applied. Unfortunately, reducing the applied voltage also reduces the “turning power” (torque) of the motor considerably. In the second method, the full voltage is always applied to the motor but it is switched on and off rapidly. The time that the motor’s supply is switched on is called the mark time, and the time that it is switched off is called the space time, as illustrated in Fig.3. By varying the mark-space (on-off) ratio, the speed of the motor can be varied. This method of speed control is commonly called “PWM” because it is achieved by pulse-width modulation of the applied motor voltage. PWM is an efficient means of speed control and unlike the first (linear) method, torque remains proportionally high. In addition, PWM speed control is easily implemented with a PICAXE microcontroller. The program in Listing 2 demonstrates how it’s done. In this example, a mark-to-space ratio of 1-to-10 is used to turn the motor very slowly. Tip: a small propeller connected to the motor shaft makes it much easier to see the speed difference. Task – experiment with different markspace ratios and observe the variations in motor speed. What is the slowest speed you can achieve? This month, the Schools Experimenter is used to control a small DC motor with the aid of a very simple breadboard circuit. Fig.1: only a few common components are needed to interface a small DC motor or solenoid to a PICAXE. Although not shown here, a 220nF polyester capacitor must be soldered directly across the motor’s terminals. Reversing a DC motor To reverse the direction of rotation of a DC motor, it’s simply a matter of reversing the polarity of its power supply. Obviously, this cannot be achieved with our simple Darlington driver circuit, which provides only siliconchip.com.au Fig.2: here’s how to wire up the motor circuit shown in Fig.1. The BD681 Darlington transistor used here is suitable for switching all low-power 3V DC motors. Don’t forget that 220nF capacitor on the motor terminals. September 2005  89 states of the PICAXE outputs (PIN 1 & PIN 2). The program in Listing 3 demonstrates how to drive a motor, first in one direction and then in the other. Note that before reversing the motor, it is brought to a full stop by taking both outputs low for about 100ms. This is necessary to prevent placing excessive load on the motor and its power supply. Powering the driver Fig.3: by varying the ratio of the “on” to “off” time of the applied voltage, the speed of a DC motor can be varied. This is referred to as “pulse-width modulated” (PWM) speed control. “on-off” control. What we need is a circuit that can switch both supply leads to the motor. Such a circuit requires a minimum of four transistors in a “H-bridge” configuration, as illustrated in Fig.4. In this configuration, the transistors are driven as pairs. When all transistors are off, no current flows and so the motor is off. If Q1 and Q4 are switched on at the same time, current flows from left to right through the motor. If Q2 and Q3 are switched on at the same time, current flows the other way, hence reversing the motor. Note that the top and bottom transistors on one side must not be switched on together as this would result in a short circuit across the power supply rails and certain disaster! Fortunately, there’s no need to wire up a complex H-bridge circuit if you would like to experiment with twoway motor control. Instead, an L293D Driver IC is ideal for the job. It contains four complete “push-pull” drivers (channels), each channel with its own output transistor pair and protection diodes. Two channels are all that’s needed to form a complete H-bridge configuration, allowing two motors to be driven from a single IC. The L293D is available in a standard 16-pin DIL package and includes in-built over-temperature protection. Fig.5 shows a general scheme for controlling two motors, although only one motor is controlled by the PICAXE in this example. Two outputs from the PICAXE are connected to the control inputs of two channels of the L293D on pins 2 & 7. Table 1 shows the response of Motor A to the four possible logic Fig.4: a minimum of four transistors in a “H-bridge” configuration is required to be able to drive a DC motor in both directions. Note that this is not a complete working circuit – it just illustrates the concept. 90  Silicon Chip The L293D should be powered from the same supply as the PICAXE. The positive rail (shown here as +4.5V) connects to pin 16 of the L293D and the 0V rail to pins 4, 5, 12 & 13. A separate input (pin 8) is provided for motor power. If controlling a 3V motor, this input can be connected to the PICAXE +4.5V supply as well. However, for higher voltage motors, pin 8 should be wired to a separate supply of 4.5-36V, as appropriate for the motor. When a higher voltage supply is needed, you may find it more convenient to power your PICAXE circuits from that supply, rather than using a separate 4.5V battery pack. This can be achieved by adding a simple regulator circuit, like that shown in Fig.7. This circuit will accept an input range of 6-17V DC and provide a regulated 5V output for the experimenter board. Similar regulator circuits are available in kit form. For example, both Jaycar (cat. KA-1797) and DSE (cat. K-3594) stock the “Universal Voltage Adapter” kit published in “Electronics Australia”. Note that these kits support user-selectable output voltages that must be set for 5V during assembly. Note also that a heatsink may be required for the L293D when driving larger motors. To avoid this pitfall, we recommend the use of low-power 3V motors for the simple experiments presented here. For those interested in all the details, the L293D datasheet can be downloaded from www.st.com and www.ti.com Task – wire up a L293D circuit on your breadboard and connect it to output 1 and output 2 of the PICAXE, as shown in Fig.6. Write a program that reverses the direction of the motor every time the switch on input 3 is pushed. Fault-finding Is your motor circuit behaving erratically? Chances are, it’s caused by motor noise. Try removing the motor siliconchip.com.au Par t s Lis t 1 BD681 Darlington transistor 1 1N4001 (or 1N4004) diode 1 L293D motor driver IC 1 small 3V DC motor (see text) 1 1.5kW 0.25W 5% resistor 2 220nF 50V polyester (MKT) capacitors 1 3-pin header 1 R/C servo (e.g. Jaycar YM-2760) Where To Buy Parts Fig.5: the L293D driver IC makes it very easy to control one or two small DC motors. In this example, two PICAXE outputs are used to control the direction of a motor (Motor A). If speed control is required as well, then the enable input (pin 1) of the L293D could be connected to a third PICAXE output rather than to the +4.5V rail. Back-EMF protection diodes are included in the L293D, so you don’t need to add them when using this chip. The L293D driver IC can be ordered from MicroZed, see www.picaxe. com.au for more information or phone (02) 4351 0886. Suitable 3V DC motors are available from Dick Smith Electronics and Jaycar Electronics or can be salvaged from old toys. The BCX38C Darlington transistor mentioned in the text is available from Farnell (cat. 425497), phone 1300 361 005. Table 1 Pin 1 H Pin 2 L Function Turn Left L H Turn Right L H L H Fast Stop Fast Stop Fig.6: it’s easy to drive a small 3V DC motor using the L293D IC. The wiring diagram shown here closely follows the circuit in Fig.5, although only two of the ground (0V) pins of the IC are connected. A 220nF capacitor provides additional noise filtering for the motor power input on pin 8. and replacing it with a LED and a 1kW series resistor. If your program then runs as expected, the problem is definitely a result of noise. As a first step, check that a 220nF polyester capacitor is connected directly across the motor terminals as described earlier. Also, try connecting the power supply leads for the motor circuit directly to the battery or power supply output, rather than to the header (H1) on the experimenter board. Extra power supply filtering near the motor may also prove beneficial. Radio control servos Small servos, such as those found siliconchip.com.au The L293D driver chip is ideal for use in small robotic vehicles, such as the PICAXE micro-robot (Part No. AXE120). September 2005  91 Fig.7: advanced experimenters may wish to use a 6V (or 12V) battery or plugpack to power a higher voltage motor or solenoid. In this case, the experimenter board can be powered from the same source by adding a simple regulator circuit. You can either assemble one like that shown here on a small section of prototyping board or buy a kit of parts (see text). in radio control (R/C) models, contain a motor, gearbox and controller board (see Fig.8). A potentiometer (variable resistor) is connected to the output shaft to provide position feedback to the controller board. This allows the controller to accurately position the shaft according to instructions from its host. To control a servo, it is not necessary to interface directly to its motor. Instead, a simple digital connection is made to the integral controller board via the white/yellow signal wire. A servo can be connected directly to the 3-pin header (H2) on the experimenter board. Two pins provide power to the servo, while the third drives the servo’s signal line via output 0 of the PICAXE. If you haven’t installed this header yet, then you should do that next. When plugged into the on-board header, the servo is being powered from the same supply as the experimenter board, so it will be running at less than the 4.8-6V supply usually specified for these devices. If more torque is required, then you will need to connect the supply leads to a separate 6V supply. For a servo to operate, it must receive a pulsed signal every 20ms. The length of this pulse (0.75-2.25ms) determines the position of the output shaft. For example, a pulse length of 1.5ms moves the shaft to the central position. Servo software The Schools Experimenter Board can directly drive an RC-type servo motor. Just connect it to the 3-pin header (H2) on the board and use the demo code shown in Listings 4 & 5. 92  Silicon Chip In Listing 4, you will find a small program that moves the servo to two different positions using for…next loops (described in Part 3). The pulsout command is used to generate the short pulse, where pulsout 1,100 literally means: “output a pulse of length 1.0 ms on output 1”. One drawback with this system is that the coded pulse must be sent every 20ms; otherwise, the servo goes “loose” and moves out of position. This could be quite a problem when you are trying to do other things (eg, waiting for a switch press) in your PICAXE program. Fortunately, the PICAXE servo command is designed to address this very problem! Unlike most other BASIC commands, the servo command (once activated) operates continuously in the background, supplying the servo with its pulse every 20ms. A simple siliconchip.com.au Program Listings What’s Inside A Servo? Listing 1 main: high 1 pause 5000 low 1 pause 5000 goto main Listing 2 Fig.8: exploded view of a typical R/C servo, showing that there’s a lot more than just a motor inside the plastic case. example is given in Listing 5, where a servo arm is moved back and forth in response to the press of a switch on input 3. What’s coming? Can this little 8-pin chip really do more? You bet! Next month, we’ll show you how to control the experimenter’s board using an infrared remote. We’ll also record and playback sounds with an add-on speech module. SC main: high 1 pause 1 low 1 pause 10 goto main Listing 3 main: ' ensure motor is stopped low 1 low 2 pause 100 ' forward direction high 1 low 2 pause 3000 ' stop motor low 1 pause 100 ' reverse direction low 1 high 2 pause 3000 goto main Listing 4 main: for b1 = 1 to 200   pulsout 1,100   pause 20 next b1 for b1 = 1 to 200   pulsout 1,200   pause 20 next b1 goto main Listing 5 main1: servo 1, 100 pause 1000 loop1: if pin3 = 1 then main2 goto loop1 main2: servo 1, 200 pause 1000 loop2: if pin3 = 1 then main1 goto loop2 TAKE YOUR PIC Picaxe.com.au DISTRIBUTOR: MicroZed.com.au Developed for students, & professional performance makes PICAXE the most easy-to-use micro ever: PICAXE “programmer" is two resistors and a 4.5V battery! PHONE (02) 4351 0886 8.30-4.30 AEST Mon-Fri FAX (02) 4351 0889 24 Hours ALL PICAXE ITEMS ON OUR SHELVES! STOCKISTS siliconchip.com.au In AUSTRALIA: In NEW ZEALAND sicom.co.nz altronics.com.au (Retail and Mail Order) oatleyelectronics.com surplustronics.co.nz School Electronic Supplies (John - 03 8802 0628 – School orders only) September 2005  93