Control discharge current using Arduino
A adjustable constant current sink circuit is typically used for this.  @Wesley has already posed a link to a detailed write-up in his comment.  Here's my simplified version.



MOSFET Q21 does the heavy lifting.  The power from the source which we are loading (device under test, in other words) is burned buy this MOSFET.  It should have adequate power rating and heat sink.

Resistor R21 is the current-sense resistor.  As the load current runs through R21, it it creates a voltage drop, which provides negative feedback to OpAmp U21.

OpAmp U21 generate the output which controls the gate of the MOSFET.  It controls the MOSFET in such a way that the voltages at its inverting and non-inverting inputs are equal.  The current (which this circuit sinks from the supply) is proportional to the voltage at the non-inverting input of the OpAmp.

Those are the core components.  R31 and C31 low-pass filter the PWM to create the DC control voltage at the non-inverting pin of the OpAmp.

Related:
How does this constant current sink actually work?  The thread deals with a BJT circuit, but the principle is the same.