Advertisement
safwan092

Untitled

Dec 2nd, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_INA219.h>
  3. #include <LiquidCrystal_I2C.h>
  4.  
  5. Adafruit_INA219 ina219;
  6. LiquidCrystal_I2C lcd(0x27, 16, 2);
  7.  
  8. void setup()
  9. {
  10. Serial.begin(9600);
  11. lcd.init();
  12. lcd.init();
  13. lcd.backlight();
  14. lcd.setCursor(0, 0);
  15. lcd.print("Hello, world!");
  16. if (!ina219.begin()) {
  17. Serial.println("INA219 not found");
  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 (higher precision on volts and amps):
  25.  
  26. ina219.setCalibration_16V_400mA(); // Set calibration for INA219
  27. delay(2000);
  28. lcd.clear();
  29. }
  30.  
  31. void loop()
  32. {
  33. float shuntVoltage = 0;
  34. float busVoltage = 0;
  35. float current_mA = 0;
  36. float loadVoltage = 0;
  37. float power_mW = 0;
  38.  
  39. shuntVoltage = ina219.getShuntVoltage_mV(); // Read shunt voltage
  40. busVoltage = ina219.getBusVoltage_V(); // Read bus voltage
  41. current_mA = ina219.getCurrent_mA(); // Read current
  42. power_mW = ina219.getPower_mW(); // Read power
  43. loadVoltage = busVoltage + (shuntVoltage / 1000); // Calculate load voltage
  44.  
  45. Serial.print("Bus Voltage: ");
  46. Serial.print(busVoltage);
  47. Serial.println(" V");
  48. Serial.print("Shunt Voltage: ");
  49. Serial.print(shuntVoltage);
  50. Serial.println(" mV");
  51. Serial.print("Load Voltage: ");
  52. Serial.print(loadVoltage);
  53. Serial.println(" V");
  54. Serial.print("Current: ");
  55. Serial.print(current_mA);
  56. Serial.println(" mA");
  57. Serial.print("Power: ");
  58. Serial.print(power_mW);
  59. Serial.println(" mW");
  60. Serial.println("");
  61. lcd.clear();
  62. lcd.setCursor(0, 0);
  63. lcd.print("VxI=");
  64. lcd.print(loadVoltage);
  65. lcd.print("Vx");
  66. lcd.print(abs(current_mA));
  67. lcd.print("mA");
  68. lcd.setCursor(0, 1);
  69. lcd.print("P = ");
  70. lcd.print(power_mW);
  71. lcd.print("mW");
  72. delay(2000); // Delay for 2 seconds
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement