229 lines
5.6 KiB
C++
229 lines
5.6 KiB
C++
// Suitable with 12v battery, adaptor wattage, current and voltage monitoring.
|
||
// Resistor divider network need more upgradation if voltage is higher that 15.6
|
||
// Try 0x3D OLED address if screen did not work.
|
||
// Precision of the voltage depends on the tolerance of the resistors.
|
||
|
||
#include <Wire.h>
|
||
#include <Adafruit_GFX.h>
|
||
#include <Adafruit_SSD1306.h>
|
||
#include <OneWire.h>
|
||
#include <DallasTemperature.h>
|
||
|
||
#define OLED_RESET -1
|
||
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
|
||
|
||
// Data wire is plugged into port 2 on the Arduino
|
||
#define ONE_WIRE_BUS 2
|
||
|
||
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
|
||
OneWire oneWire(ONE_WIRE_BUS);
|
||
|
||
// Pass our oneWire reference to Dallas Temperature.
|
||
DallasTemperature sensors(&oneWire);
|
||
|
||
void setup() {
|
||
// put your setup code here, to run once:
|
||
Serial.begin(9600);
|
||
|
||
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Change the address to your's one
|
||
display.clearDisplay();
|
||
// Start up the library
|
||
sensors.begin();
|
||
display.clearDisplay();
|
||
|
||
display.setTextSize(1);
|
||
display.setTextColor(WHITE);
|
||
display.setCursor(0, 30);
|
||
// Display static text
|
||
display.println("F4IYT 1.00"); //Write custom text here
|
||
display.display();
|
||
delay(100);
|
||
// Scroll in various directions, pausing in-between:
|
||
display.startscrollright(0x00, 0x0F);
|
||
delay(2000);
|
||
display.stopscroll();
|
||
delay(1000);
|
||
display.startscrollleft(0x00, 0x0F);
|
||
delay(1000);
|
||
display.stopscroll();
|
||
delay(1000);
|
||
display.startscrolldiagright(0x00, 0x07);
|
||
delay(1000);
|
||
display.startscrolldiagleft(0x00, 0x07);
|
||
delay(1000);
|
||
display.stopscroll();
|
||
delay(1000);
|
||
}
|
||
|
||
float Vref=5.00;
|
||
float pts=1024.0;
|
||
//5v
|
||
float R4=10000.0; // R4 10K
|
||
float R6=10000.0; // R6 10K
|
||
//12v
|
||
float R3=100000.0; // R4 100K
|
||
float R5=10000.0; // R6 10K
|
||
//27v
|
||
float R1=100000.0; // R4 100K
|
||
float R2=10000.0; // R6 10K
|
||
|
||
//amp => 0.100 for 20A, 0.185 for 30A, 0.66 for 5A
|
||
// follow this link:
|
||
//https://startingelectronics.org/articles/arduino/measuring-voltage-with-arduino/
|
||
float amp=0.185;
|
||
|
||
void loop() {
|
||
// put your main code here, to run repeatedly:
|
||
int adc0 = analogRead(A0);
|
||
int adc1 = analogRead(A1);
|
||
int adc2 = analogRead(A2);
|
||
int adc3 = analogRead(A3);
|
||
int adc6 = analogRead(A6);
|
||
int adc7 = analogRead(A7);
|
||
//Gestion 27v
|
||
float voltage0 = (adc0 * Vref) / pts;
|
||
float voltage1 = (((adc1 * Vref)/pts)/(R2/(R1+R2))); // resistor divider basics needed to be learn
|
||
float current0 = (voltage0 - 2.5) / amp;
|
||
//Gestion 12v
|
||
float voltage2 = (adc2 * Vref) / pts;
|
||
float voltage3 = (((adc3 * Vref)/pts)/(R5/(R3+R5))); // resistor divider basics needed to be learn
|
||
float current2 = (voltage2 - 2.5) / amp;
|
||
//Gestion 5V
|
||
float voltage6 = (adc6 * Vref) / pts;
|
||
float voltage7 = (((adc7 * Vref)/pts)/(R6/(R4+R6))); // resistor divider basics needed to be learn
|
||
float current6 = (voltage6 - 2.5) / amp;
|
||
|
||
|
||
// request to all devices on the bus
|
||
sensors.requestTemperatures(); // Send the command to get temperatures
|
||
|
||
// After we got the temperatures, we can print them here.
|
||
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
|
||
float tempC0 = sensors.getTempCByIndex(0);
|
||
float tempC1 = sensors.getTempCByIndex(1);
|
||
float tempC2 = sensors.getTempCByIndex(2);
|
||
|
||
|
||
//Check
|
||
if (voltage1<0.99) {
|
||
voltage1=0.00;
|
||
}
|
||
if (voltage3<0.99) {
|
||
voltage3=0.00;
|
||
}
|
||
if (voltage7<0.99) {
|
||
voltage7=0.00;
|
||
}
|
||
if (current0 < 0.16) {
|
||
current0 = 0;
|
||
}
|
||
if (current2 < 0.16) {
|
||
current2 = 0;
|
||
}
|
||
if (current6 < 0.16) {
|
||
current6 = 0;
|
||
}
|
||
if (tempC0<0.00) {
|
||
tempC0=0.00;
|
||
}
|
||
if (tempC1<0.00) {
|
||
tempC1=0.00;
|
||
}
|
||
if (tempC2<0.00) {
|
||
tempC2=0.00;
|
||
}
|
||
//if (Serial.available()>0) {
|
||
//if (Serial.read()=='m') {
|
||
//Courant
|
||
Serial.print(current0);
|
||
Serial.print(F(","));
|
||
Serial.print(current2);
|
||
Serial.print(F(","));
|
||
Serial.print(current6);
|
||
Serial.print(F(","));
|
||
//Tension
|
||
Serial.print(voltage1);
|
||
Serial.print(F(","));
|
||
Serial.print(voltage3);
|
||
Serial.print(F(","));
|
||
Serial.print(voltage7);
|
||
Serial.print(F(","));
|
||
//Temperature
|
||
Serial.print(tempC0);
|
||
Serial.print(F(","));
|
||
Serial.print(tempC1);
|
||
Serial.print(F(","));
|
||
Serial.print(tempC2);
|
||
Serial.print(F("\n"));
|
||
//}
|
||
//}
|
||
|
||
//Gestion display
|
||
display.clearDisplay();
|
||
display.setTextSize(1);
|
||
display.setTextColor(WHITE);
|
||
|
||
display.setCursor(0,0);
|
||
display.print("A: ");
|
||
display.print(current0);
|
||
display.display();
|
||
|
||
display.setCursor(0,10);
|
||
display.print("V: ");
|
||
display.print(voltage1);
|
||
display.display();
|
||
|
||
display.setCursor(0,20);
|
||
display.print("P: ");
|
||
display.print(current0*voltage1);
|
||
display.display();
|
||
|
||
display.setCursor(60,0);
|
||
display.print("A: ");
|
||
display.print(current2);
|
||
display.display();
|
||
|
||
display.setCursor(60,10);
|
||
display.print("V: ");
|
||
display.print(voltage3);
|
||
display.display();
|
||
|
||
display.setCursor(60,20);
|
||
display.print("P: ");
|
||
display.print(current2*voltage3);
|
||
display.display();
|
||
|
||
display.setCursor(0,30);
|
||
display.print("A: ");
|
||
display.print(current6);
|
||
display.display();
|
||
|
||
display.setCursor(0,40);
|
||
display.print("V: ");
|
||
display.print(voltage7);
|
||
display.display();
|
||
|
||
display.setCursor(0,50);
|
||
display.print("P: ");
|
||
display.print(current6*voltage7);
|
||
display.display();
|
||
|
||
display.setCursor(60,30);
|
||
display.print("T0: ");
|
||
display.print(tempC0);
|
||
display.display();
|
||
|
||
display.setCursor(60,40);
|
||
display.print("T1: ");
|
||
display.print(tempC1);
|
||
display.display();
|
||
|
||
display.setCursor(60,50);
|
||
display.print("T2: ");
|
||
display.print(tempC2);
|
||
display.display();
|
||
|
||
|
||
delay(3000);
|
||
}
|