-
Notifications
You must be signed in to change notification settings - Fork 90
/
reflowOvenController.ino
627 lines (587 loc) · 17.8 KB
/
reflowOvenController.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*******************************************************************************
* Title: Reflow Oven Controller
* Version: 1.20
* Date: 26-11-2012
* Company: Rocket Scream Electronics
* Author: Lim Phang Moh
* Website: www.rocketscream.com
*
* Brief
* =====
* This is an example firmware for our Arduino compatible reflow oven controller.
* The reflow curve used in this firmware is meant for lead-free profile
* (it's even easier for leaded process!). You'll need to use the MAX31855
* library for Arduino if you are having a shield of v1.60 & above which can be
* downloaded from our GitHub repository. Please check our wiki
* (www.rocketscream.com/wiki) for more information on using this piece of code
* together with the reflow oven controller shield.
*
* Temperature (Degree Celcius) Magic Happens Here!
* 245-| x x
* | x x
* | x x
* | x x
* 200-| x x
* | x | | x
* | x | | x
* | x | |
* 150-| x | |
* | x | | |
* | x | | |
* | x | | |
* | x | | |
* | x | | |
* | x | | |
* 30 -| x | | |
* |< 60 - 90 s >|< 90 - 120 s >|< 90 - 120 s >|
* | Preheat Stage | Soaking Stage | Reflow Stage | Cool
* 0 |_ _ _ _ _ _ _ _|_ _ _ _ _ _ _ _ _ _|_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
* Time (Seconds)
*
* This firmware owed very much on the works of other talented individuals as
* follows:
* ==========================================
* Brett Beauregard (www.brettbeauregard.com)
* ==========================================
* Author of Arduino PID library. On top of providing industry standard PID
* implementation, he gave a lot of help in making this reflow oven controller
* possible using his awesome library.
*
* ==========================================
* Limor Fried of Adafruit (www.adafruit.com)
* ==========================================
* Author of Arduino MAX6675 library. Adafruit has been the source of tonnes of
* tutorials, examples, and libraries for everyone to learn.
*
* Disclaimer
* ==========
* Dealing with high voltage is a very dangerous act! Please make sure you know
* what you are dealing with and have proper knowledge before hand. Your use of
* any information or materials on this reflow oven controller is entirely at
* your own risk, for which we shall not be liable.
*
* Licences
* ========
* This reflow oven controller hardware and firmware are released under the
* Creative Commons Share Alike v3.0 license
* http://creativecommons.org/licenses/by-sa/3.0/
* You are free to take this piece of code, use it and modify it.
* All we ask is attribution including the supporting libraries used in this
* firmware.
*
* Required Libraries
* ==================
* - Arduino PID Library:
* >> https://github.com/br3ttb/Arduino-PID-Library
* - MAX31855 Library (for board v1.60 & above):
* >> https://github.com/rocketscream/MAX31855
* - MAX6675 Library (for board v1.50 & below):
* >> https://github.com/adafruit/MAX6675-library
*
* Revision Description
* ======== ===========
* 1.20 Adds supports for v1.60 (and above) of Reflow Oven Controller
* Shield:
* - Uses MAX31855KASA+ chip and pin reassign (allowing A4 & A5 (I2C)
* to be used for user application).
* - Uses analog based switch (allowing D2 & D3 to be used for user
* application).
* Adds waiting state when temperature too hot to start reflow process.
* Corrected thermocouple disconnect error interpretation (MAX6675).
* 1.10 Arduino IDE 1.0 compatible.
* 1.00 Initial public release.
*******************************************************************************/
// Comment either one the following #define to select your board revision
// Newer board version starts from v1.60 using MAX31855KASA+ chip
#define USE_MAX31855
// Older board version below version v1.60 using MAX6675ISA+ chip
//#define USE_MAX6675
// ***** INCLUDES *****
#include <LiquidCrystal.h>
#ifdef USE_MAX31855
#include <MAX31855.h>
#else
#include <max6675.h>
#endif
#include <PID_v1.h>
// ***** TYPE DEFINITIONS *****
typedef enum REFLOW_STATE
{
REFLOW_STATE_IDLE,
REFLOW_STATE_PREHEAT,
REFLOW_STATE_SOAK,
REFLOW_STATE_REFLOW,
REFLOW_STATE_COOL,
REFLOW_STATE_COMPLETE,
REFLOW_STATE_TOO_HOT,
REFLOW_STATE_ERROR
} reflowState_t;
typedef enum REFLOW_STATUS
{
REFLOW_STATUS_OFF,
REFLOW_STATUS_ON
} reflowStatus_t;
typedef enum SWITCH
{
SWITCH_NONE,
SWITCH_1,
SWITCH_2
} switch_t;
typedef enum DEBOUNCE_STATE
{
DEBOUNCE_STATE_IDLE,
DEBOUNCE_STATE_CHECK,
DEBOUNCE_STATE_RELEASE
} debounceState_t;
// ***** CONSTANTS *****
#define TEMPERATURE_ROOM 50
#define TEMPERATURE_SOAK_MIN 150
#define TEMPERATURE_SOAK_MAX 200
#define TEMPERATURE_REFLOW_MAX 250
#define TEMPERATURE_COOL_MIN 100
#define SENSOR_SAMPLING_TIME 1000
#define SOAK_TEMPERATURE_STEP 5
#define SOAK_MICRO_PERIOD 9000
#define DEBOUNCE_PERIOD_MIN 50
// ***** PID PARAMETERS *****
// ***** PRE-HEAT STAGE *****
#define PID_KP_PREHEAT 100
#define PID_KI_PREHEAT 0.025
#define PID_KD_PREHEAT 20
// ***** SOAKING STAGE *****
#define PID_KP_SOAK 300
#define PID_KI_SOAK 0.05
#define PID_KD_SOAK 250
// ***** REFLOW STAGE *****
#define PID_KP_REFLOW 300
#define PID_KI_REFLOW 0.05
#define PID_KD_REFLOW 350
#define PID_SAMPLE_TIME 1000
// ***** LCD MESSAGES *****
const char* lcdMessagesReflowStatus[] = {
"Ready",
"Pre-heat",
"Soak",
"Reflow",
"Cool",
"Complete",
"Wait,hot",
"Error"
};
// ***** DEGREE SYMBOL FOR LCD *****
unsigned char degree[8] = {
140,146,146,140,128,128,128,128};
// ***** PIN ASSIGNMENT *****
#ifdef USE_MAX31855
int ssrPin = 5;
int thermocoupleSOPin = A3;
int thermocoupleCSPin = A2;
int thermocoupleCLKPin = A1;
int lcdRsPin = 7;
int lcdEPin = 8;
int lcdD4Pin = 9;
int lcdD5Pin = 10;
int lcdD6Pin = 11;
int lcdD7Pin = 12;
int ledRedPin = 4;
int buzzerPin = 6;
int switchPin = A0;
#else
int ssrPin = 5;
int thermocoupleSOPin = A5;
int thermocoupleCSPin = A4;
int thermocoupleCLKPin = A3;
int lcdRsPin = 7;
int lcdEPin = 8;
int lcdD4Pin = 9;
int lcdD5Pin = 10;
int lcdD6Pin = 11;
int lcdD7Pin = 12;
int ledRedPin = A1;
int ledGreenPin = A0;
int buzzerPin = 6;
int switch1Pin = 2;
int switch2Pin = 3;
#endif
// ***** PID CONTROL VARIABLES *****
double setpoint;
double input;
double output;
double kp = PID_KP_PREHEAT;
double ki = PID_KI_PREHEAT;
double kd = PID_KD_PREHEAT;
int windowSize;
unsigned long windowStartTime;
unsigned long nextCheck;
unsigned long nextRead;
unsigned long timerSoak;
unsigned long buzzerPeriod;
// Reflow oven controller state machine state variable
reflowState_t reflowState;
// Reflow oven controller status
reflowStatus_t reflowStatus;
// Switch debounce state machine state variable
debounceState_t debounceState;
// Switch debounce timer
long lastDebounceTime;
// Switch press status
switch_t switchStatus;
// Seconds timer
int timerSeconds;
// Specify PID control interface
PID reflowOvenPID(&input, &output, &setpoint, kp, ki, kd, DIRECT);
// Specify LCD interface
LiquidCrystal lcd(lcdRsPin, lcdEPin, lcdD4Pin, lcdD5Pin, lcdD6Pin, lcdD7Pin);
// Specify MAX6675 thermocouple interface
#ifdef USE_MAX31855
MAX31855 thermocouple(thermocoupleSOPin, thermocoupleCSPin,
thermocoupleCLKPin);
#else
MAX6675 thermocouple(thermocoupleCLKPin, thermocoupleCSPin,
thermocoupleSOPin);
#endif
void setup()
{
// SSR pin initialization to ensure reflow oven is off
digitalWrite(ssrPin, LOW);
pinMode(ssrPin, OUTPUT);
// Buzzer pin initialization to ensure annoying buzzer is off
digitalWrite(buzzerPin, LOW);
pinMode(buzzerPin, OUTPUT);
// LED pins initialization and turn on upon start-up (active low)
digitalWrite(ledRedPin, LOW);
pinMode(ledRedPin, OUTPUT);
#ifdef USE_MAX6675
// LED pins initialization and turn on upon start-up (active low)
digitalWrite(ledGreenPin, LOW);
pinMode(ledGreenPin, OUTPUT);
// Switch pins initialization
pinMode(switch1Pin, INPUT);
pinMode(switch2Pin, INPUT);
#endif
// Start-up splash
digitalWrite(buzzerPin, HIGH);
lcd.begin(8, 2);
lcd.createChar(0, degree);
lcd.clear();
lcd.print("Reflow");
lcd.setCursor(0, 1);
lcd.print("Oven 1.2");
digitalWrite(buzzerPin, LOW);
delay(2500);
lcd.clear();
// Serial communication at 57600 bps
Serial.begin(57600);
// Turn off LED (active low)
digitalWrite(ledRedPin, HIGH);
#ifdef USE_MAX6675
digitalWrite(ledGreenPin, HIGH);
#endif
// Set window size
windowSize = 2000;
// Initialize time keeping variable
nextCheck = millis();
// Initialize thermocouple reading variable
nextRead = millis();
}
void loop()
{
// Current time
unsigned long now;
// Time to read thermocouple?
if (millis() > nextRead)
{
// Read thermocouple next sampling period
nextRead += SENSOR_SAMPLING_TIME;
// Read current temperature
#ifdef USE_MAX31855
input = thermocouple.readThermocouple(CELSIUS);
#else
input = thermocouple.readCelsius();
#endif
// If thermocouple problem detected
#ifdef USE_MAX6675
if (isnan(input))
#else
if((input == FAULT_OPEN) || (input == FAULT_SHORT_GND) ||
(input == FAULT_SHORT_VCC))
#endif
{
// Illegal operation
reflowState = REFLOW_STATE_ERROR;
reflowStatus = REFLOW_STATUS_OFF;
}
}
if (millis() > nextCheck)
{
// Check input in the next seconds
nextCheck += 1000;
// If reflow process is on going
if (reflowStatus == REFLOW_STATUS_ON)
{
// Toggle red LED as system heart beat
digitalWrite(ledRedPin, !(digitalRead(ledRedPin)));
// Increase seconds timer for reflow curve analysis
timerSeconds++;
// Send temperature and time stamp to serial
Serial.print(timerSeconds);
Serial.print(" ");
Serial.print(setpoint);
Serial.print(" ");
Serial.print(input);
Serial.print(" ");
Serial.println(output);
}
else
{
// Turn off red LED
digitalWrite(ledRedPin, HIGH);
}
// Clear LCD
lcd.clear();
// Print current system state
lcd.print(lcdMessagesReflowStatus[reflowState]);
// Move the cursor to the 2 line
lcd.setCursor(0, 1);
// If currently in error state
if (reflowState == REFLOW_STATE_ERROR)
{
// No thermocouple wire connected
lcd.print("TC Error!");
}
else
{
// Print current temperature
lcd.print(input);
#if ARDUINO >= 100
// Print degree Celsius symbol
lcd.write((uint8_t)0);
#else
// Print degree Celsius symbol
lcd.print(0, BYTE);
#endif
lcd.print("C ");
}
}
// Reflow oven controller state machine
switch (reflowState)
{
case REFLOW_STATE_IDLE:
// If oven temperature is still above room temperature
if (input >= TEMPERATURE_ROOM)
{
reflowState = REFLOW_STATE_TOO_HOT;
}
else
{
// If switch is pressed to start reflow process
if (switchStatus == SWITCH_1)
{
// Send header for CSV file
Serial.println("Time Setpoint Input Output");
// Intialize seconds timer for serial debug information
timerSeconds = 0;
// Initialize PID control window starting time
windowStartTime = millis();
// Ramp up to minimum soaking temperature
setpoint = TEMPERATURE_SOAK_MIN;
// Tell the PID to range between 0 and the full window size
reflowOvenPID.SetOutputLimits(0, windowSize);
reflowOvenPID.SetSampleTime(PID_SAMPLE_TIME);
// Turn the PID on
reflowOvenPID.SetMode(AUTOMATIC);
// Proceed to preheat stage
reflowState = REFLOW_STATE_PREHEAT;
}
}
break;
case REFLOW_STATE_PREHEAT:
reflowStatus = REFLOW_STATUS_ON;
// If minimum soak temperature is achieve
if (input >= TEMPERATURE_SOAK_MIN)
{
// Chop soaking period into smaller sub-period
timerSoak = millis() + SOAK_MICRO_PERIOD;
// Set less agressive PID parameters for soaking ramp
reflowOvenPID.SetTunings(PID_KP_SOAK, PID_KI_SOAK, PID_KD_SOAK);
// Ramp up to first section of soaking temperature
setpoint = TEMPERATURE_SOAK_MIN + SOAK_TEMPERATURE_STEP;
// Proceed to soaking state
reflowState = REFLOW_STATE_SOAK;
}
break;
case REFLOW_STATE_SOAK:
// If micro soak temperature is achieved
if (millis() > timerSoak)
{
timerSoak = millis() + SOAK_MICRO_PERIOD;
// Increment micro setpoint
setpoint += SOAK_TEMPERATURE_STEP;
if (setpoint > TEMPERATURE_SOAK_MAX)
{
// Set agressive PID parameters for reflow ramp
reflowOvenPID.SetTunings(PID_KP_REFLOW, PID_KI_REFLOW, PID_KD_REFLOW);
// Ramp up to first section of soaking temperature
setpoint = TEMPERATURE_REFLOW_MAX;
// Proceed to reflowing state
reflowState = REFLOW_STATE_REFLOW;
}
}
break;
case REFLOW_STATE_REFLOW:
// We need to avoid hovering at peak temperature for too long
// Crude method that works like a charm and safe for the components
if (input >= (TEMPERATURE_REFLOW_MAX - 5))
{
// Set PID parameters for cooling ramp
reflowOvenPID.SetTunings(PID_KP_REFLOW, PID_KI_REFLOW, PID_KD_REFLOW);
// Ramp down to minimum cooling temperature
setpoint = TEMPERATURE_COOL_MIN;
// Proceed to cooling state
reflowState = REFLOW_STATE_COOL;
}
break;
case REFLOW_STATE_COOL:
// If minimum cool temperature is achieve
if (input <= TEMPERATURE_COOL_MIN)
{
// Retrieve current time for buzzer usage
buzzerPeriod = millis() + 1000;
// Turn on buzzer and green LED to indicate completion
#ifdef USE_MAX6675
digitalWrite(ledGreenPin, LOW);
#endif
digitalWrite(buzzerPin, HIGH);
// Turn off reflow process
reflowStatus = REFLOW_STATUS_OFF;
// Proceed to reflow Completion state
reflowState = REFLOW_STATE_COMPLETE;
}
break;
case REFLOW_STATE_COMPLETE:
if (millis() > buzzerPeriod)
{
// Turn off buzzer and green LED
digitalWrite(buzzerPin, LOW);
#ifdef USE_MAX6675
digitalWrite(ledGreenPin, HIGH);
#endif
// Reflow process ended
reflowState = REFLOW_STATE_IDLE;
}
break;
case REFLOW_STATE_TOO_HOT:
// If oven temperature drops below room temperature
if (input < TEMPERATURE_ROOM)
{
// Ready to reflow
reflowState = REFLOW_STATE_IDLE;
}
break;
case REFLOW_STATE_ERROR:
// If thermocouple problem is still present
#ifdef USE_MAX6675
if (isnan(input))
#else
if((input == FAULT_OPEN) || (input == FAULT_SHORT_GND) ||
(input == FAULT_SHORT_VCC))
#endif
{
// Wait until thermocouple wire is connected
reflowState = REFLOW_STATE_ERROR;
}
else
{
// Clear to perform reflow process
reflowState = REFLOW_STATE_IDLE;
}
break;
}
// If switch 1 is pressed
if (switchStatus == SWITCH_1)
{
// If currently reflow process is on going
if (reflowStatus == REFLOW_STATUS_ON)
{
// Button press is for cancelling
// Turn off reflow process
reflowStatus = REFLOW_STATUS_OFF;
// Reinitialize state machine
reflowState = REFLOW_STATE_IDLE;
}
}
// Simple switch debounce state machine (for switch #1 (both analog & digital
// switch supported))
switch (debounceState)
{
case DEBOUNCE_STATE_IDLE:
// No valid switch press
switchStatus = SWITCH_NONE;
// If switch #1 is pressed
#ifdef USE_MAX6675
if (digitalRead(switch1Pin) == LOW)
#else
if (analogRead(switchPin) == 0)
#endif
{
// Intialize debounce counter
lastDebounceTime = millis();
// Proceed to check validity of button press
debounceState = DEBOUNCE_STATE_CHECK;
}
break;
case DEBOUNCE_STATE_CHECK:
#ifdef USE_MAX6675
// If switch #1 is still pressed
if (digitalRead(switch1Pin) == LOW)
#else
if (analogRead(switchPin) == 0)
#endif
{
// If minimum debounce period is completed
if ((millis() - lastDebounceTime) > DEBOUNCE_PERIOD_MIN)
{
// Proceed to wait for button release
debounceState = DEBOUNCE_STATE_RELEASE;
}
}
// False trigger
else
{
// Reinitialize button debounce state machine
debounceState = DEBOUNCE_STATE_IDLE;
}
break;
case DEBOUNCE_STATE_RELEASE:
#ifdef USE_MAX6675
if (digitalRead(switch1Pin) == HIGH)
#else
if (analogRead(switchPin) > 0)
#endif
{
// Valid switch 1 press
switchStatus = SWITCH_1;
// Reinitialize button debounce state machine
debounceState = DEBOUNCE_STATE_IDLE;
}
break;
}
// PID computation and SSR control
if (reflowStatus == REFLOW_STATUS_ON)
{
now = millis();
reflowOvenPID.Compute();
if((now - windowStartTime) > windowSize)
{
// Time to shift the Relay Window
windowStartTime += windowSize;
}
if(output > (now - windowStartTime)) digitalWrite(ssrPin, HIGH);
else digitalWrite(ssrPin, LOW);
}
// Reflow oven process is off, ensure oven is off
else
{
digitalWrite(ssrPin, LOW);
}
}