-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
PWM5.ino
183 lines (146 loc) · 6.03 KB
/
PWM5.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
/* This PWM solar charge controller is based on:
Julian Itett's PWM5: https://www.youtube.com/watch?v=AobcNhLG_Xw&list=PLjzGSu1yGFjWa5BEHgX5UUrSexppett3W
arduined.eu's PWM solar charge controller: https://www.arduined.eu/arduino-solar-charge-controller/
Changes and improvements, implemented by TheDIYGuy999:
- Using the 3.3V, 8MHz Arduino Pro Mini, which requires about 5mA (instead of 20mA for the 5V, 16MHz version)
- IRLZ44N logic level MOSFET used, because the charge pump can't deliver enough voltage with 3.3V supply
- Structurized the software, added serial output, improved the LED to be similar with Julians design
- Readings and calculations are based on vcc, which is read automatically
- Much simpler differential control algorithm
- Schematic better structurized, easier to understand
- Minor component value changes
- Eagle schematic and board file available
- Gerber files available (allows you to order your own boards easily)
*/
const float codeVersion = 1.0; // Software revision
//
// =======================================================================================================
// BUILD OPTIONS (comment out unneeded options)
// =======================================================================================================
//
#define DEBUG // if not commented out, Serial.print() is active! For debugging only!!
//
// =======================================================================================================
// INCLUDE LIRBARIES
// =======================================================================================================
//
// Tabs (header files in sketch directory)
#include "readVCC.h"
// Libraries
#include <statusLED.h> // TheDIYGuy999 library: https://github.com/TheDIYGuy999/statusLED
//
// =======================================================================================================
// PIN ASSIGNMENTS & GLOBAL VARIABLES
// =======================================================================================================
//
// Pins
#define PWM 9
#define PUMP1 3
#define PUMP2 11
#define VSENSE_OUT A1
// Status LED objects
statusLED LED(false); // false = not inversed (LED wired between GND and output)
float outputVoltage;
float vcc = 3.3; // Init value only. Will be read automatically later on. Base for all our voltage readings!
float pwm; // float required for finer granularity during calculation in differential equations!
// Configuration variables (you may have to change them)
float targetOutputVoltage = 13.8; // Our maximum battery charge voltage
float voltageCalibration = 94.7; // (100k + 10k) / 10k = 11 -> 1024 / 11 = 93.09 in theory
//
// =======================================================================================================
// MAIN ARDUINO SETUP (1x during startup)
// =======================================================================================================
//
void setup() {
// Serial port setup
#ifdef DEBUG
Serial.begin(19200);
#endif
// Output setup
pinMode(PWM, OUTPUT);
pinMode(PUMP1, OUTPUT);
pinMode(PUMP2, OUTPUT);
// PWM setup
TCCR2A = TCCR2A | B00110000; // Inverting mode for charge pump
TCCR2B = TCCR2B & B11111000 | B001; // Charge pump pins 15kHz
TCCR1B = TCCR1B & B11111000 | B010; // PWM 1.95kHz (the maximum of this MOSFET driver circuit!)
analogWrite(PUMP2, 117); // 117
analogWrite(PUMP1, 137); // 137
LED.begin(13); // Onboard LED on pin 13
}
//
// =======================================================================================================
// LED
// =======================================================================================================
//
void led() {
if (pwm == 255) {
// Indicate battery voltage: 13 flashes = 13V etc.
LED.flash(20, 380, 700, outputVoltage); // ON, OFF, PAUSE, PULSES
}
// Indicate pwm
else LED.flash((pwm / 2), 128 - (pwm / 2), 0, 0); // ON, OFF, PAUSE, PULSES
}
//
// =======================================================================================================
// CHECK VCC VOLTAGE
// =======================================================================================================
//
void checkVcc(boolean immediately) {
static unsigned long lastVcc;
if (millis() - lastVcc >= 200 || immediately) { // Every 200ms or immediately
lastVcc = millis();
vcc = readVcc() / 1000.0;
}
}
//
// =======================================================================================================
// READ SENSORS
// =======================================================================================================
//
void readSensors() {
outputVoltage = analogRead(VSENSE_OUT) * vcc / voltageCalibration;
}
//
// =======================================================================================================
// CONTROL LOOP
// =======================================================================================================
//
void controlLoop() {
pwm += targetOutputVoltage - outputVoltage; // simple p (differential) controller
pwm = constrain(pwm, 0, 255 );
analogWrite(PWM, pwm);
}
//
// =======================================================================================================
// SERIAL PRINT
// =======================================================================================================
//
void serialPrint() {
#ifdef DEBUG
static unsigned long lastPrint;
if (millis() - lastPrint >= 1000) { // Every 1000ms
lastPrint = millis();
Serial.print("Setpoint: ");
Serial.print(targetOutputVoltage);
Serial.print(" Battery: ");
Serial.print(outputVoltage);
Serial.print(" PWM: ");
Serial.print(pwm, 0);
Serial.print(" vcc: ");
Serial.println(vcc);
}
#endif
}
//
// =======================================================================================================
// MAIN LOOP
// =======================================================================================================
//
void loop() {
checkVcc(false); // with delay
readSensors();
controlLoop();
led();
serialPrint();
}