safwan092

Fan 12V With Current Sensor and 20x4LCD Arduino PowerSupply 12V3A Power Cost in SAR

Nov 13th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_INA219.h>
  3. #include <LiquidCrystal_I2C.h>
  4.  
  5. float cost_per_kWh = 0.18; // in SAR Residential is 18 Hallala/kWh
  6. Adafruit_INA219 ina219;
  7. LiquidCrystal_I2C lcd(0x27, 20, 4);
  8.  
  9. void setup() {
  10. Serial.begin(9600);
  11. lcd.init();
  12. lcd.init();
  13. lcd.backlight();
  14. // Initialize the INA219.
  15. if (! ina219.begin())
  16. {
  17. Serial.println("Failed to find INA219 chip");
  18. while (1) {
  19. delay(10);
  20. }
  21. }
  22. // To use a slightly lower 32V, 1A range (higher precision on amps):
  23. //ina219.setCalibration_32V_1A();
  24. // Or to use a lower 16V, 400mA range, call:
  25. //ina219.setCalibration_16V_400mA();
  26. Serial.println("Measuring voltage, current, and power with INA219 ...");
  27. }
  28.  
  29. void loop() {
  30. float shuntvoltage = 0;
  31. float busvoltage = 0;
  32. float current_mA = 0;
  33. float loadvoltage = 0;
  34. float power_mW = 0;
  35. float cost_SAR = 0;
  36.  
  37. shuntvoltage = ina219.getShuntVoltage_mV();
  38. busvoltage = ina219.getBusVoltage_V();
  39. current_mA = ina219.getCurrent_mA();
  40. power_mW = ina219.getPower_mW();
  41. loadvoltage = busvoltage + (shuntvoltage / 1000);
  42. cost_SAR = ((power_mW * cost_per_kWh / 1000) / 1000)*30;
  43.  
  44. Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V");
  45. Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  46. Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V");
  47. Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
  48. Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW");
  49. Serial.println("");
  50.  
  51. lcd.setCursor(0, 0);
  52. lcd.print("V = ");
  53. lcd.print(loadvoltage);
  54. lcd.print(" V");
  55. lcd.setCursor(0, 1);
  56. lcd.print("I = ");
  57. lcd.print(current_mA);
  58. lcd.print(" mA");
  59. lcd.setCursor(0, 2);
  60. lcd.print("P = ");
  61. lcd.print(power_mW);
  62. lcd.print("mW");
  63. lcd.setCursor(0, 3);
  64. lcd.print("$ = ");
  65. lcd.print(cost_SAR);
  66. lcd.print(" SAR");
  67. delay(1000);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment