LED & Button

press & let light begin

Never say you are only a software guy. If you want an overall understanding you have to know your hardware. I hope you decide that the next time you will built it yourself. You will have to ride another learning curve nevertheless the excitement pays.

So, you have LEDs connected to your micro's port pins. These LEDs are Diodes that Emit Light when current flows through them (in their “forward” direction). The ones you have require about 20mA each. When 20mA current flows forward into the LED, the voltage across it is around 2V. It is the datasheet that provides the working conditions. So, the rest 3V are across the resistor. For setting the current to 20mA, R1 should be 3V/20mA=150ohms. If less, you provide excessive current, if more you don’t get maximum brightness. Now, 20mA is the maximum and we can be happy with less than than. The resistors on the board could be 180 ohms, 5% accuracy.

Now, the story above holds if the port pin is grounded (logical 0). If it is at 5V (logical 1), there is no voltage difference, no current flows, the LED is off. Remember 0 for on, 1 for off, inverse logic.

So, for every LED we consume 16.67mA and we have 16 LEDs on the board that need 267mA. The question is can the voltage regulator provide such current? Yes, it can, the voltage regulator can give up to 500mA however, it gets hot and we are measuring temperature.

I did some tests putting a few boards in the refrigerator (my wife did not share my excitement) and I realized that heat coming out from the regulator was seriously affecting measurements. A simple way to solve this would be to change regulator. Instead of this, I decided to use 270Ohm resistors that give less current (11.11mA per LED) and the last thing I decided that there is no need for the sensor to be on the board.

For the button the story is similar. This time the port pin has to be configured as input. Depending on the pin voltage, it will read either as 0 (less than 0.7V) or as 1 (more than 4.2V). Better stay close to 0V and 5V, we don't want to be ambiguous.

So, the button is not pressed and due to the open circuit no current flows (no DC flows via the capacitor as well). There is no voltage difference across R, thus the pin stays at 5V or logic 1. If pressed, current flows through the button and the port pin is grounded, logic 0. The current flowing is about 1mA and there is no reason for having more than that, even the internal 50k pull-up could be used instead. Remember, 1 for not-pressed, 0 for pressed, again inverse logic.

And what is this cap doing there? Well, with buttons there is the issue of “button debouncing”. When you press a button the voltage goes up and down many times till it settles. It may need as long as 200ms for settling down, what our micro will read during this period? The capacitor will smooth things out, it acts as a low pass filter preventing fast voltage changes, luckily. For best results we need a resistor and a diode as well, however we may handle debouncing in software.

Links
http://www.theledlight.com/technical.html http://www.epanorama.net/links/opto.html http://ourworld.compuserve.com/homepages/Bill_Bowden/ http://www.ibiblio.org/obp/electricCircuits/
 
 
victor@avrtutor.com