Advertisement
CuriousScientist

Arduino power meter based on the ACS 712 and the ADS1115

Jun 26th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //The code belongs to the following tutorial video: https://youtu.be/wqADfB0Y4ts
  3.  
  4.  
  5. #include "SSD1306Ascii.h"
  6. #include "SSD1306AsciiAvrI2c.h"
  7. #define I2C_ADDRESS 0x3C //Address
  8. #define RST_PIN -1 //For OLED with no reset pin
  9. SSD1306AsciiAvrI2c display;
  10.  
  11. #include <Adafruit_ADS1015.h>
  12.  
  13. Adafruit_ADS1115 ads;
  14.  
  15. unsigned long startTime;
  16. unsigned long elapsedTime;
  17.  
  18. float Voltage;
  19. float Current;
  20. float Power;
  21.  
  22. void setup(void)
  23. {  
  24.   Serial.begin(9600);  //start serial connection
  25.   Serial.println("Power Tester"); //print a message on the serial terminal
  26.  
  27.   // The ADC input range (or gain) can be changed via the following
  28.   // functions, but be careful never to exceed VDD +0.3V max, or to
  29.   // exceed the upper and lower limits if you adjust the input range!
  30.   // Setting these values incorrectly may destroy your ADC!
  31.   //                                                                ADS1015  ADS1115
  32.   //                                                                -------  -------
  33.   // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  34.   // ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  35.   // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  36.   // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  37.   // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
  38.   // ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
  39.  
  40.   //ADS SETUP PART
  41.   //----Set default------
  42.   //ads.setSPS(DR_128SPS); //Sampling speed -some libraries don't support this. check it for yourself.
  43.  
  44.   ads.setGain(GAIN_TWOTHIRDS);
  45.   ads.begin();
  46.  
  47.  
  48.   //--------------------------------------------------------------------------------------------
  49.   //OLED part-----------------------------------------------------------------------------------
  50.   #if RST_PIN >= 0
  51.   display.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN);
  52.   #else // RST_PIN >= 0
  53.   display.begin(&Adafruit128x32, I2C_ADDRESS);
  54.   #endif // RST_PIN >= 0
  55.   //Call oled.setI2cClock(frequency) to change from the default frequency.
  56.  
  57.   display.setFont(System5x7);
  58.   display.set1X(); //set2x() is too large for the 128*32 oled
  59.   display.clear();
  60.   //--endofOLED----
  61.  
  62.   startTime = millis(); //"start" timer
  63.  
  64. }
  65.  
  66. void loop(void)
  67. {
  68.  
  69.   MeasurePower();
  70.   PrintSerial();
  71.   PrintOLED();  
  72.   delay(500);
  73.  
  74.  
  75.  
  76. //--------------------------------------------------------------------------------------------------------
  77. }
  78.  
  79. void MeasurePower()
  80. {  
  81.  elapsedTime = millis() - startTime; //"note down" the elapsed time
  82.  
  83.  //Converts the raw data to mV (* 0.1875), then to V (/1000)
  84.  // 4.3985 is the division factor that I got for the voltage divider.
  85.  Voltage = 4.3985 * ads.readADC_Differential_0_1() * 0.1875 / 1000;  //This should give the output in VOLTS
  86.  
  87.  //You have to calibrate the sensor!
  88.  //My function: I (in Amps) = ([Measured voltage in mV] - 2309.098) / 92.10048
  89.  Current = ((ads.readADC_Differential_2_3()* 0.1875) - 2309.098 ) / 92.10048; //This should give the output in AMPS
  90.  
  91.  //Watt = U * I
  92.  Power = Voltage * Current;
  93.  
  94. }
  95. void PrintSerial()
  96. {    
  97.   Serial.print(elapsedTime/1000); //print time in seconds
  98.   Serial.print(" "); //space as delimiter
  99.   Serial.print(Voltage,3);
  100.   Serial.print(" ");
  101.   Serial.print(Current,3);
  102.   Serial.print(" ");
  103.   Serial.println(Power,3);   //we do a linebreak together with the last print  
  104. }
  105.  
  106. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  107. //The code belongs to the following tutorial video: https://youtu.be/wqADfB0Y4ts
  108.  
  109. void PrintOLED()
  110. {  
  111.   //128x32 OLED
  112.   //1st line of the OLED
  113.   display.clear();  //we have to clear the previous data before printing the new one
  114.   display.setCursor(0, 0); //The cursor's unit is in pixels and not in blocks as in the case of the 16x2 LCD
  115.   display.print("U: ");
  116.   display.print(Voltage,3); //we print with 3 decimal places
  117.   display.print("  I: ");
  118.   display.println(Current,3);
  119.   //2nd line
  120.   display.setCursor(0, 1);
  121.   display.print("t: ");
  122.   display.print(elapsedTime/1000);   //time in seconds
  123.   display.print(" s  P: ");
  124.   display.print(Power,1);
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement