Example: Safely Toggle >4mA Devices with GPIO
Using a MOSFET to safely work with devices that draw more than 4mA of current
Looking at the datasheet for the LPC2148, we can see that the GPIO pins on the LPC2148 -- in fact the entire LPC2000 family -- can only safely supply or sink (pull to GND) a maximum of 4mA each (see "HIGH-level output current" and "LOW-level output current" on page 27), and the MCU itself is limited to a total of 300mA (100mA "supply current per pin" x 3 available supply pins). Since even something as basic as an LED typically draws around 20mA, this 4mA upper limit is much too low to safely hook up most devices directly to a GPIO pin ... especially if we are using several such devices on our board.
While you can almost certainly get away with hooking one or two 20mA LEDs directly to a GPIO pin -- many people do exactly this -- it isn't recommended, and you may want to search for an LED that draws less current (2ma, 5mA and 10mA varieties also exist).
One of the easiest ways to stay below the 4mA limit on GPIO (and the 300mA limit on the 2148) is by using something called a MOSFET -- short for metal–oxide–semiconductor field-effect transistor (see MOSFET on Wikipedia for all the dull technical details). While the name sounds complicated, the device essentially acts like a programmable on/off or opened/closed switch: If the 'gate' is open, electrical current enters on one end, and flows through the MOSFET out the other end causing a 'complete' circuit (and turning your LED or connected device on). If the gate is closed, the electrical current is interrupted and the device will be turned off. The advantage of using a MOSFET is that the current flows through the MOSFET and not through the LPC2148. This means that you can safely use GPIO to turn on or off devices that require fairly large amounts of electrical current, without overheating or risking damage to your MCU. (The 'gate' on the MOSFET is exceptionally sensitive and requires almost no electical current to 'open' or 'close' it.)
While there are two main types of MOSFETs -- N-Channel and P-Channel -- the type that you need to turn something like an LED on or off is an N-Channel MOSFET. To see how you would hook up an LED using an N-Channel MOSFET and a GPIO pin, take a look at the following schematic:
You should be able to understand fairly easily how the MOSFET works in this case. The current is coming from an external 3.3V source (external meaning simply that it isn't coming directly from the LPC2148), it flows through a current-limiting resistor (R1, used to protect the LED from burning out), through the LED, and into the MOSFET (Q1). If the 'gate' on the MOSFET is open (using the GPIO pin that it is connected to on your MCU), the current will continue to flow through the device to GND and the LED will turn on. If the 'gate' on the MOSFET is closed, the LED will remain off since there is no connection to GND available.
The 2N7002 (surface-mount SOT23 package) or 2N7000 (breadboard-friendly TO-92 package) used in this example is a common and inexpensive N-Channel MOSFET that serve as good general-purpose device. They can easily be found online or in any self-respecting electronics store.
And that's it. Your code will be exactly the same as if you had the LED hooked up directly to the GPIO Pin -- you set the GPIO pin to output with IODIR, using IOSET and IOCLR to turn the LED on or off) -- but you now have a much safer way to work with high current devices without any risk of damaging your MCU!