
The thermal regulator is responsible for two things:
- Reporting the temperature of the load
- Controlling the fan speed for cooling the load
The thermal regulator accomplishes the first objective by measuring the output of the thermal regulator circuit with the ATMEGA32U4’s ADC. The second objective is accomplished by converting the load’s temperature to a duty cycle. Let’s take a look at the hardware and software of getting the temperature.

The thermistor circuit is shown above; the thermistor is in parallel to C39 and R24. This circuit converts the resistance of the thermistor, which depends on temperature, to a voltage that can be read by the ADC.

The graph shows TOUT as a function of temperature. Unfortunately, the output is not linear, so this conversion must be reversed in the software; that is, the temperature must be calculated from TOUT.

The image above shows deriving the equation for converting voltage to temperature. On the left is the table of temperature and voltage. In the middle is the graphs showing the data, and below that is 1st, 2nd and 3rd order equations for calculating the temperature as a function of voltage. On the right shows the calculated temperatures using the temperature equations, and the errors for each temperature.

The macros above shows how to convert the voltage to temperature. The first, second and third order equations are defined, and then SCH_TR_VOLT_TO_TEMP chooses which order to use. In this case, the second order equation is used.
Now let’s look at how the temperature is used to calculate duty cycle:

In the software, Tmin, Tmax, Dmin and Dmax are defined in macros. For example, the minimum allowed duty cycle will be Dmin, which corresponds to Tmin, and the same for Dmax and Tmax. The equation above shows calculating the duty cycle when the temperature falls between Tmin and Tmax. It should be noted that if t is below Tmin, then the calculated duty cycle will be below Dmin, and if t is above Tmax, then the calculated duty cycle will be above Dmax. However, this is irrelevant in this case since Dmin and Dmax are 0 and 100 respectively, and the PWM code will interpret negative numbers as 0 and values above 100 as 100. Currently, Tmin is set to 35 °C, and Tmax is set to 85 °C. It should also be noted that though the equation above shows the equation using temperature, in the code the duty cycle is calculated using voltages. This is because I coded the voltage-to-duty-cycle code before I calculated the voltage-to-temperature equation. I should update that in the future.
Now that we know how to calculate temperature and duty cycle, let’s look at the code:

The constructor for the temperature regulator is very simple. First, last_time is zeroed, which is used to see if enough time has passed to run the regulation code. Second, enable is set to true. If the temperature regulator is enabled, then duty cycle depends on the measured temperature. If temperature regulator is disabled, then duty cycle will stop changing based on the measured temperature, allowing the user to manually set the fan speed. Third, the target duty cycle is initialized to zero.

Above is the code used to regulate the fan temperature. First, the method sees if enough time has passed to run this code. If it has, then the ADC is read and the result is saved in volts. Then, temperature is calculated using the 2nd order equation, with temp_volt as the argument. Next, enable is checked. If enable is true, then the duty cycle should be calculated using temperature, or temp_volt in this case. If enable is false, then duty cycle is set to target_duty_cycle. target_duty_cycle is initialized to zero, but it can be changed through the user interface.
Next, let’s take a look at the user interface!