August 29, 2011 by bretm I just wanted to share some details about a Nerdkits project I'm working on. I wanted a tool to run a controlled amount of current through an LED at a specific duty cycle in order to see how bright the LED is, and also get a reading of the LED forward voltage at that current level. I have the hardware put together (it's really simple), and I'm working on the software. I'll post that when it's done. Here's the circuit, minus all the standard Nerdkit/LCD stuff: I'm using Timer/Counter2 in fast PWM mode to generate a 57600 Hz PWM signal that goes through low-pass filter R1/C1. This generates a voltage from 0V to 5V in 20mV increments that I can control with OCR2A. The current is dropped down by R2, and then Q1 shuts it off completely whenever OC1A goes high. I then use OCR1A to control the duty cycle of the analog pulses. The resulting pulsed current is amplified by Q2, and then current-limited by R3 through R6. I'm using four resistors in parallel just because I only have 1/4-watt resistors and I need to spread the load. I really want about 30 ohms, so I use four times 120 ohms. The total power dissipation needs to be about 1 watt if the duty cycle is set to 100% and Q2 is fully on. This forms a current-sensing resistor. In order to make accurate measurements I need an accurate resistor, so this should have a 1% tolerance. Using four of them should improve the accuracy further, averaging out the individual variance. In actuality I'm using 5% tolerance and then just measuring the actual resistance using my multimeter while the circuit is off, and then hard-coding the actual resistance value into my program. I'm still experimenting with actual values in order to get fine control of the current through the LED. Right now it's too coarse at the lower levels of current. I'm thinking of switching Timer1 and Timer2 around so that I can use 10-bit PWM for the voltage level control, since I only need 8-bit PWM for the duty-cycle control. The down-side to that is the PWM frequency will be lower and it will slow down my measurement rate if I want to keep the same ripple voltage coming out of the low-pass filter. The LCD screen main menu will look like this: ``````Max current <100mA> Max voltage 5.00V Duty cycle 1/10 @1mS Manual Auto `````` Two buttons are used for up/down to select the cursor row. Right now it's on the Max current row. Two buttons for left/right change the current up or down by 1mA. This is just setting the max current for the test--outputs are still zero at this point. Two other buttons change the value up or down by 10 at a time instead of 1. When the cursor is on the bottom row, left will select Manual mode and right will select Auto mode (the left and right arrows will show up next to these). In manual mode, the screen changes to this: ``````Manual Current <100mA> Voltage 4.61V Exit `````` To produce the specified current I slowly ramp up the output voltage until I reach the desired current. The current is measured by measuring the voltage at either end of the current-sense resistor, subtracting the two ADC readings to get the voltage drop, and then dividing by the resistance. LED voltage is measured using using ADC1 and subtracting from 5V. If the cursor is on the second row and voltage is under manual control, the output voltage is slowly ramped up until the desired voltage is measured across the LED, and then the resulting current is displayed. This is a less useful mode because there is only a narrow range of LED forward voltage where the LED will light up and where the circuit is able to supply enough current. Mostly it's here to display the forward voltage for a given current level. It auto mode, the software automatically generates a ramp and calculates the forward voltage at equal current intervals and then lets you scroll up and down the list: ``````Auto 70mA 2.25V ^ 80mA 2.38V 90mA 2.50V