This code example implements a linear low-side current regulator for LEDs using the PIC18F16Q41's digital-to-analog converter (DAC), comparator, and a handful of external components. The operational amplifier (OPA) module on the PIC18F16Q41 can be used in this demo to reduce the value of the shunt resistor or to reduce the current output range of the circuit.
TB3280: Using Operational Amplifiers in PIC16 and PIC18
TB3279: Optimizing Internal Operational Amplifiers for Analog Signal Conditioning
AN3521: Analog Sensor Measurement and Acquisition
Code Example: Linear LED Current Regulator using PIC18F16Q41
- MPLAB® X IDE v5.40 or newer
- MPLAB XC8 Compiler v2.20 or newer
- MPLAB Code Configurator (MCC) Classic v3.95.0 or newer
- 6.8 ohm resistor (current shunt)
- 100k ohm resistor
- This resistor discharges the capacitor passively and ensures the MOSFET remains off if the part is off or the pin is tri-stated. For this example, it is not needed.
- 10k ohm resistor
- 0.1uF capacitor
- N-Type MOSFET (rec. signal transistor, such as a 2N7000A)
- LED
- Breadboard
Before building or operating this demo, ensure that the parts selected for this demo are appropriately rated for the peak power of this circuit.
The parts that dissipate the most power are the 6.8 ohm shunt resistor, the MOSFET, and the LED. Some parts may become warm or hot during operation.
Pin | Function |
---|---|
RB6 | Comparator Output (connect to the RC network) |
RC3 | Current Feedback Input |
RB5 | OPA1IN0+ (see note) |
RC2 | OPA1OUT (see note) |
Note:
If using the operational amplifier, connect the current shunt output to RB5, then the output of the operational amplifier to RC3.
Otherwise, connect the current shunt output to RC3 directly.
This code example regulates the current through the LED(s) to generate a breathing effect. The MOSFET acts as a voltage-controlled current sink. To set the voltage on the MOSFET's gate, and thus the current through the transistor, the comparator compares the voltage on the current shunt (RC3) with the DAC's level. If the current sensed is less than the set level, the comparator turns on. When the current sensed is greater than or equal to the DAC level, then the comparator turns off. The signal is integrated through the 10k ohm and 0.1uF RC network on the gate of the MOSFET to regulate the transistor. To reduce switching noise, the comparator is used in synchronous mode by latching the output through an internal flip-flop.
Using the internal DAC, almost any arbitrary pattern can be created (flickering for candles, linear brightness control, etc...). The breathing effect was chosen to show the ramp up and down of the current through the LED. The GIF below shows the LED breathing effect.
To generate the effect, the DAC generates a triangle wave which is used by the comparator to set the current through the LED according to the following formula:
Iout = Vdac / (6.8 x Gain)
In the unamplified (or gain of 1) configuration, the maximum current is ~151mA, assuming sufficient headroom on the power supply. (The image below shows the power supply saturate and fail to reach the full range.) Using the OPA module with a gain > 1 can either:
- Reduce the current output range. The default gain is 8, which limits the current to a maximum of ~19mA.
- Reduce the size of the current shunt resistor, reducing the power dissipated by the resistor.
Voltage Across the Sense Resistor
Due to the MOSFET acting as voltage controlled current sink, there is some short circuit protection built into the circuit. However, it is not recommended to depend on this circuit as the only means of protection. Prolonged short circuits may cause heating.
This code example can be run at voltages in excess of Vdd. Running at a higher voltage enables more LEDs to be strung together in series. However, some modifications to the circuit should be made to handle the higher voltages and power.
Important! If an I/O associated with the PIC18F16Q41 exceeds Vdd or Vss, then the device will be permanently damaged.
With a few external parts, the analog peripherals on the PIC18F16Q41 can be used to control the current through an LED.