# 2 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 V_{DD}, and the
bottom is internally connected to ground. Therefore, the resistor ladder acts as a voltage
divider that generates a reference voltage, V_{INP}, on the non-inverting input of the
op amp. The value of the reference voltage can be determined using the resistor divider
formula: *V _{INP} = (R1/(R2+R1))V_{DD}*.

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,
V_{INP}. The current flowing through R3 and the load will then be *i =
V _{INP}/R3*.

As an example with realistic component values, consider the scenario where
V_{DD} 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
V_{INP} will be *(1/16)V _{DD}* or 0.21V. The constant current through
the load will then be

*V*or 1.0 mA.

_{INP}/R3MUXWIP Selection | Description | Voltage Divider Ratio | Current Value |
---|---|---|---|

WIP0 | R1 = 15R, R2 = 1R | 15/16 | 0.9375 V_{DD}/R3 |

WIP1 | R1 = 14R, R2 = 2R | 14/16 | 0.875 V_{DD}/R3 |

WIP2 | R1 = 12R, R2 = 4R | 12/16 | 0.75 V_{DD}/R3 |

WIP3 | R1 = 8R, R2 = 8R | 8/16 | 0.5 V_{DD}/R3 |

WIP4 | R1 = 6R, R2 = 10R | 6/16 | 0.375 V_{DD}/R3 |

WIP5 | R1 = 4R, R2 = 12R | 4/16 | 0.25 V_{DD}/R3 |

WIP6 | R1 = 2R, R2 = 14R | 2/16 | 0.125 V_{DD}/R3 |

WIP7 | R1 = 1R, R2 = 15R | 1/16 | 0.0625 V_{DD}/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 =
V _{DAC}/R3*, where V

_{DAC}is the output voltage of the DAC.