SkillAgentSearch skills...

TemperatureControllerMCU

TRIAC Controller Heater with PID Control System

Install / Use

/learn @yongxianan/TemperatureControllerMCU
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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

cubeMX whole circuit diagram

<h6>Figure 1 : the whole setup circuit diagram and cubeMX pins configuration.

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.

zero crossing circuit

<h6>Figure 2 : zero crossing circuit

(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.

<h6>Figure 3 : zero crossing signal and input signal (sine wave) Figure 3 shown that it take 96.7 us for output signal (channel B) to reach 3.187 V when the input signal cross zero volt (channel A) in simulation program.

<h6>Figure 4 : zero crossing signal (opto-couple input signal) and input signal (sine wave). Figure 4 shown there is small distortion signal for opto-couple input signal.

<h6>Figure 5 : sine wave signal as input and the opto-couple output signal range from 0V-3.3V

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.

<h6>Figure 6 : output compare mode from manual

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

<h6>Figure 7 : timer block diagram. the zero-crossing circuit will trigger the TI2FP2 pin on MCU everytime AC signal crossing zero, TI2FP2 reset the timer 2 counter back to zero. the output compare pin will trigger TRIAC according to TI2FP2 pin and firing angle, it also update the next output compare trigger timing (next CCR tick cycle).

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

<h6>Figure 8 : test on output compare trigger (channel 1) with firing angle of 90 degree (at 10000 ticks cycles). the expected delay is 5 ms and the actual delay is 4.8 ms.

<h6>Figure 9 : the expected time between 2 trigger (channel 1) in a sine wave period is 10ms. tested result shown the time between 2 trigger is 10.4 ms. channel 2 is signal from zero crossing circuit.

TRIAC triggering circuit

<h6>Figure 10 : using negative G- to trigger, second and third quadrant used. (50-60 mA)

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. negative trigger triac circuit

<h6>Figure 11 : negative trigger TRIAC circuit with the lamp shown, the MCU is connect to U1 opto-coupler. this circuit being used in this project.
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.

<h6>Figure 13 : when user key in "start" and enter, it show the temperature data, it wait until user key in enter to stop it. User can set target temperature with "set temp". it will ignore other commands that are not recognize by the MCU. the time is get from HAL_GetTick function.

read temperature from sensor

t

View on GitHub
GitHub Stars18
CategoryDevelopment
Updated19h ago
Forks5

Languages

C

Security Score

75/100

Audited on Mar 30, 2026

No findings