You are on page 1of 4

C:\Users\dntg\Documents\Arduino\battery_log\battery_log.

ino mercoledì 3 gennaio 2018 19:27


/*
* Battery charging Logging
* Will use an INA219 sensing Current and Battery Voltage module
* Battery Voltage and Charging Current are displayed on a Nokia5110 LCD
* These same values are sent to an Excel spreadsheet
* for later graphing and analysis
*/

/*
* PCD8544 - Interface with Philips PCD8544 (or compatible) NOKIA 5110 LCDs.
*
* Copyright (c) 2010 Carlos Rodrigues <cefrodrigues@gmail.com>
*
* To use this sketch, connect the eight pins from your LCD like this:
*
* Pin 1 (GND) -> Arduino GND +3.3V (rightmost, when facing the display head-on)
* Pin 2 (LIGHT) -> Center Pin of Potentiometer
* Pin 3 (Vcc) -> Arduino 3.3V
* Pin 4 (CLK) -> Arduino digital pin 8
* Pin 5 (DIN) -> Arduino digital pin 9
* Pin 6 (DC) -> Arduino digital pin 10
* Pin 7 (CE) -> Arduino digital pin 12
* Pin 8 (RST) -> Arduino digital pin 11
*
* Since these LCDs are +3.3V devices, you have to add extra components to
* connect it to the digital pins of the Arduino (not necessary if you are
* using a 3.3V variant of the Arduino, such as Sparkfun's Arduino Pro).
*/

#include <PCD8544.h>
#include <Keyboard.h>
#include <Wire.h>
#include <Adafruit_INA219.h>

boolean keyboardOn = false;


boolean firstTime = true;
const int buttonPin = 5; // inpur for rotary push button

// A custom glyph (a smiley)...


static const byte glyph[] = { B00010000, B00110100, B00110000, B00110100, B00010000 };

static PCD8544 lcd;

Adafruit_INA219 ina219;
// Define Current and Voltage variables
const unsigned int n_max = 5; // Numero di misure su cui effettuare la media

float shuntvoltage[n_max-1];
float busvoltage[n_max-1];
float current_mA[n_max-1];
float loadvoltage[n_max-1];
float mean_current_mA[2];
float mean_loadvoltage[2];
float diff_current_mA[1];
float diff_loadvoltage[1];
float display_lcd_voltage;
float display_lcd_current;

static unsigned int counter = 0;


static unsigned int delay_time = 500;
static unsigned int time_max = 10000; // Tempo MAX fra una registrazione e la successiva
(in millisec)

/*
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to
program the Zero!
#define Serial SerialUSB
#endif
*/
-1-
C:\Users\dntg\Documents\Arduino\battery_log\battery_log.ino mercoledì 3 gennaio 2018 19:27
void setup(void)
{
// PCD8544-compatible displays may have a different resolution...
lcd.begin(84, 48);
lcd.setContrast(40); // sets LCD contrast from 0 to 127

// Add the smiley to position "0" of the ASCII table...


lcd.createChar(0, glyph);
/*
#ifndef ESP8266
while (!Serial); // will pause Zero, Leonardo, etc until serial console opens
#endif
*/
uint32_t currentFrequency;

Serial.begin(115200);
Serial.println("Hello!");

// Initialize the INA219.


// By default the initialization will use the largest range (32V, 2A). However
// you can call a setCalibration function to change this range (see comments).
ina219.begin();
//ina219.setCalibration_32V_2A();
// To use a slightly lower 32V, 1A range (higher precision on amps):
//ina219.setCalibration_32V_1A();
// Or to use a lower 16V, 400mA range (higher precision on volts and amps):
//ina219.setCalibration_16V_400mA();

pinMode(buttonPin, INPUT); // set pin to input


digitalWrite(buttonPin, HIGH); // turn on pullup resistor

// initialize control over the keyboard:


Keyboard.begin();

// Write a piece of text on the first line...


lcd.setCursor(0, 0);
lcd.print("Batt chrg Log");

delay(2000);
Serial.println("Measuring voltage and current with INA219 ...");

for(int j=0; j<n_max; j++){


acquire_current(j);
mean_current_mA[2] += current_mA[j];
mean_loadvoltage[2] += loadvoltage[j];
}
mean_current_mA[2] /= n_max;
mean_loadvoltage[2] /= n_max;
display_current();
display_lcd_voltage = mean_loadvoltage[2];
display_lcd_current = mean_current_mA[2];
}

