Figure 3—Code algorithms in this project act like a hardware peak detector. The diode allows the capacitor to charge and hold the highest voltage applied to VIN. The resister can be sized to slowly leak off the capacitor’s charge (VCAP).