Silicon photo-multiplier (SiPM) gain drifts due to temperature variations and this issue can be a source of relevant errors, for instance when the application requires high accuracy in the evaluation of the photopeak position, especially at low signal levels. In this paper we describe a compensation system that exploits a SiPM as temperature sensor and is able to keep the detector gain constant by means of fine adjustment of its bias voltage. The system is based on a feedback loop which measures the average amplitude of the dark pulses generated by the detector and compares it with a fixed reference, thus automatically correcting the device bias voltage and achieving constant amplitude of the dark pulses in presence of temperature variations. The same bias correction can be also applied to compensate the gain fluctuations of a SiPM not involved in the feedback loop, characterized by the same temperature dependence. The experimental results prove the effectiveness of the compensation system: for the SiPM used in the measurements, considering a temperature span of 10 $^\circ$ C, the system is able to reduce the corresponding variation of the dark pulse amplitude from about 22% to only 0.78%.