Skip to content

Commit cec0f52

Browse files
committed
0.1.0 INA226
1 parent e766fd7 commit cec0f52

File tree

17 files changed

+1102
-0
lines changed

17 files changed

+1102
-0
lines changed

libraries/INA226/.arduino-ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
compile:
2+
# Choosing to run compilation tests on 2 different Arduino platforms
3+
platforms:
4+
- uno
5+
- leonardo
6+
- due
7+
- zero
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
name: Arduino-lint
3+
4+
on: [push, pull_request]
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: arduino/arduino-lint-action@v1
11+
with:
12+
library-manager: update
13+
compliance: strict
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: Arduino CI
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
arduino_ci:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: Arduino-CI/action@master
13+
# Arduino-CI/[email protected]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: json-syntax-check
15+
uses: limitusus/json-syntax-check@v1
16+
with:
17+
pattern: "\\.json$"
18+

libraries/INA226/INA226.cpp

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
// FILE: INA266.h
2+
// AUTHOR: Rob Tillaart
3+
// VERSION: 0.1.0
4+
// DATE: 2021-05-18
5+
// PURPOSE: Arduino library for INA266 power sensor
6+
// URL: https://github.com/RobTillaart/INA226
7+
//
8+
// HISTORY:
9+
// 0.1.0 2021-05-18 initial version
10+
11+
12+
13+
#include "INA226.h"
14+
15+
#define INA226_CONFIGURATION 0x00
16+
#define INA226_SHUNT_VOLTAGE 0x01
17+
#define INA226_BUS_VOLTAGE 0x02
18+
#define INA226_POWER 0x03
19+
#define INA226_CURRENT 0x04
20+
#define INA226_CALIBRATION 0x05
21+
#define INA226_MASK_ENABLE 0x06
22+
#define INA226_ALERT_LIMIT 0x07
23+
#define INA226_MANUFACTURER 0xFE
24+
#define INA226_DIE_ID 0xFF
25+
26+
27+
////////////////////////////////////////////////////////
28+
//
29+
// Constructor
30+
//
31+
INA226::INA226(const int8_t address, TwoWire *wire)
32+
{
33+
_address = address;
34+
_wire = wire;
35+
_current_LSB = 0;
36+
}
37+
38+
39+
#if defined (ESP8266) || defined(ESP32)
40+
bool INA226::begin(const uint8_t sda, const uint8_t scl)
41+
{
42+
_wire = &Wire;
43+
_wire->begin(sda, scl);
44+
if (! isConnected()) return false;
45+
return true;
46+
}
47+
#endif
48+
49+
50+
bool INA226::begin()
51+
{
52+
_wire->begin();
53+
if (! isConnected()) return false;
54+
return true;
55+
}
56+
57+
58+
bool INA226::isConnected()
59+
{
60+
_wire->beginTransmission(_address);
61+
return ( _wire->endTransmission() == 0);
62+
}
63+
64+
65+
////////////////////////////////////////////////////////
66+
//
67+
// Core functions
68+
//
69+
float INA226::getShuntVoltage()
70+
{
71+
uint16_t val = _readRegister(INA226_SHUNT_VOLTAGE);
72+
if (val & 0x8000)
73+
{
74+
val = val & 0x7FFF;
75+
val = val ^ 0x7FFF;
76+
val++;
77+
return val * -2.5e-6;
78+
}
79+
return val * 2.5e-6; // fixed 2.50 uV
80+
}
81+
82+
83+
float INA226::getBusVoltage()
84+
{
85+
uint16_t val = _readRegister(INA226_BUS_VOLTAGE);
86+
return val * 1.25e-3; // fixed 1.25 mV
87+
}
88+
89+
90+
float INA226::getPower()
91+
{
92+
uint16_t val = _readRegister(INA226_POWER);
93+
return val * 25 * _current_LSB;
94+
}
95+
96+
97+
float INA226::getCurrent()
98+
{
99+
uint16_t val = _readRegister(INA226_CURRENT);
100+
return val * _current_LSB;
101+
}
102+
103+
104+
////////////////////////////////////////////////////////
105+
//
106+
// Configuration
107+
//
108+
void INA226::reset()
109+
{
110+
uint16_t mask = _readRegister(INA226_CONFIGURATION);
111+
mask |= 0x800;
112+
_writeRegister(INA226_CONFIGURATION, mask);
113+
}
114+
115+
116+
void INA226::setAverage(uint8_t avg)
117+
{
118+
if (avg > 7) return;
119+
uint16_t mask = _readRegister(INA226_CONFIGURATION);
120+
mask &= 0xF1FF;
121+
mask |= (avg << 9);
122+
_writeRegister(INA226_CONFIGURATION, mask);
123+
}
124+
125+
126+
uint8_t INA226::getAverage()
127+
{
128+
uint16_t mask = _readRegister(INA226_CONFIGURATION);
129+
mask >>= 9;
130+
mask &= 7;
131+
return mask;
132+
}
133+
134+
135+
void INA226::setBusVoltageConversionTime(uint8_t bvct)
136+
{
137+
if (bvct > 7) return;
138+
uint16_t mask = _readRegister(INA226_CONFIGURATION);
139+
mask &= 0xFE3F;
140+
mask |= (bvct << 6);
141+
_writeRegister(INA226_CONFIGURATION, mask);
142+
}
143+
144+
145+
uint8_t INA226::getBusVoltageConversionTime()
146+
{
147+
uint16_t mask = _readRegister(INA226_CONFIGURATION);
148+
mask >>= 6;
149+
mask &= 7;
150+
return mask;
151+
}
152+
153+
154+
void INA226::setShuntVoltageConversionTime(uint8_t svct)
155+
{
156+
if (svct > 7) return;
157+
uint16_t mask = _readRegister(INA226_CONFIGURATION);
158+
mask &= 0xFFC7;
159+
mask |= (svct << 3);
160+
_writeRegister(INA226_CONFIGURATION, mask);
161+
}
162+
163+
164+
uint8_t INA226::getShuntVoltageConversionTime()
165+
{
166+
uint16_t mask = _readRegister(INA226_CONFIGURATION);
167+
mask >>= 3;
168+
mask &= 7;
169+
return mask;
170+
}
171+
172+
173+
////////////////////////////////////////////////////////
174+
//
175+
// Calibration
176+
//
177+
void INA226::setMaxCurrentShunt(float ampere, float ohm)
178+
{
179+
_current_LSB = ampere * (1.0 / 32768.0);
180+
// make the LSB a round number
181+
float factor = 1;
182+
// Serial.println(_current_LSB, 6);
183+
while (_current_LSB < 1)
184+
{
185+
_current_LSB *= 10;
186+
factor *= 10;
187+
}
188+
_current_LSB = 10.0 / factor;
189+
// Serial.println(_current_LSB, 6);
190+
uint16_t calib = round(0.00512 / (_current_LSB * ohm));
191+
// Serial.println(calib);
192+
_writeRegister(INA226_CALIBRATION, calib);
193+
}
194+
195+
196+
////////////////////////////////////////////////////////
197+
//
198+
// operating mode
199+
//
200+
void INA226::setMode(uint8_t mode)
201+
{
202+
if (mode > 7) return;
203+
uint16_t m = _readRegister(INA226_CONFIGURATION);
204+
m &= 0xFFF8;
205+
m |= mode;
206+
_writeRegister(INA226_CONFIGURATION, m);
207+
}
208+
209+
210+
uint8_t INA226::getMode()
211+
{
212+
return _readRegister(INA226_CONFIGURATION) & 0x0007;
213+
}
214+
215+
216+
////////////////////////////////////////////////////////
217+
//
218+
// alert
219+
//
220+
void INA226::setAlertRegister(uint16_t mask)
221+
{
222+
_writeRegister(INA226_MASK_ENABLE, (mask & 0xFC00));
223+
}
224+
225+
226+
uint16_t INA226::getAlertFlag()
227+
{
228+
return _readRegister(INA226_MASK_ENABLE) & 0x001F;
229+
}
230+
231+
232+
void INA226::setAlertLimit(uint16_t limit)
233+
{
234+
_writeRegister(INA226_ALERT_LIMIT, limit);
235+
}
236+
237+
238+
uint16_t INA226::getAlertLimit()
239+
{
240+
return _readRegister(INA226_ALERT_LIMIT);
241+
}
242+
243+
244+
////////////////////////////////////////////////////////
245+
//
246+
// meta information
247+
//
248+
uint16_t INA226::getManufacturerID()
249+
{
250+
return _readRegister(INA226_MANUFACTURER);
251+
}
252+
253+
uint16_t INA226::getDieID()
254+
{
255+
return _readRegister(INA226_DIE_ID);
256+
}
257+
258+
259+
////////////////////////////////////////////////////////
260+
//
261+
// PRIVATE
262+
//
263+
uint16_t INA226::_readRegister(uint8_t reg)
264+
{
265+
_wire->beginTransmission(_address);
266+
_wire->write(reg);
267+
_wire->endTransmission();
268+
269+
_wire->requestFrom(_address, (uint8_t)2);
270+
uint16_t value = _wire->read();
271+
value <<= 8;
272+
value |= _wire->read();
273+
return value;
274+
}
275+
276+
277+
uint16_t INA226::_writeRegister(uint8_t reg, uint16_t value)
278+
{
279+
_wire->beginTransmission(_address);
280+
_wire->write(reg);
281+
_wire->write(value >> 8);
282+
_wire->write(value & 0xFF);
283+
return _wire->endTransmission();
284+
}
285+
286+
287+
// -- END OF FILE --
288+

0 commit comments

Comments
 (0)