Σάββατο, 30 Αυγούστου 2014

Microcontrollers and LEDs

The (somewhat lengthy) Introduction

Arduino is breeding a new generation of hackers! Those of you old enough to remember the 80s will probably have fond memories of programming a home computer of that era: the ZX Spectrum, the Commodore 64, the TI-99 and all the other wonderful machines that wouldn't do much unless you could use their BASIC language (or learn machine code!) Sure, you could get ready made programs and games on cassette(!) but there were many who would buy them just for the fun of programming. The hardware had a lot of limitations, sure, but the mere joy of squeezing every single bit of power from the machine was unsurpassed.

Computers have evolved a lot since then, but also lost a lot: the interactiveness of the first computer languages, the simplicity of programming, the single point of entry: you would start learning BASIC. What about now? Where does a beginner start programming? Sure, there are many choices, in both programming languages and operating systems. Most of them are free. But here is the problem: the choice is too big. If that is not enough, the changes are rapid and most people won't be able to follow the latest trend or hype: first it's C++, then it's Java, tomorrow is Python and so on. All very nice,  but where does a beginner start? Confusion...

There are other problems too: your first (simple) programs will not be very impressive by today's standards. They will probably be command line and text driven (mind you, I've written a book on learning programming by writting games in Python and Pygame but it's in Greek only for the moment I'm afraid. You can get a PDF copy of it here). People get bored quickly writing the same old computational programs in modern PCs full of colors, sounds and multimedia. Trust me, I teach these subjects. Programming is now mostly viewed like a chore (we are teaching it the wrong way).

Microcontrollers to the rescue! Actually, they share a lot more than you think with the home computers of the 80s.

  • They are "underpowered" (by todays CPU standards)
  • Their programming is simple and has immediate (and spectacular) results
  • They are actually made to be "actively hacked" rather than "passively used"
They also combine tinkering with both hardware and software,  making them suitable for both computing and electronic engineering hackers!

Most people that are comfortable with programming don't necessarily have adequate knowledge of electronics: some basic stuff is required even at the beginner level: how do you connect a LED to a microcontroller output? Let's see.

Connecting a LED to a Microcontroller Output

You have to keep the following things in mind:
  • Some microcontrollers have 5V outputs (arduino), others have 3.3V or less (MSP430)
  • Some microcontrollers can provide enough current (in other words, they have adequately low output resistance) to drive a LED directly (with a suitable resistor). Arduino can drive LEDs with no problems.
  • Other microcontrollers like MSP430 have limited output current capability. You might be able to drive a LED from them directly but at risk of damaging the MCU.
  • No LED can operate directly from an MCU output: you will need a to connect a resistor in series. Typical LEDs operate at low voltages, depending on their color. For example, red leds need about 1.5V. Fancier colors like blue and white need higher voltages.
Assuming you are using a microcontroller like Arduino, all you need to connect your LED to an output is a suitable current limiting resistor.

Connecting a LED to an Arduino

Connecting a LED to an arduino  is very simple, since the outputs of the MCU can source enough current by themselves. With the following facts in mind it is easy to calculate the resistor:
  • An "active" arduino output provides 5V
  • A typical red LED needs 1.5V
  • About 10mA through the LED are usually sufficient for a good brightness.
Our circuit looks like this:


 Calculating R1 is simple:



We know Vout is 5V, Vled is 1.5V and we need 10mA for the LED. Solving for R1:


 Feel free to adjust this a bit up or down to use a value actually available in the E12 series of resistors (either 330Ω or 390Ω will do). Use this same calculation for other LEDs, adjusting for current and LED color (voltage drop) accordingly.

Connecting a LED to an MSP430 (or other low power MCU)

Low power MCU cannot (and should not) drive LEDs directly. Instead, use the output from the MCU to switch on a transistor that carries the load:


Although we show a separate 5V supply for the LED, if you are using a TI launchpad board, you can source this directly from its 5V output pin.

We now have two resistors to calculate, R1 and R2.

Starting with R2, the equation for the output part of our circuit is:





We will be operating our transistor as a switch. In this configuration the transistor is used in two states: cut off (where no current flows) or fully on (saturation) where the transistor shows almost no resistance (the collector-emitter voltage is zero).

In an ideal transistor (which exists only in... an ideal world) the Vcesat would be zero, but in practice it will be around 0.2V.  We still need about 10 mA for the LED. Solving the above equation for R2 yields:


Conveniently, this is an E12 value as well :)
You may adjust a bit up or down if you have happen to not have the exact value.

Since we know the collector current wil be 10mA, we can easily calculate the base current and resistor R1. Assuming the transistor has b (hfe) 300:



The equation for the input:


Solving for R1:

 

69K is not a value of the E12 series but feel free to replace it with either 68K (little more current) or even 56K (if your transistor has a lower hfe).

In any case, you will only be drawing a minute and absolutely safe amount of current from the MCU output: around 30 μΑ!

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου