Arduino(9): 1.77" TFT LCD display INA219 current sensor & voltage value

Inadvertently knowing that there is a sensor on the auction site, the title reads "INA219 I2C interface zero-drift bidirectional current power monitoring sensor module sensor", I would like to know how this monitoring power supply is achieved, see If the price is still cheap, buy one to implement how to record the current. If the detection is only a series of windows, it is a bit of a hassle, so I found a 1.77 inch TFT LCD to display the current value. By the way, how to use the Adafruit-GFX library.


[INA219 Power Monitoring Sensor Module]

  • Use the I2C interface
  • Operating voltage  between +3.0 and +5.5V
  • Temperature range (1% maximum error accuracy)  -40C to +85C
  • Resolution  12 bits
  • Voltage range from  0 V to +26 V
  • Read current in amps and read power in watts
  • Average up to 128 samples
With the industry's highest accuracy and the smallest size, the INA219 not only monitors the voltage drop across the shunt resistor, senses the shunt supply voltage, but also calculates the power supply. Available in a SOT23 package, the device provides a small, low-cost solution for servo, notebook, power, battery management, and digital current sensing in automotive and telecom equipment.

[Install Adafruit_GFX Library]

The Adafruit_GFX library provides the text and graphics needed to display the desired functionality. 
In the Arduino menu, [Sketch] → drop down and select [Include Library] → [Manage Libraries]. In the search area, enter Adafruit_GFX, select the following red frame of the Library, press [Install] on the right, and [Installed] appears to indicate that the installation is complete.

The same method installs the following two libraries:
  • Adafruit_ST7735
  • Adafruit_INA219

[Material]

  • Arduino Uno x 1
  • Current Sensor Voltmeter INA219 x 1
  • ST7735 1.77" TFT LCD x 1
  • Breadboard x 1
  • Breadboard power supply x 1
  • 9V battery (with wiring) x 1
  • LED x 1
  • 220 ohm resistor x 1
  • Connection line xn

[Wiring and circuit diagram]

Arduino
LCD Pin
INA219
3.3V
8 LEDA
-
Pin10
7 CS
-
Pin9
6 RS
-
Pin 8
5 RES
-
Pin 11
4 SDA
-
Pin 13
3 SCK
-
5V
2 VCC
VCC
GND
1 GND
GND
A4 (SDA)
-
SDA
A5 (SCL)
-
SCL


[Code]

#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
#define TFT_SCLK 13   
#define TFT_MOSI 11   

#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_INA219.h>
#include <Wire.h>
#include <SPI.h>
#include <stdio.h>

Adafruit_INA219 ina219;

#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif

//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

//Black theme
#define COLOR1 ST7735_WHITE
#define COLOR2 ST7735_BLACK

//White theme
//#define COLOR1 ST7735_BLACK
//#define COLOR2 ST7735_WHITE

int text_color_humidex;
float humidity, temperature, humidex;
String message;

void setup(void)
{
    Serial.begin(9600);
    uint32_t currentFrequency;
    
    Serial.println("Hello!");
      
    
    Wire.begin();
    ina219.begin();
    Serial.println("Measuring voltage and current with INA219 ...");
    
    tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
    tft.fillScreen(COLOR2);
}

void loop()
{
    float shuntvoltage = 0;
    float busvoltage = 0;
    float current_mA = 0;
    float loadvoltage = 0;
    float power_mW = 0;

    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.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("");
  
    // output
    tft.setCursor(0,1);
    tft.setTextSize(1);
    tft.setTextColor(COLOR1, COLOR2);        
    tft.println("Bus Voltage:   "); 
    tft.setTextSize(2);
    tft.setTextColor(Color565(200, 0, 0), COLOR2); // red 
    tft.print(busvoltage); tft.println(" V");
        
    tft.setTextSize(1);
    tft.setTextColor(COLOR1, COLOR2);    
    tft.println("");    
    tft.println("Shunt Voltage: "); 
    tft.setTextSize(2);
    tft.setTextColor(Color565(0, 255, 0), COLOR2); // green
    tft.print(shuntvoltage); tft.println(" mV");
        
    tft.setTextSize(1);
    tft.setTextColor(COLOR1, COLOR2);
    tft.println("");
    tft.println("Load Voltage:  "); 
    tft.setTextSize(2);
    tft.setTextColor(Color565(0, 0, 255), COLOR2); // blue    
    tft.print(loadvoltage); tft.println(" V");
    
    tft.setTextSize(1);
    tft.setTextColor(COLOR1, COLOR2);
    tft.println("");
    tft.println("Current:       "); 
    tft.setTextSize(2);
    tft.setTextColor(Color565(70, 200, 70), COLOR2); // dark green
    tft.print(current_mA); tft.println(" mA");
        
    tft.setTextSize(1);
    tft.setTextColor(COLOR1, COLOR2);
    tft.println("");
    tft.println("Power:         "); 
    tft.setTextSize(2);
    tft.setTextColor(Color565(255, 56, 76), COLOR2); // bright red
    tft.print(power_mW); tft.println(" mW");
    tft.println("");
    
    delay(2000);
    tft.fillScreen(COLOR2);
}

// Pass 8-bit (each) R,G,B, get back 16-bit packed color
uint16_t Color565(uint8_t r, uint8_t g, uint8_t b)
{
    return ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3);
}


[Results]




[Reference]


Post a Comment

Previous Post Next Post