Make: Electronics

Home > Other > Make: Electronics > Page 40
Make: Electronics Page 40

by Charles Platt


  Figure 5-121. This 3D rendering shows a possible configuration of the light-seeking cart, with two photoresistors enclosed in small tubes to restrict their response to light.

  Another idea is to rewire your cart so that it actually runs away from the light. Can you imagine how this might be done?

  Just one more thought: if you use infrared photoresistors, you can control your cart with beams from infrared LEDs, in normal room lighting. If you and a couple of friends all have infrared transmitters, you can get your cart to run from one of you to the next, like an obedient dog.

  This takes us about as far as I’m going to go into robotics. I urge you to check out the sites online if you want to pursue the topic further. You can also buy a wide variety of robot kits, although of course I feel that it’s more fun to invent or develop things for yourself.

  All that’s left now is to perform one last introduction: to a device that should make your life much easier, even though the device is much more complicated than anything we have dealt with so far.

  Experiment 34: Hardware Meets Software

  Throughout this book, in accordance with the goal of learning by discovery, I have asked you to do an experiment first, after which I’ve suggested the general principles and ideas that we can learn from it. I now have to change that policy, because the next experiment involves so much setup that it’s only fair to tell you what to expect before you begin the preparations.

  We are about to enter the realm of controller chips, often known as MCUs, which is an acronym for micro controller unit. An MCU contains some flash memory, which stores a program that you write yourself. The flash memory is like the memory in a portable media player, or the memory card that you use in a digital camera. It needs no electricity to power it. In addition, the chip has a processor which carries out the instructions in your program. It has RAM to store the values of variables on a temporary basis, and ROM, which tells it how to perform tasks such as sensing a varying voltage input and converting it into digital form for internal use. It also contains an accurate oscillator, so that it can keep track of time. Put it all together, and it’s a tiny computer that you can buy for under $5.

  Let’s suppose that you have a greenhouse where the temperature must never fall below freezing. You set up a temperature sensor, and you have two different heaters. You want to switch on the first heater if the temperature falls below 38° Fahrenheit. But if, for some reason, that heater is broken, you want to switch on the second, backup heater when the temperature goes below 36° Fahrenheit.

  Programming a MCU to take care of this can be very simple indeed. You could even add extra features, such as a second temperature sensor, just in case the first one fails, and you could tell the chip to use whichever sensor gives a lower reading.

  Another application for a MCU would be in a fairly elaborate security system. The chip could monitor the status of various intrusion sensors, and can take various preprogrammed steps, depending on the sensors’ status. You could include delay intervals, too.

  Many MCUs have additional useful features built in, such as the ability to control servo motors that turn to a specific angle in response to a stream of pulses. Servos are widely used in radio-controlled model boats, airplanes, and hobby robotics.

  Perhaps you are now wondering why, if MCUs can do all this, haven’t we been using them all along? Why did I spend so much time describing the development of an alarm system using discrete components, if one chip could have done everything?

  There are three answers:

  1. MCUs cannot do everything. They need other components to help them interact with the world, such as transistors, relays, sensors, and amplifiers. You need to know how those things work, so that you can make intelligent use of them.

  2. MCUs can introduce their own kinds of problems and errors, associated with using software in addition to hardware. I’ll have more to say about this later.

  3. MCUs have limits and restrictions, most obviously their requirement for a 5-volt regulated power supply, and their inability to source or sink much current from each pin. They also demand that you learn a programming language (which differs from one brand of MCU to the next). And to get the program into the chip, you have to be able to plug it into a computer and do a download, which is not always convenient.

  In this experiment, you’ll learn how to write a program for a small and simple MCU, and you’ll transfer the program into it and see how it works.

  Background

  Origins of programmable chips

  In factories and laboratories, many procedures are repetitive. A flow sensor may have to control a heating element. A motion sensor may have to adjust the speed of a motor. Microcontrollers are perfect for this kind of routine task.

  A company named General Instrument introduced an early line of MCUs in 1976, and called them PICs, meaning Programmable Intelligent Computer—or Programmable Interface Controller, depending which source you believe. General Instrument sold the brand to another company named Microchip Technology, which owns it today.

  “PIC” is trademarked, but is sometimes used as if it’s a generic term, like Scotch tape. In this book, I’ve chosen a range of controllers based on the PIC architecture. They are licensed by a British company named Revolution Education Ltd., which calls its range of chips the PICAXE, for no apparent logical reason other than that it sounds cool.

  I like these microcontrollers because they were developed originally as an educational tool and because they are very easy to use. They’re cheap, and some of them are quite powerful. Despite their odd name, I think they’re the best way to get acquainted with the core concepts of MCUs.

  After you play with the PICAXE, if you want to go farther into MCUs, I suggest the BASIC Stamp (which uses a very similar language, but with additional powerful commands) and the very popular Arduino (which is a more recent design, packed with powerful features, but requires you to learn a variant of C language to program it). I’ll say more about these chips later.

  If you search for “picaxe” on Wikipedia, you’ll find an excellent introduction to all the various features. In fact, I think it’s a clearer overview than you’ll get from the PICAXE website.

  Supplies

  Figure 5-122 shows some of the chips in the PICAXE range. I’ll be telling you how to use the smallest—the 08M—which costs less than $5 and is cheaper than any other MCU that I’ve found. It has only 256 bytes of memory to store a program (not gigabytes, megabytes, or kilobytes, just 256 bytes!), but you’ll be surprised how many possibilities this amount allows for. Figure 5-123 shows a closeup of the 08M with its legs safely embedded in a piece of conductive foam.

  Figure 5-122. A page from the PICAXE catalog lists only some of the chips that are available. What began as an educational aid has become a useful prototyping tool.

  In the United States, there are three distributors of this chip:

  http://www.advancedmicrocircuits.com

  http://www.phanderson.com/picaxe

  http://www.sparkfun.com

  I like P. H. Anderson for its grass-roots hobbyist approach, and they have good prices if you want to buy multiple chips. But SparkFun Electronics offers other associated products that you may find interesting.

  All the distributors will want to sell you “starter kits,” such as the one in Figure 5-124, perhaps because the PICAXE itself is so cheap that it doesn’t offer much of a profit margin. Still, for our purposes, you should buy the chip as a standalone item. And buy two of them, just in case you damage one (for example, by connecting voltage to it incorrectly).

  Figure 5-123. When supplied by one of its American distributors, a PICAXE 08M arrives embedded in a little square of conductive foam. The chip is the same size as a 555 timer but has the power of a tiny computer.

  Figure 5-124. A typical PICAXE kit includes a p
rinted circuit board, which you may not really need, and some other not-entirely-essential items. But the 3.5-mm stereo jack socket (top, center) is absolutely necessary.

  To download your programming instructions into the chip, first you’ll type the instructions on a computer, and then you’ll feed them through a cable into the PICAXE memory. So you’ll need to buy a cable, and you’ll need software to help you to write the program.

  You can use the PICAXE with a serial cable, but I don’t recommend it. The old RS-232 serial communications standard on PCs is pretty much obsolete, and PICAXE has recognized this by offering a USB cable (which contains a serial converter inside its plug). The USB cable is a little more expensive, but is also simpler and compatible with Apple computers. From any of the U.S. distributors, buy USB cable part AXE027, also sold as part PGM-08312 by http://www.sparkfun.com (quantity: 1). The cable is shown in Figure 5-125.

  Figure 5-125. The USB download cable made for use with the PICAXE terminates in a 3.5-mm audio plug. This should not be inserted in any audio device. It establishes a serial connection with a computer, allowing program code to be downloaded into the chip.

  To write your software and send it down the wire to the chip, the PICAXE Programming Editor is the tool of choice. It comes in only a Windows version. For those who prefer Mac OS or Linux, you can get a free download of another piece of software known as AXEpad, which has fewer features, but will do the job. All the downloadable software is freely available from http://www.rev-ed.co.uk/picaxe/software.htm.

  Finally, you need a 3.5-mm stereo audio socket with solder connections. The reason for this is that the manufacturers of the PICAXE have used a stereo audio plug on the free end of their USB cable, and you have to be able to plug it into something. The PICAXE breadboard adapter, SparkFun stock number DEV-08331, contains the necessary stereo socket in addition to a few other little items. Quantity: 1. See Figure 5-126.

  Figure 5-126. Closeup of the 3.5-mm stereo socket that is used with the USB download cable.

  Oddly enough, the USB cable is the most expensive item on the list, because of the electronics hidden inside it.

  Software Installation and Setup

  Now you have to go through a setup procedure. There is no way around this. Here is what you will be doing:

  1. Install a driver so that your computer will recognize the special USB cable.

  2. Install the Programming Editor software (or AXEpad for Mac/Linux) so that your computer will help you to write programs and then download them into the chip.

  3. Mount the PICAXE on your breadboard and add the socket to receive downloads.

  These steps are explained in the following sections.

  The USB driver

  Fair warning: If you go to the PICAXE website and try to use its search function, it probably won’t find what you want. Search for “USB Driver,” for instance, and it will pretend it has never heard of such a thing.

  The PICAXE home page also has irritating drop-down menus that tend to disappear just when you’re about to click on them, but at the time of writing, you can bypass these issues by going straight to the Software Downloads section at http://www.rev-ed.co.uk/picaxe/software.htm.

  Scroll down past all the software until you get to Additional Resources. Look for the AXE027 PICAXE USB Download Cable. At first glance, it looks as if they want to sell you a cable, but in fact this is the list of drivers. Double-click the one appropriate to your computer, and choose a destination on your computer for the download—a place where you will find it easily, such as your desktop.

  Be careful not to download the driver for the USB010 USB-Serial adapter by mistake. The USB-Serial adapter is something else entirely.

  The download will leave you with a zipped file folder. You will have to unzip it. On Windows XP, right-click the folder and choose “Extract all.” View the extracted files and you will find a PDF installation guide. Linux and Mac users can find instructions currently stashed at http://www.rev-ed.co.uk/docs/AXE027.pdf.

  When installing the driver on a Windows platform, here are a few tips to minimize your exasperation level:

  1. Remember, the special USB cable contains some electronics. It is not just a cable, but a device designed for interacting with a PICAXE chip. Don’t try to use it for anything else!

  2. You have to plug the cable into a USB port before you install the driver, because your computer will need to verify that the driver matches the cable.

  3. You must not attach the PICAXE to the other end of the cable until after you have installed the driver.

  4. Every USB port on your computer has a separate identity. Whichever one you choose when you first plug in the cable, you should use that port every time in the future. Otherwise, you will have to repeat the process of telling your computer what the cable is.

  5. Bearing in mind Tip #4, you should avoid using the cable in a standalone USB hub.

  6. The cable is fooling the PICAXE into thinking that it’s talking to a serial port on your computer. Those “communication” ports are known as COM1, COM2, COM3, or COM4. When you install the driver, the installer will choose one of those COM ports for you, and later you will have to know which one it is. The PDF guide should help you through this procedure. Unfortunately, you cannot skip it.

  The Programming Editor software

  If you have come this far, you’re ready for the next big step, which is much easier. You need the PICAXE Programming Editor, available for free on the Software Downloads web page where you found the USB driver. (If you are using a Mac or Linux, you will need AXEpad, which is on the same web page.)

  Downloading and installing the Programming Editor should be simple and painless. Once you have done that, you should find that it has placed a shortcut on your desktop. Double-click it, go to View→Options, and in the window that opens (shown in Figure 5-127), click the Serial Port tab. You should see a dialog box like the one in Figure 5-128. Now make sure that the Programming Editor is looking at the same COM port that was chosen by the USB driver. Otherwise, the Programming Editor won’t know where to find your PICAXE chip.

  In the Programming Editor, go to View→Options and click the Mode tab, then click the button to select the 08M chip.

  Figure 5-127. This screenshot shows the options window of the PICAXE Program Editor, which you must use to select the type of chip that you intend to program (in our case, the 08M).

  Figure 5-128. Another screenshot of the options window shows the second essential choice that you must make: selection of the COM port that the installer chose on your computer.

  Are we having fun yet? Obviously not, but you’re through with software hassles for the time being. The last step before you’re ready to use the PICAXE is to mount it, and its socket, on your breadboard.

  Setting up the hardware

  The PICAXE 08M looks like a 555 timer. (Other chips in the PICAXE range have more pins and more features.) It requires a properly regulated 5 volts, just like the logic chips you dealt with previously. In fact, the PICAXE people are rather emphatic about protecting it from voltage spikes. They want you to use two capacitors (one 100 μF, one 0.1 μF) on either side of an LM7805 regulator. This seems like overkill, but the PICAXE is more inconvenient to replace than a 555 timer. You certainly can’t run down to RadioShack to buy one. So let’s do what the manufacturer says, just in case, and set up a breadboard as shown in Figures 5-129 and 5-130.

  Figure 5-129. PICAXE documentation specifies a 100 μF and 0.1 μF capacitor on the input side of a 5-volt regulator, and a similar pair of capacitors on its output side. On a breadboard, they can be arrayed like this.

  Figure 5-130. The actual components for power regulation, applied to a breadboard, delivering 5 volts (positive and negative) down each side.

  Now for the chip itself. Note that the pins for positive and negative powe
r are exactly opposite to those for the 555 timer, so be careful!

  Set up your breadboard following the schematic shown in Figure 5-131. Note that I am showing the stereo socket on its underside, because I think that’s how you’ll have to use it with the breadboard. If you try to stick its pins into the holes in the board, they will fit, but when you insert the plug into the socket, the thickness of the plug will tend to raise the socket up so that it loses contact. I really think the way to go is to solder wires to the pins on the socket and push the wires into the breadboard. See Figure 5-133.

  Figure 5-131. The schematic of a test circuit for the PICAXE 08M shows the underside of the stereo socket, the essential 10K and 22K resistors on the input pin, and an LED to show an output from the chip.

  Be aware that the PICAXE manual shows things differently (although I have retained their labeling convention for the parts of the socket and the parts of the plug, identified as a, b, and c).

  One little detail about the socket that is commonly supplied for use with the PICAXE: typically it has two pairs of contacts for the connections labeled b and c in the manual, and in my diagram. When you solder a connection, your solder joint should include both of the contacts in each pair, as shown in Figure 5-132.

  Remember that the PICAXE must have 5 volts DC, and remember that your voltage regular will deliver this voltage reliably only if you give it extra voltage on its input side. If you provide it with 9 volts, that will provide a good amount of headroom.

 

‹ Prev