reflow.ino 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. 60W PD Powered Automatic Reflow Hoteplate
  3. Author: tobychui
  4. Notes:
  5. Temperature sensing is done using 1k 1% resistor and a 10k Thermistor
  6. You might need to change the code if you are using other values
  7. Touch Button Mappings
  8. Start Btn: P1.4
  9. Stop Btn: P1.7
  10. Download Settings
  11. CH552
  12. 16Mhz (internal, 3.3V or 5V)
  13. */
  14. #include <Serial.h>
  15. #include <TouchKey.h>
  16. #define TEMP_PIN 11 //P1.1
  17. #define HEATER_PIN 34 //P3.4
  18. #define LED_PREHEAT 15 //P1.5
  19. #define LED_REFLOW 16 //P1.6
  20. //Timing settings. Each cycle is approximately 100ms
  21. #define HOLDSTART_TIME 30 //START Button hold down time in cycles
  22. #define HOLDSTOP_TIME 10 //STOP Button hold down time in cycles
  23. #define SOAKING_TIME 600 //SOAKING temperature upkeep time in cycles
  24. /*
  25. * Temperature Settings
  26. *
  27. * The ADC value can be calculated using the following line equation
  28. * t=-0.7x+250
  29. *
  30. * where X is ADC reading and t is target temp in degree C
  31. *
  32. * Note that different setup (e.g. ref resistor value / NTC resistor value) might have different equations
  33. * You can experiment with yours and get a few data point to plot your own line
  34. */
  35. #define COOLDOWN_TEMP_ADC 255 //Room temperature
  36. //Preheat temp & power
  37. #define PREHEAT_START_ADC 143 //150 degree C
  38. #define PREHEAT_TEMP_ADC 122 //160 degree C
  39. #define PREHEAT_PWR_PWM 155 //60% power output
  40. //Reflow temp & power
  41. #define REFLOW_TEMP_ADC 58 //220 degree C
  42. #define REFLOW_PWR_PWM 255 //100% power output
  43. //Overheat cutoff temp
  44. #define CUTOFF_TEMP_ADC 53 //250 degree C
  45. //Func def
  46. bool updateHeaterPowerState();
  47. void updateKeyStates();
  48. int readTemp();
  49. //Runtimes
  50. bool reflowing = false; //Reflow has started
  51. bool reflowComplete = false; //Indicate if the stopping is done because of reflow complete
  52. /*
  53. Reflow Stages define what is the next state the hotplate should reach
  54. 0: Standby
  55. 1: Preheating
  56. 2: Preheat Holding / Soaking
  57. 3: Reflowing
  58. 4: Cooling down
  59. */
  60. int reflowStages = 0;
  61. int startCountdown = HOLDSTART_TIME; //Hold for HOLDSTART_TIME to start to prevent accidental touches
  62. int stopcountdown = HOLDSTOP_TIME; //Hold for HOLDSTOP_TIME to stop reflowing
  63. int cycleCounter = 0; //Record cycle time up to 10 seconds
  64. int soakingCountdown = SOAKING_TIME;
  65. //Blink state controls
  66. bool blinkState = false; //If blinking this will be switching between true and false
  67. bool blinkYellow = true; //Slow blink yellow if enabled (Standby). Do not enable both yellow and red at the same time
  68. bool blinkRed = false; //Slow blink red if enable (End of Reflow). Do not enable both yellow and red at the same time
  69. bool fastBlinkState = false;
  70. bool fastblinkRed = false;
  71. //Real-time temperature control targets
  72. int targetTempADC = 255; //Target ADC reading to reach, 255 = room temperature
  73. int targetPwrPWM = 0; //Target PWM cycle for heater, 255 = full power, 0 = off
  74. int offset = 0; //Allow +-offset for the ADC reading before reheating / cooling
  75. void setup() {
  76. //Enable the touch buttons
  77. //Enable all 6 channels:TIN2(P1.4),TIN5(P1.7)
  78. TouchKey_begin((1 << 2) | (1 << 5));
  79. //refer to AN3891 MPR121 Baseline System for more details
  80. TouchKey_SetMaxHalfDelta(164); // increase if sensor value are more noisy
  81. TouchKey_SetNoiseHalfDelta(8); // If baseline need to adjust at higher rate, increase this value
  82. TouchKey_SetNoiseCountLimit(10); // If baseline need to adjust faster, increase this value
  83. TouchKey_SetFilterDelayLimit(5); // make overall adjustment slower
  84. TouchKey_SetTouchThreshold(220); // Bigger touch pad can use a bigger value
  85. TouchKey_SetReleaseThreshold(150); // Smaller than touch threshold
  86. //Set LED pins to output
  87. pinMode(LED_PREHEAT, OUTPUT);
  88. pinMode(LED_REFLOW, OUTPUT);
  89. digitalWrite(LED_PREHEAT, LOW);
  90. digitalWrite(LED_REFLOW, LOW);
  91. //Set temp sense pin to input
  92. pinMode(TEMP_PIN, INPUT);
  93. //Set mosfet control pins to output and disable it
  94. pinMode(HEATER_PIN, OUTPUT);
  95. digitalWrite(HEATER_PIN, LOW);
  96. }
  97. void loop() {
  98. //Check key status
  99. updateKeyStates();
  100. //Main reflowing logics
  101. //Add delay to the end of each conditions so each loop cycle is approximate 100ms
  102. if (reflowing) {
  103. bool tempReached = updateHeaterPowerState(); //This action takes 100ms
  104. if (tempReached) {
  105. //This stage temperature reached. Move to next stage
  106. if (reflowStages == 1) {
  107. //Preheat stage completed. Enter soaking stage
  108. USBSerial_println("Preheat temperature reached. Soaking started");
  109. targetTempADC = PREHEAT_TEMP_ADC; //Set temperature to soaking end temp
  110. reflowStages = 2; //Set reflow stage to soaking
  111. soakingCountdown = SOAKING_TIME;
  112. fastblinkRed = true;
  113. } else if (reflowStages == 3) {
  114. //Reflowing target temperature reached. Start cooldown and shut down heater
  115. USBSerial_println("Reflow completed. Cooling down");
  116. reflowStages = 4;
  117. blinkYellow = false;
  118. blinkRed = true;
  119. targetTempADC = COOLDOWN_TEMP_ADC;
  120. targetPwrPWM = 0;
  121. //Reflow ended. Wait until stop being press to exit this state
  122. }
  123. }
  124. if (reflowStages == 2) {
  125. //Wait for the soaking to complete and enter reflow stage
  126. soakingCountdown--;
  127. if (soakingCountdown <= 0) {
  128. //Soaking completed. Enter reflow stage
  129. USBSerial_println("Soaking time ended. Reflow started");
  130. //Set Reflow LED to high
  131. fastblinkRed = false;
  132. digitalWrite(LED_REFLOW, HIGH);
  133. //Set to reflow temperature
  134. targetTempADC = REFLOW_TEMP_ADC; //Set the target temp to reflow
  135. targetPwrPWM = REFLOW_PWR_PWM; //Set power rating to reflow
  136. //Update the reflow stage to 3
  137. reflowStages = 3;
  138. } else if (soakingCountdown % 100 == 0) {
  139. USBSerial_print("Soaking cycles left: ");
  140. USBSerial_println(soakingCountdown);
  141. }
  142. }
  143. delay(1);
  144. } else {
  145. //Standby mode
  146. digitalWrite(HEATER_PIN, LOW);
  147. blinkYellow = true;
  148. delay(100);
  149. }
  150. //FastBlink handler, execute every 500ms
  151. if (cycleCounter % 5 == 0 && fastblinkRed) {
  152. digitalWrite(LED_PREHEAT, LOW);
  153. if (fastBlinkState) {
  154. digitalWrite(LED_REFLOW, HIGH);
  155. } else {
  156. digitalWrite(LED_REFLOW, LOW);
  157. }
  158. fastBlinkState = !fastBlinkState;
  159. }
  160. //Blink handler, execute every 2 seconds
  161. if (cycleCounter % 20 == 0) {
  162. if (blinkYellow) {
  163. if (blinkState) {
  164. digitalWrite(LED_PREHEAT, HIGH);
  165. } else {
  166. digitalWrite(LED_PREHEAT, LOW);
  167. }
  168. } else if (blinkRed) {
  169. if (blinkState) {
  170. digitalWrite(LED_REFLOW, HIGH);
  171. } else {
  172. digitalWrite(LED_REFLOW, LOW);
  173. }
  174. }
  175. blinkState = !blinkState;
  176. }
  177. //Cycle printout
  178. //USBSerial_println("cycle");
  179. cycleCounter++;
  180. if (cycleCounter > 600) {
  181. cycleCounter = 0;
  182. }
  183. }