Skip Navigation LinksTutorials > LPC2148 QuickStart Guide > Lesson 2 - Basic Input and Output (GPIO) > Example: Safely Toggle >4mA Devices

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).
How do you safely connect a >4mA device to GPIO?

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:

Connecting a GPIO Pin to an N-Channel MOSFET

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!

Components Used in this Example
While all of the components mentionned above are easy to find, here is a list of the specific models we used in this example (simply because it's what we had laying around):
  1. LED1 - RED 15mcd 20mA 1206 LED 
  2. R1 - Current-Limiting 150 Ohm Resistor  
  3. Q1 - 2N7002E N-Channel MOSFET

  • Facebook
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Furl it!

Comments