void loop(void)
{
unsigned long ref_time = millis();
while (millis()-ref_time < time_max) { // Delay deve essere 60000
for(int j=0; j<n_max; j++){
acquire_current(j);
mean_current_mA[2] += current_mA[j];
mean_loadvoltage[2] += loadvoltage[j];
}
mean_current_mA[0] = mean_current_mA[1];
mean_loadvoltage[0] = mean_loadvoltage[1];
mean_current_mA[1] = mean_current_mA[2];
mean_loadvoltage[1] = mean_loadvoltage[2];
mean_current_mA[2] /= n_max;
mean_loadvoltage[2] /= n_max;

diff_current_mA[0] = mean_current_mA[1]-display_lcd_current;
diff_loadvoltage[0] = mean_loadvoltage[1]-display_lcd_voltage;
-2-
C:\Users\dntg\Documents\Arduino\battery_log\battery_log.ino mercoledì 3 gennaio 2018 19:27
diff_current_mA[1] = mean_current_mA[2]-display_lcd_current;
diff_loadvoltage[1] = mean_loadvoltage[2]-display_lcd_voltage;

delay(1000);

if((diff_current_mA[0]*diff_current_mA[1]>0 && (abs(diff_current_mA[0])>5 ||


abs(diff_current_mA[1])>5))||(diff_loadvoltage[0]*diff_loadvoltage[1]>0 &&
(abs(diff_loadvoltage[0])>0.08 || abs(diff_loadvoltage[1])>0.08))) {
//if(abs(diff_loadvoltage[1])>0.06){
display_current();
display_lcd_voltage = mean_loadvoltage[2];
display_lcd_current = mean_current_mA[2];
keyb_log();
break;
}
}
if(millis()-ref_time > time_max) {
display_current();
display_lcd_voltage = mean_loadvoltage[2];
display_lcd_current = mean_current_mA[2];
keyb_log();
}
//delay(delay_time);
counter++;
}

void acquire_current(int n) {
shuntvoltage[n] = ina219.getShuntVoltage_mV();
busvoltage[n] = ina219.getBusVoltage_V();
current_mA[n] = ina219.getCurrent_mA();
loadvoltage[n] = busvoltage[n] + (shuntvoltage[n] / 1000);
}

void display_current() {
// Write measurements on the second line...
lcd.setCursor(0, 1);
if (mean_loadvoltage[2] <0) {
lcd.print("Volt:");
} else {
lcd.print("Volt: ");
}
lcd.print(mean_loadvoltage[2], 2);
lcd.print(" V");

lcd.setCursor(0, 2);
if (mean_current_mA[2] <0) {
lcd.print("Curr:");
} else {
lcd.print("Curr: ");
}
lcd.print(mean_current_mA[2], 2);
lcd.print(" mA");
lcd.write(0); // write the smiley

lcd.setCursor(0, 3);
lcd.print("Diff: ");
lcd.print(abs(diff_loadvoltage[1]));
// Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V");
// Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
Serial.print("Load Voltage: "); Serial.print(mean_loadvoltage[2]); Serial.println(" V");
Serial.print("Current: "); Serial.print(mean_current_mA[2]); Serial.println(" mA");
Serial.println("");
}

void keyb_log() {
if (!digitalRead(buttonPin)) { // encoder pressed
keyboardOn = !keyboardOn;
while (!digitalRead(buttonPin)) {}
}
if (keyboardOn) {

-3-
C:\Users\dntg\Documents\Arduino\battery_log\battery_log.ino mercoledì 3 gennaio 2018 19:27
// output to keyboard

if (firstTime) {
keyHeader();
firstTime = false;
}
Keyboard.print(millis() / 1000);
Keyboard.print(char(9));
Keyboard.print(mean_loadvoltage[2], 2);
Keyboard.print(char(9));
Keyboard.print(mean_current_mA[2], 0);
Keyboard.print(char(10));
Keyboard.println(char(13));
keyboardOn = false;
}
}

void keyHeader() {
Keyboard.print("Time");
Keyboard.print(char(9));
Keyboard.print("Volt");
Keyboard.print(char(9));
Keyboard.print("mA");
Keyboard.print(char(10));
Keyboard.println(char(13));
}

-4-

You might also like