Esp32c3-liquidcrystaldisplay-powermeter.ino

From Federal Burro of Information
Revision as of 23:28, 23 September 2023 by David (talk | contribs) (Created page with "<pre> /* LiquidCrystal Library - Hello World Demonstrates the use a 16x2 LCD display. The LiquidCrystal library works with all LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and you can usually tell them by the 16-pin interface. This sketch prints "Hello World!" to the LCD and shows the time. The circuit: * LCD RS pin to digital pin 12 * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 5 *...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
/*
  LiquidCrystal Library - Hello World

 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.

 This sketch prints "Hello World!" to the LCD
 and shows the time.

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 modified 7 Nov 2016
 by Arturo Guadalupi

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld

*/

// include the library code:
#include <LiquidCrystal.h>
#define PIN 13
#include <Wire.h>
#include <Adafruit_INA219.h>

#define I2C_SDA 0
#define I2C_SCL 1

Adafruit_INA219 ina219(0x41);
  float shuntvoltage = 0;
  float busvoltage = 0;
  float current_mA = 0;
  float loadvoltage = 0;
  float power_mW = 0;


// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
// orig const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const int rs = 18, en = 19, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  Wire.begin(I2C_SDA, I2C_SCL);
  // set up the LCD's number of columns and rows:
  Serial.begin(115200);
  while (!Serial) {
    delay(1);
  }

  Serial.println("ina219 setup()");
  if (! ina219.begin()) {
    Serial.println("Failed to find INA219 chip");
    while (1) { delay(10); }
  }
  ina219.setCalibration_16V_400mA();
  Serial.println("INA219 setup done OK");

  Serial.println("lcd setup()");
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop() {
  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);

  Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.print(" V");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.print(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.print(" V");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.print(" mA");
  Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(busvoltage);   lcd.print(",");
  lcd.print(shuntvoltage); lcd.print(",");
  lcd.print(loadvoltage);  lcd.print(",");
  lcd.print(current_mA);   lcd.print(",");
  lcd.print(power_mW);
  delay(1000);

}