-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbq34z100_status_arduino.ino
234 lines (163 loc) · 4.83 KB
/
bq34z100_status_arduino.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
// Read Values from BQ34Z100 Gas Gauge Chip
// by Samuel J. Vanderwaal
// This code creates functions to read different status values from the BQ34Z100 gas gauge chip, it interprets that data and stores it into variables.
#include <Wire.h> // Wire library for communicating over I2C
#define BQ34Z100 0x55 // This is the I2C address of the BQ34Z100
unsigned int soc, voltage, remain_cap;
int avg_current, batt_temp, cntrl_stat1, cntrl_stat2;
float power_draw;
byte instCurrentLSB[] = {0x00, 0x18};
byte instCurrentMSB[] = {0x01, 0x00};
unsigned int inst_current_lsb, inst_current_msb;
int inst_current;
// ~~BQ34Z100 Status Functions~~
// State of Charge, a percent value of the battery's total charge
void readSOC()
{
Wire.beginTransmission(BQ34Z100);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
unsigned int low = Wire.read();
Wire.beginTransmission(BQ34Z100);
Wire.write(0x03);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
unsigned int high = Wire.read();
unsigned int high1 = high<<8;
soc = high1 + low;
}
// The battery's remaining capacity in mAh
void readRemainingCapacity()
{
Wire.beginTransmission(BQ34Z100);
Wire.write(0x04);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
unsigned int low = Wire.read();
Wire.beginTransmission(BQ34Z100);
Wire.write(0x05);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
unsigned int high = Wire.read();
unsigned int high1 = high<<8;
remain_cap = high1 + low;
}
void readVoltage()
{
Wire.beginTransmission(BQ34Z100);
Wire.write(0x08);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
unsigned int low = Wire.read();
Wire.beginTransmission(BQ34Z100);
Wire.write(0x09);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
unsigned int high = Wire.read();
unsigned int high1 = high<<8;
voltage = high1 + low;
}
void readAverageCurrent()
{
Wire.beginTransmission(BQ34Z100);
Wire.write(0x0a);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
unsigned int low = Wire.read();
Wire.beginTransmission(BQ34Z100);
Wire.write(0x0b);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
unsigned int high = Wire.read();
unsigned int high1 = high<<8;
avg_current = high1 + low;
}
void readInstCurrent()
{
// Write Control command bytes to gas gauge chip: 0x00/0x01
Wire.beginTransmission(BQ34Z100);
Wire.write(0x00);
Wire.endTransmission();
Wire.beginTransmission(BQ34Z100);
Wire.write(0x18);
Wire.endTransmission();
Wire.beginTransmission(BQ34Z100);
Wire.write(0x01);
Wire.endTransmission();
Wire.beginTransmission(BQ34Z100);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
inst_current_lsb = Wire.read();
Wire.requestFrom(BQ34Z100,1);
inst_current_msb = Wire.read();
unsigned int temp = inst_current_msb << 8;
inst_current = temp + inst_current_lsb;
}
void readBattTemp()
{
Wire.beginTransmission(BQ34Z100);
Wire.write(0x0c);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
unsigned int low = Wire.read();
Wire.beginTransmission(BQ34Z100);
Wire.write(0x0d);
Wire.endTransmission();
Wire.requestFrom(BQ34Z100,1);
unsigned int high = Wire.read();
unsigned int high1 = high<<8;
batt_temp = high1 + low;
batt_temp = 0.1*batt_temp; // Each bit is 0.1K, so we have a value in Kelvins
batt_temp = batt_temp - 273.15; // Convert to degrees Celsius
}
float PowerDraw(float volt, float current)
{
volt = volt/1000.0;
current = current/1000.0;
float power = volt*current;
return power;
}
void setup()
{
Serial.begin(9600);
Serial3.begin(57600);
Wire.begin();
}
void loop()
{
readSOC();
Serial.print("Battery Charge: ");
Serial.print(soc);
Serial.println("%");
readRemainingCapacity();
Serial.print("Remaining Capacity: ");
Serial.print(remain_cap);
Serial.println(" mAh");
readVoltage();
Serial.print("Battery Pack Voltage: ");
Serial.print(voltage);
Serial.println(" mV");
readAverageCurrent();
Serial.print("Average Current Draw: ");
Serial.print(avg_current);
Serial.println(" mA");
readInstCurrent();
Serial.print("Instantaneous Current Draw: ");
Serial.println(inst_current);
Serial.print("Instantaneous Current Draw LSB: ");
Serial.println(inst_current_lsb);
Serial.print("Instantantous Current MSB: ");
Serial.println(inst_current_msb);
readBattTemp();
Serial.print("Battery Temperature: ");
Serial.print(batt_temp);
Serial.println(" C");
power_draw = PowerDraw(voltage,avg_current);
Serial.print("Power Draw: ");
Serial.print(power_draw);
Serial.println(" W");
Serial.print("\r\n");
delay(5000);
}