Measuring temperature is a favorite first project for manufacturers, along with flashing LEDs, but it’s a bit boring. Let’s try something!
Adding a servo motor allows you to physically react to the measured temperature, such as by moving a valve or knob or the slats of a blind.
Three lines are required to control the servo.
setPWMFrequency(50) dutyCycle = map(A, 90, -90, 102, 512) writePWM(IO01, dutyCycle)
The first command sets the PWM frequency to the usual 50Hz for analog servos. of duty cycle The variable is used to control the servo angle. map() Use the function to calculate the correct pulse width ratio from 102 for 90 degrees to 512 for -90 degrees. (These values are derived from Oxocard’s internal PWM frequency generation. Fortunately, everything is already specified for you.)
Pass the result to output pin IO01 using: writePWM() The servo setup is now complete. However, additional calls are required to move the servo linearly with temperature. map() is required. map() I just explained:
A = map(T, 40, 10, -90, 90) dutyCycle = map(A, 90, -90, 102, 512)
This allows you to map expected indoor temperatures up to 40°C (104°F) to one full swing of the servo and a minimum of 10°C (50°F) to the opposite swing. At 25°C (77°F) the servo should be approximately centered.