RAK16000 WisBlock DC Current Module Quick Start Guide
The RAK16000 is a WisBlock IO module based on the Texas Instruments INA219BID. It measures DC current and voltage through an I2C interface, allowing the WisBlock Core to calculate power consumption.
Prerequisite
-
Prepare a WisBlock Base with a sensor slot, a compatible WisBlock Core, and the RAK16000 module. Refer to the RAK16000 Installation Guide.
-
Install the Arduino IDE and RAKwireless Arduino BSP to add the supported WisBlock Core boards to the Arduino Boards Manager.
-
USB cable
Initial Test of the RAK16000 WisBlock Module
-
This guide uses the RAK4631 WisBlock Core for the configuration example. You can also use the RAK11200 or RAK11310 WisBlock Core.
-
If you are using the RAK11200 as your WisBlock Core, configure the Boot0 pin correctly before uploading the code. Otherwise, the upload will fail. Refer to the RAK11200 Quick Start Guide for details.
-
Install the RAKwireless Arduino BSP using the
package_rakwireless_index.jsonboard package. The supported WisBlock Core boards will then be available in the Arduino IDE. -
You need to select first the WisBlock Core you have.
Figure 1: Selecting RAK4631 as WisBlock Core- Copy the following sample code into your Arduino IDE:
Click to view the example
/**
@file RAK16000_DcCurrent_INA219.ino
@author rakwireless.com
@brief Get DC current from sensor
@version 0.1
@date 2021-7-28
@copyright Copyright (c) 2020
**/
#include <Wire.h>
#include <INA219_WE.h>// Click here to get the library: http://librarymanager/All#INA219_WE
#define I2C_ADDRESS 0x41
INA219_WE ina219 = INA219_WE(I2C_ADDRESS);
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
delay(300);
// Initialize sensor
Wire.begin();
if(!ina219.init()){
Serial.println("INA219 not connected!");
}
/* Set ADC Mode for Bus and ShuntVoltage
* Mode * * Res / Samples * * Conversion Time *
BIT_MODE_9 9 Bit Resolution 84 µs
BIT_MODE_10 10 Bit Resolution 148 µs
BIT_MODE_11 11 Bit Resolution 276 µs
BIT_MODE_12 12 Bit Resolution 532 µs (DEFAULT)
SAMPLE_MODE_2 Mean Value 2 samples 1.06 ms
SAMPLE_MODE_4 Mean Value 4 samples 2.13 ms
SAMPLE_MODE_8 Mean Value 8 samples 4.26 ms
SAMPLE_MODE_16 Mean Value 16 samples 8.51 ms
SAMPLE_MODE_32 Mean Value 32 samples 17.02 ms
SAMPLE_MODE_64 Mean Value 64 samples 34.05 ms
SAMPLE_MODE_128 Mean Value 128 samples 68.10 ms
*/
ina219.setADCMode(SAMPLE_MODE_128); // choose mode and uncomment for change of default
/* Set measure mode
POWER_DOWN - INA219 switched off
TRIGGERED - measurement on demand
ADC_OFF - Analog/Digital Converter switched off
CONTINUOUS - Continuous measurements (DEFAULT)
*/
ina219.setMeasureMode(CONTINUOUS); // choose mode and uncomment for change of default
/* Set PGain
* Gain * * Shunt Voltage Range * * Max Current (if shunt is 0.1 ohms) *
PG_40 40 mV 0.4 A
PG_80 80 mV 0.8 A
PG_160 160 mV 1.6 A
PG_320 320 mV 3.2 A (DEFAULT)
*/
ina219.setPGain(PG_320); // choose gain and uncomment for change of default
/* Set Bus Voltage Range
BRNG_16 -> 16 V
BRNG_32 -> 32 V (DEFAULT)
*/
ina219.setBusRange(BRNG_32); // choose range and uncomment for change of default
ina219.setShuntSizeInOhms(0.1); // 100 mΩ shunt resistor
Serial.println("INA219 Current Sensor Example Sketch - Continuous");
/* If the current values delivered by the INA219 differ by a constant factor
from values obtained with calibrated equipment you can define a correction factor.
Correction factor = current delivered from calibrated equipment / current delivered by INA219
*/
ina219.setCorrectionFactor(0.99); // insert your correction factor if necessary
}
void loop()
{
float shuntVoltage_mV = 0.0;
float loadVoltage_V = 0.0;
float busVoltage_V = 0.0;
float current_mA = 0.0;
float power_mW = 0.0;
bool ina219_overflow = false;
shuntVoltage_mV = ina219.getShuntVoltage_mV();
busVoltage_V = ina219.getBusVoltage_V();
//here we use the I=U/R to calculate, here the Resistor is 100mΩ, accuracy can reach to 0.5%.
current_mA = shuntVoltage_mV/0.1;
power_mW = ina219.getBusPower();
loadVoltage_V = busVoltage_V + (shuntVoltage_mV/1000);
ina219_overflow = ina219.getOverflow();
Serial.print("Shunt Voltage [mV]: "); Serial.println(shuntVoltage_mV);
Serial.print("Bus Voltage [V]: "); Serial.println(busVoltage_V);
Serial.print("Load Voltage [V]: "); Serial.println(loadVoltage_V);
Serial.print("Current[mA]: "); Serial.println(current_mA);
Serial.print("Bus Power [mW]: "); Serial.println(power_mW);
if(!ina219_overflow){
Serial.println("Values OK - no overflow");
}
else{
Serial.println("Overflow! Choose higher PGAIN");
}
Serial.println();
delay(1000);
}
If you experience any error in compiling the example sketch, check the updated code for your WisBlock Core Module that can be found on the RAK16000 WisBlock Example Code Repository and this sample code in Github will work on all WisBlock Core.
- Once the example sketch is open, install the INA219_WE library by clicking the yellow-highlighted link.
Figure 1: Opening the INA219_WE library in Arduino IDEClick on the Install button to finish library installation.
Figure 1: Installing INA219_WE from Arduino Library Manager- Select the right port and upload the code.
Figure 1: Selecting the correct serial port
Figure 1: Uploading the RAK16000 sample code- After successful upload, open Arduino Serial Monitor and check the current current consumption and load voltage.
Figure 1: RAK16000 Serial Monitor