#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#include "INA226.h"
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
INA226 INA1(0x40);
INA226 INA2(0x41);
#define Mosfet_1 PC13
#define Mosfet_2 PC14
void setup() {
pinMode(Mosfet_1, OUTPUT);
pinMode(Mosfet_2, OUTPUT);
Serial.begin(9600);
INA1.begin();
INA2.begin();
// put your setup code here, to run once:
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;

; // Don't proceed, loop forever
}
//display.display();
display.clearDisplay();
display.display();
delay(2000);
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
hienthi();
display.display();
INA2.setMaxCurrentShunt(1, 0.002);
INA1.setMaxCurrentShunt(1, 0.002);
// put your setup code here, to run once:
}
unsigned long time_clear;
void loop() {
if ((unsigned long)(millis() - time_clear) > 1000) {
display.fillRect(60, 0, 50, 15, SSD1306_BLACK);
display.setCursor(60, 0);
display.println(INA1.getBusVoltage(), 2);
display.fillRect(60, 25, 50, 15, SSD1306_BLACK);
display.setCursor(60, 25);
display.println(INA2.getBusVoltage(), 2);
display.fillRect(60, 50, 68, 15, SSD1306_BLACK);
display.setCursor(60, 50);
display.println(INA1.getCurrent(), 2);
display.setCursor(112, 50);
display.println("A");
display.display();
time_clear = millis();
}
if (INA1.getBusVoltage() > 3.55) {
digitalWrite(Mosfet_1, LOW);
} else {
digitalWrite(Mosfet_1, HIGH);
}
if (INA2.getBusVoltage() > 3.55) {
digitalWrite(Mosfet_2, LOW);
} else {
digitalWrite(Mosfet_2, HIGH);
}
}
void hienthi() {
display.setCursor(0, 0);
display.println("VOL1:");
//display.drawRect(60, 0, 50, 15, SSD1306_WHITE);
display.setCursor(60, 0);
display.println("3.33");
display.setCursor(112, 0);
display.println("V");
display.setCursor(0, 25);
display.println("VOL2:");
display.setCursor(112, 25);
display.println("V");
display.setCursor(0, 50);
display.println("CUR:");
display.setCursor(112, 50);
display.println("A");
}
Em dùng con INA226 để đo dòng - áp ( áp thì đã đo ổn định). Tuy nhiên chỗ dòng thì nó lại nhảy giá trị không ổn định ( từ 0A - 3,28A). Các bác biết cách lọc bớt nhiễu để lấy giá trị trung bình thì giúp em với ạ.
via theNEXTvoz for iPhone