Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <Adafruit_INA219.h>
- #include <LiquidCrystal_I2C.h>
- float cost_per_kWh = 0.18; // in SAR Residential is 18 Hallala/kWh
- Adafruit_INA219 ina219;
- LiquidCrystal_I2C lcd(0x27, 20, 4);
- void setup() {
- Serial.begin(9600);
- lcd.init();
- lcd.init();
- lcd.backlight();
- // Initialize the INA219.
- if (! ina219.begin())
- {
- Serial.println("Failed to find INA219 chip");
- while (1) {
- delay(10);
- }
- }
- // To use a slightly lower 32V, 1A range (higher precision on amps):
- //ina219.setCalibration_32V_1A();
- // Or to use a lower 16V, 400mA range, call:
- //ina219.setCalibration_16V_400mA();
- Serial.println("Measuring voltage, current, and power with INA219 ...");
- }
- void loop() {
- float shuntvoltage = 0;
- float busvoltage = 0;
- float current_mA = 0;
- float loadvoltage = 0;
- float power_mW = 0;
- float cost_SAR = 0;
- shuntvoltage = ina219.getShuntVoltage_mV();
- busvoltage = ina219.getBusVoltage_V();
- current_mA = ina219.getCurrent_mA();
- power_mW = ina219.getPower_mW();
- loadvoltage = busvoltage + (shuntvoltage / 1000);
- cost_SAR = ((power_mW * cost_per_kWh / 1000) / 1000)*30;
- 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(loadvoltage); Serial.println(" V");
- Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
- Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW");
- Serial.println("");
- lcd.setCursor(0, 0);
- lcd.print("V = ");
- lcd.print(loadvoltage);
- lcd.print(" V");
- lcd.setCursor(0, 1);
- lcd.print("I = ");
- lcd.print(current_mA);
- lcd.print(" mA");
- lcd.setCursor(0, 2);
- lcd.print("P = ");
- lcd.print(power_mW);
- lcd.print("mW");
- lcd.setCursor(0, 3);
- lcd.print("$ = ");
- lcd.print(cost_SAR);
- lcd.print(" SAR");
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment