Panel Cookies
yt_link
insta_link
fb_link
twitter_link

Arduino power meter code

Una publicación compartida de ELECTRONOOBS®️ (@electronoobs) el


Arduino power meter code - MAX471


Help me by sharing this post



Download the .zip file below. Open it on your Arduino IDE and upload it to the Arduino and make sure you have the connections as in the schematic. Also, read all the comments in the code to understand more. You could also copy and paste the code fom below.
Download full ocde here (MAX471)



This code is for the schematic.





Remember to install the Adafruit_GFX library
Downlaod the Adafruit_GFX library here:



/*********************************************************************
Power meter example. Author: ELECTRONOOBS. 08/05/2018
Tutorial video: https://www.youtube.com/watch?v=_PKQdEUam6Y
Tutorial link: http://www.electronoobs.com/eng_arduino_tut28.php
*********************************************************************/


//Inport the libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif


//variables
float voltage = 0;
float current_voltage = 0;          //this maps the measured voltage in volts from 0 t 3.7V
float current =  0; 
float power=0;
float voltage_compenstion = 0;
float energy = 0;
int RawValue= 0;

unsigned long previousMillis = 0;
unsigned long loop_delay = 100;
unsigned long Time = 0;

void setup()   {                
  //Begin the display i2c omunication
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)  
}


//THIS FUNCTION WILL MAP THE float VALUES IN THE GIVEN RANGE
float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

//This functions will calculate the curret, voltage, power and energy values
void get_values()
{
  voltage = (fmap(analogRead(A1),0.0,1023.0,0.0,3.7)) / 0.218;
  //The module gives one volt per Amp.
  RawValue = analogRead(A0); 
  current = (RawValue * 5.0 )/ 1024.0; // scale the ADC, we get current value in Amps
  
  power = voltage*current;              //Calculate power and energy
  energy = energy + (power/3600)/1000;
}



void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= loop_delay)
  {
    previousMillis = currentMillis;
    get_values();
    Time = currentMillis / 1000;        //Get the elapsed time in seconds

    //Print all the values on the display
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.print(voltage);
    display.print("V");

    display.setCursor(60,0);
    display.print(current);
    display.print("A");

    display.setCursor(0,11);
    display.print(power);
    display.print("W");

    display.setCursor(60,11);
    display.print(energy);
    display.print("mWh");

    display.setCursor(100,22);
    display.print(Time);
    display.print("s");
        
    display.display();    //This functions will display the data
  }
}


Arduino power meter tutorial












Affiliate Disclosure

ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel Intermedio