在拍賣網站無意中知道有一個感測器,標題寫著「INA219 I2C 介面零漂移 雙向電流 電源監控感測器模組感測器」,想瞭解一下,這個監控電源的作法是如何達成的,看一下價格還算便宜,就買一個來實作如何紀錄電流。如果檢測僅用串列埠的視窗來看,有點麻煩,於是找了一個1.77吋的TFT LCD來顯示電流值,順便練習一下,如何使用Adafruit-GFX這個函式庫。
在 Arduino 選單, [Sketch 草稿碼] →下拉選擇 [Include Library 匯入程式庫] → [Manage Libraries 管理程式庫] 。在搜尋的地方輸入 Adafruit_GFX,選擇以下最上方的 Library,按下右方的 [Install],出現 [Installed] 表示安裝完成。
同樣的方法再安裝以下兩個 Library:
[INA219 電源監控感測器模組]
- 使用 I2C 介面
- 工作電壓 +3.0 至 +5.5V 之間
- 溫度範圍(可實現 1% 的最大誤差精度) -40C 至 +85C
- 分辨率 12 位元
- 電壓範圍為 0 V 至 +26 V
- 以安培為單位讀取電流,並以瓦特為單位讀取電源
- 對多達 128 個採樣求平均值
[安裝Adafruit_GFX Library]
Adafruit_GFX程式庫提供顯示的文字及圖形顯示所需的功能。在 Arduino 選單, [Sketch 草稿碼] →下拉選擇 [Include Library 匯入程式庫] → [Manage Libraries 管理程式庫] 。在搜尋的地方輸入 Adafruit_GFX,選擇以下最上方的 Library,按下右方的 [Install],出現 [Installed] 表示安裝完成。
同樣的方法再安裝以下兩個 Library:
- Adafruit_ST7735
- Adafruit_INA219
[材料]
- Arduino Uno x 1
- 電流傳感器電壓表 INA219 x 1
- ST7735 1.77" TFT LCD x 1
- 麵包板 x 1
- 麵包板電源 x 1
- 9V 電池(含接線) x 1
- LED x 1
- 220歐姆電阻 x 1
- 連接線 x n條
[接線與電路圖]
| Arduino Uno | LCD Pin | INA219 |
|---|---|---|
| 3.3V | 8 LEDA | - |
| D10 | 7 CS | - |
| D9 | 6 RS | - |
| D8 | 5 RES | - |
| D11 | 4 SDA | - |
| D13 | 3 SCK | - |
| 5V | 2 VCC | VCC |
| GND | 1 GND | GND |
| A4(SDA) | - | SDA |
| A5(SCL) | - | SCL |
[程式]
程式內有一個 Color565() 的函式,是將紅色R、綠色G及藍色B三原色轉換成十六進位值色碼。#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("");
// 輸出資料
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); // 紅色
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); // 綠色
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); // 藍色
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); // 暗綠色
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); // 亮紅色
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);
}
[實作結果]
[參考資料]
- INA219 Arduino Current Sensor Voltmeter Tutorial: Quick Start
- 1.77 Inch TFT LCD Display With ST7735S On Arduino Mega 2560



張貼留言