Constant-Current Driver Using OPAMP Peripheral

The microcontroller's (MCU’s) OPAMP peripheral can be used to provide a constant-current drive to a load, for example, a light-emitting diode (LED). The schematic diagram of the constant-current driver is shown in the figure below. Only one component is required in addition to the load and MCU: Resistor R3 for converting current into voltage. The remainder of the constant-current driver is provided by the OPAMP peripheral inside the MCU.
Figure 1. Schematic Diagram of a Constant-Current Driver Using OPAMP Peripheral

As shown in the schematic diagram, op amp OP0 inside the MCU’s OPAMP peripheral is configured with the non-inverting input (+) connected to the wiper of the internal resistor ladder. The top of the resistor ladder is internally connected to VDD, and the bottom is internally connected to ground. Therefore, the resistor ladder acts as a voltage divider that generates a reference voltage, VINP, on the non-inverting input of the op amp. The value of the reference voltage can be determined using the resistor divider formula: VINP = (R1/(R2+R1))VDD.

The output of the op amp, OP0OUT, is connected to one side of the load, and the other side of the load is connected to both R3 and the inverting input (-) OP0INN. Since the op amp input has a high impedance, the current flowing through the load will also flow through R3 to ground. The voltage generated across R3 will be proportional to the current through R3. The feedback loop created will cause the op amp to adjust its output voltage such that the voltage on its inverting input is the same as that on its non-inverting input, VINP. The current flowing through R3 and the load will then be i = VINP/R3.

As an example with realistic component values, consider the scenario where VDD is 3.3V, and R3 is 205Ω. If the resistor ladder wiper setting is selected as WIP7 so that R2 is 15R and R1 is 1R, the ratio R1/(R2+R1) is (1/16), and VINP will be (1/16)VDD or 0.21V. The constant current through the load will then be VINP/R3 or 1.0 mA.

Because the OPAMP peripheral has eight different selections for the wiper position, determined by the value written to the MUXWIP bit field in the OPAMP.OPnRESMUX register, it is possible to select eight different current values under firmware control. The following table displays the current value provided by each MUXWIP bit field value.
Table 1. MUXWIP Selection of Current Value
MUXWIP Selection Description Voltage Divider Ratio Current Value
WIP0 R1 = 15R, R2 = 1R 15/16 0.9375 VDD/R3
WIP1 R1 = 14R, R2 = 2R 14/16 0.875 VDD/R3
WIP2 R1 = 12R, R2 = 4R 12/16 0.75 VDD/R3
WIP3 R1 = 8R, R2 = 8R 8/16 0.5 VDD/R3
WIP4 R1 = 6R, R2 = 10R 6/16 0.375 VDD/R3
WIP5 R1 = 4R, R2 = 12R 4/16 0.25 VDD/R3
WIP6 R1 = 2R, R2 = 14R 2/16 0.125 VDD/R3
WIP7 R1 = 1R, R2 = 15R 1/16 0.0625 VDD/R3

If more precise control of the current value is required, the MCU’s digital-to-analog converter (DAC) can be internally connected to the non-inverting (+) input of the op amp as shown in the figure below. This is achieved by writing the DAC setting to the MUXPOS bit field of the OPAMP.OPnINMUX register. In this case, the current value is given by the formula i = VDAC/R3, where VDAC is the output voltage of the DAC.

Figure 2. Schematic Diagram of a Constant-Current Driver using OPAMP and DAC