TemperatureControllerMCU
TRIAC Controller Heater with PID Control System
Install / Use
/learn @yongxianan/TemperatureControllerMCUREADME
IDE code inside TC3
TRIAC Controller Heater with PID Control System
description
this project use STM32F103C8T6 develop a heating system with PID cotroller, it allows user to set target temperature using the command line interface through the UART communication with PC. Temperature is measure and feedback using Serial Peripheral Interface through MAX6675 K-thermocouple to Digital Converter and K-type thermocouple. The PID algorithm is program inside the microcontroller to control the temperature to a desired temperature. TRIAC is used by microcontroller to control the power delivered to the light bulb (act as a heater). this project can be used in oven for control temperature and other purpose.
list of materials needed for this project:
- TRIAC (control circuit for average current: firing angle)
- Type K Thermocouple
- MAX6675 (for thermocouple)
- STM32F103C8 as programmer (ST-LINK V2)
- stm32f103 V2 smart (MCU)
- 9V transformer
- opto-isolator 4N25 (use in zero crossing circuit for circuit protection)
- opto-isolator 4N32 (use in TRIAC trigger circuit protection)
- comparator LM311N
In fig 1, STM32F103C8T6 use as a programmer for the STM32F103 V2 SMART, STM32F103C8T6 is flash to ST-LINK. 9V transformer provide power for zero crossing circuit and also for zero crossing detection. TRIAC triggering circuit is used for controlling power deliver to the lamp. MAX 6675 with K-type thermocouple is used for temperature feedback from the light bulb. The USB TO TTL is used for communication through the UART to the PC, it allows user to set desired temperature and read actual temperature from light bulb.
zero crossing circuit
creating circuit that output (rising edge/falling edge) whenever input signal crossing zero volt. The idea is using caparator to detect input voltage and compare to a reference voltage then give a digital output. in this circuit, comparator output high when V+ higher potential than 0V, output low (0V) when V+ lower potential than 0V.

(Note that D3 and D4 act as rectifier, provide DC to power the comparator) Opto-couple is used for protecting the circuit from high voltage and current, it used for protecting PC and MCU. From Figure 2, expected output signal (channel B range from 0 - 3.3 V) in oscilloscope when 9 Vrms sine wave signal as input (channel A). The output result will either have rising edge or falling edge when the input sine wave cross zero volt. when the input voltage near 0V (channel A), channel B voltage already rise to 2.874 V, then able reach 3.3 V.



TRIAC trigger method using Timer in MCU
The TRIAC triggering is done by using output compare pin on MCU with output compare mode in timer 2, MCU detect the zero crossing in sine wave, then control the timing for triggering.
calculation:
- prescaler=36
- internal clock = 72MHz
- after prescaler, timer clock=2MHz=0.5us
- 50Hz=10ms high + 10ms low
- 20000 * 0.25us = 10ms
- 40000 ticks for whole sine wave

MCU using 1 output compare from timer 2 and interrupt to trigger the TRIAC. Function that being create:
- void update_CCR3(uint32_t CCR1);
it's function just to update the CCR1 register to implement the next toggle. There are 4 CCR register (CCR1 to CCR4), 4 output compare, depends on which output compare you use. (Note that the actual CCR1 register is being used here because the output compare 3 is change to output compare 1 due to pin location is not suitable.)
- void TriacTriggerCallback(CCRxData *ccr3Data);
it's function that is called at CNT==0 (because CCR3 is 0 in the beginning) and CNT==CCR3. this is the interrupt that toggle the output compare pin, each sine wave will toggle the output compare pin for 4 times. the state machine (4 state due to 4 toggles) is inside this interrupt and it will keep track the state everytime it been called. take counter value from global variables at one particular state in state machine.
- void config_pulse(CCRxData *ccr3Data, uint32_t firingAngle, uint32_t width);
used for config firing angle and pulse width. it will calculate and set the ticks cycles for toggle timing.
- HAL_TIM_OC_Start_IT(&htim2,TIM_CHANNEL_1);
enable interrupt and output compare


TRIAC triggering circuit

positive trigger has been trigger (first and fourth quadrant 50-100 mA) on the gate, there is visible delay display in oscilloscope when using positive trigger on the gate, the negative trigger found no delay from oscilloscope.

config_pulse(&ccr3Data,i,40);
if(resetState==false)
i++;
else
i--;
if(i>=150)
resetState=true;
else if(i<=0)
resetState=false;
HAL_Delay(500);
<h6>Figure 12 : there is testing for different firing angle for TRIAC circuit using this code.
a test from 0 to 150 degree firing angle and 150 to 0 degree firing angle, change (minus or add) the firing angle every 500ms using HAL_Delay. in the test, the (pulse width + firing angle won't exceed 165) because it may accidentally firing the next 180 degree sine wave, it only detect the rising edge from zero crossing circuit. testing video : https://youtu.be/LwTu69IMxX0
UART communication between MUC and PC
MCU send data to PC using HAL function:
- HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
MCU receive data from PC using interrupt, this function will be called by interrupt:
-void interruptRxTx(UART_HandleTypeDef *huart, UartInfo *uart);
the function will store each character into a buffer, other function only able to read this buffer if this interrupt function receive a "enter" from user. it also send back each character to PC when user is typing to the console for UART, so that the console feedback to user whether user got key in characters correctly.
_%26_tdd.png)
read temperature from sensor
t
