INA219_test.ino 2.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
  This code was designed specifically for usage with the openCanSat kit (https://opencansat.eu)
  
  This program is free software; you can redistribute it 
  and/or modify it under the terms of the GNU General    
  Public License as published by the Free Software       
  Foundation; either version 3 of the License, or        
  (at your option) any later version.
                   
  You should have received a copy of the GNU General    
  Public License along with this program.
  If not, see <http://www.gnu.org/licenses/>.
  Licence can be viewed at                               
  http://www.gnu.org/licenses/gpl-3.0.txt
  Please maintain this license information along with authorship
  and copyright notices in any redistribution of this code
 */
Markéta Jedličková's avatar
Markéta Jedličková committed
18
#include <Adafruit_INA219.h> 	// Include Adafruit INA219 library
19

Markéta Jedličková's avatar
Markéta Jedličková committed
20
#define Serial SerialUSB			// Change default serial to SAMD USB serial
21

Markéta Jedličková's avatar
Markéta Jedličková committed
22
// Create object 'ina219' from the library with address 0x40
23
24
25
26
27
28
// (according to the circuit, which will be used to access the
// library methods by a dot notation
Adafruit_INA219 ina219(0x40);

int ms_delay = 500; // Number of milliseconds between each two readings

Markéta Jedličková's avatar
Markéta Jedličková committed
29
// Empty variables for measured values
30
31
32
33
34
float voltage_shunt = 0;
float voltage_bus = 0;
float current_mA = 0;
float voltage_load = 0;
  
Markéta Jedličková's avatar
Markéta Jedličková committed
35
// This will only run once
36
37
38
39
void setup(void) 
{ 
  Serial.begin(57600); // start serial comm with baud rate of 57600Bd

Markéta Jedličková's avatar
Markéta Jedličková committed
40
  // Wait for the Arduino serial (on your PC) to connect
41
42
43
44
45
46
47
  // please, open the Arduino serial console (right top corner)
  // note that the port may change after uploading the sketch
  // COMMENT OUT FOR USAGE WITHOUT A PC!
  while(!Serial);
  
  Serial.println("openCanSat INA219 test started");

Markéta Jedličková's avatar
Markéta Jedličková committed
48
  // Begin communication with the INA219
49
50
51
  ina219.begin();
}

Markéta Jedličková's avatar
Markéta Jedličková committed
52
// Code in this function will run repeatedly
53
54
void loop(void)
{
Markéta Jedličková's avatar
Markéta Jedličková committed
55
  // Read values from INA219 into the variables
56
57
58
59
60
  voltage_shunt = ina219.getShuntVoltage_mV();
  voltage_bus = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  voltage_load = voltage_bus + (voltage_shunt / 1000);

Markéta Jedličková's avatar
Markéta Jedličková committed
61
  // Print the measured variables to serial
62
63
64
65
66
67
  Serial.println("Shunt Voltage: " + (String) voltage_shunt + " mV"); 
  Serial.println("Bus Voltage: " + (String) voltage_bus + " V");
  Serial.println("Current: " + (String) current_mA + " mA");  
  Serial.println("Load Voltage: " + (String) voltage_load + " V"); 
  Serial.println(); 
  
Markéta Jedličková's avatar
Markéta Jedličková committed
68
  delay(ms_delay); // Wait a while (defined at the beginning)
69
}