Advertisement
Yilltronics

Voltimetro AC V2

Apr 9th, 2022
1,863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ==========================================================
  2.     This sketch(code) was programmed by THE ELECTRO MENTOR
  3.     and was downloaded from theelectromentor.com
  4.     Kindly visit the site for more information and other
  5.     exciting Arduino projects.
  6.     Mail Us : info.arduinoguy@gmail.com
  7.     Please subcribe to my Youtube Channel:
  8.     https://www.youtube.com/channel/UCYBJ0K-M3fl5DBPCb5aJWDQ
  9.     for more electronics and Arduino Projects. Thank You.
  10.     ==========================================================
  11. */
  12.  
  13. #include<math.h>
  14. #include <Wire.h>
  15. #include <LiquidCrystal_I2C.h>
  16. #define I2C_ADDR    0x27
  17. LiquidCrystal_I2C lcd(I2C_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE );
  18.  
  19. int decimalPrecision = 2;           // decimal places for all values shown in LED Display & Serial Monitor
  20. int VoltageAnalogInputPin = A0;     // Which pin to measure voltage Value (Pin A0 is reserved for button function)
  21. float voltageSampleRead  = 0;       /* to read the value of a sample in analog including voltageOffset1 */
  22. float voltageLastSample  = 0;       /* to count time for each sample. Technically 1 milli second 1 sample is taken */
  23. float voltageSampleSum   = 0;       /* accumulation of sample readings */
  24. float voltageSampleCount = 0;       /* to count number of sample. */
  25. float voltageMean ;                 /* to calculate the average value from all samples, in analog values*/
  26. float RMSVoltageMean ;              /* square roof of voltageMean without offset value, in analog value*/
  27. float adjustRMSVoltageMean;
  28. float FinalRMSVoltage;              /* final voltage value with offset value*/
  29.  
  30.  
  31. /* 1.1- AC Voltage Offset */
  32.  
  33. float voltageOffset1 = 0.00 ;  // to Offset deviation and accuracy. Offset any fake current when no current operates.
  34. // Offset will automatically callibrate when SELECT Button on the LCD Display Shield is pressed.
  35. // If you do not have LCD Display Shield, look into serial monitor to add or minus the value manually and key in here.
  36. // 26 means add 26 to all analog value measured.
  37. float voltageOffset2 = 0.00;   // too offset value due to calculation error from squared and square root
  38.  
  39.  
  40.  
  41. void setup() {
  42.  
  43.   Serial.begin(9600);  /* In order to see value in serial monitor */
  44.   lcd.begin(16, 4);
  45.   //  pinMode(pin, INPUT);
  46. }
  47.  
  48. void loop(){
  49.   /* 1- AC Voltage Measurement */
  50.   if (micros() >= voltageLastSample + 1000 )                                                                     /* every 0.2 milli second taking 1 reading */
  51.   {
  52.     voltageSampleRead = (analogRead(VoltageAnalogInputPin) - 512) + voltageOffset1;                           /* read the sample value including offset value*/
  53.     voltageSampleSum = voltageSampleSum + sq(voltageSampleRead) ;                                             /* accumulate total analog values for each sample readings*/
  54.  
  55.     voltageSampleCount = voltageSampleCount + 1;                                                              /* to move on to the next following count */
  56.     voltageLastSample = micros() ;                                                                            /* to reset the time again so that next cycle can start again*/
  57.   }
  58.  
  59.   if (voltageSampleCount == 1000)                                                                               /* after 4000 count or 800 milli seconds (0.8 second), do the calculation and display value*/
  60.   {
  61.     voltageMean = voltageSampleSum / voltageSampleCount;                                                      /* calculate average value of all sample readings taken*/
  62.     RMSVoltageMean = (sqrt(voltageMean)) * 1.5;                                                               // The value X 1.5 means the ratio towards the module amplification.
  63.     adjustRMSVoltageMean = RMSVoltageMean + voltageOffset2;                                                   /* square root of the average value including offset value */                                                                                                                                                       /* square root of the average value*/
  64.     FinalRMSVoltage = RMSVoltageMean + voltageOffset2;                                                        /* this is the final RMS voltage*/
  65.     if (FinalRMSVoltage <= 2.5)                                                                               /* to eliminate any possible ghost value*/
  66.     {
  67.       FinalRMSVoltage = 0;
  68.     }
  69.     Serial.print (FinalRMSVoltage, decimalPrecision);
  70.     Serial.print( "\n" );
  71.     lcd.clear();
  72.     lcd.setCursor ( 0, 0 );
  73.     lcd.print("RMS");
  74.     lcd.setCursor ( 0, 1);
  75.     lcd.print("Voltaje  ");
  76.     lcd.setCursor ( 0, 2);
  77.     lcd.print(FinalRMSVoltage, decimalPrecision);
  78.     lcd.print("V");
  79.    
  80.     voltageSampleSum = 0;                                                                                     /* to reset accumulate sample values for the next cycle */
  81.     voltageSampleCount = 0;                                                                                   /* to reset number of sample for the next cycle */
  82.   }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement