Skip to content

Instantly share code, notes, and snippets.

@rogersguedes
Created June 2, 2020 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogersguedes/4e84a322946e9c3a45e8c49b8c4f2d0f to your computer and use it in GitHub Desktop.
Save rogersguedes/4e84a322946e9c3a45e8c49b8c4f2d0f to your computer and use it in GitHub Desktop.
float watt_average = 0;
float measuredvalue = 0;
float measuredvalue1 = 0;
float measuredvalue2 = 0;
float digstepV = 0.0080566; // IS MULTIPLIED BY 10
float voltage = 230; // the net voltage
int tel = 1;
char var;
float longaverage = 0;
float longaverage2 = 0;
int AnalogPin = 20; // = a5 the pin that is being used on the Olimexino-STM32 a0 = 15 a1 = 16 see the latest schematic
// https://www.olimex.com/Products/Duino/STM32/OLIMEXINO-STM32/resources/OLIMEXINO-STM32_Rev_D.pdf
void setup()
{
// Declare the sensorPin as INPUT_ANALOG:
pinMode(AnalogPin, INPUT_ANALOG);
}
void measure_power()
{
int n = 0;
float totalsum = 0;
watt_average = 0;
while(n < 1000) // preform 1000 measurements and look if somebody or something is interested
{
// measure AC current in watts
// The ACS712 chip 20A has a linear curve of 100 mv per ampere between -20 till 20 ampere from 0,5 volt till 4,5 volt by a supply-voltage of 5 volt
// The Olimexino_stm32 works only with 3.3 volts. With a resistor divider (potentiometer between 5V and ground trim the output till 3.3V and you have the correct divider)
// on the output of the ACS712 is fed into the ADC of the Olimexino-stm32, as long there is no current flowing the division is nearly linear so take at least 4,7K as value.
// the Olimexino stm32 has a 12 bit DAC digital analog converter. 2^12 is 4096 the zero-crossing is at 2048
// If the measured value is less than 2048 the relevant voltage = 2048 min the measured value (example 2048 - 490 = 1558)
// If the measured value is more than 2048 the relevant voltage = the measured value min 2048 ( example 3567 - 2048 = 1519)
// The result is only positive values. 3.3 Volts divided by 4096 = 0.00080566 volt per bit step.
// multiplication of the measured value with 0.00080566 gives the voltage of the output from the ACS712 in V every volt = 10 amp
// so if we measure a value of 156 * 0.00080566 = 0.12568296 volt * 1000 = 125.68296 Mv every milivolt = 0.01 amp = 1.2568 amp P = 230 * 1.2568 = 289 Watt
// multiplying by 1000 and then dividing by 100 is the same a one multiplication by 10 so we can change the digstepV constant to 0.0080566
// According to the Law of Ohm P=I*V The voltage of the net in Europe is 230 Volt rms http://www.researchgate.net/post/Is_the_single_phase_230V_that_we_get_Vrms_or_Vpp
// By preforming many measurements in rapid succession(1000)and store every measurement we get a clear picture of the real power that is being used.
// after 1000 measurements we calculate the mean value and look is there is any interest in the value. If not we start a new cycle
// the cycles go so fast that a mean of the mean seams even more appropriate the clock runs a 72MHZ
// Millivolts (mV) Volts (V)
// 0 mV 0 V
// 1 mV 0.001 V
// 10 mV 0.01 V
// 100 mV 0.1 V
// 1000 mV 1 V
//
measuredvalue = analogRead(AnalogPin);
if(measuredvalue < 2048){
measuredvalue = 2048 - measuredvalue;
}
else if(measuredvalue > 2048){
measuredvalue = measuredvalue - 2048;
}
else if( measuredvalue == 2048){
measuredvalue = 0;
}
measuredvalue1 = measuredvalue * digstepV; // multiply the measurement with the constant of the bit-step voltage
measuredvalue2 = measuredvalue1 * voltage; // P=I*V law of Ohm NB 230 is already the rms value of the electricity net
totalsum += measuredvalue2; // sum all the values together so we can calculate the mean value after 1000 measurements
n++; // Add one to the counter
}
watt_average = totalsum / n; // The mean wattage = sum of measurements divided by number of measurements
}
void loop()
{
measure_power();
longaverage += watt_average;
tel++;
if (SerialUSB.available() > 0){
var = SerialUSB.read();
// if there is some request on the serial port just print the result of the last measurement and start a new one.
// if there is no interest start a new measurement as well.
SerialUSB.print(" watt_average ");
SerialUSB.print(watt_average);
SerialUSB.print(" tel = ");
SerialUSB.print(tel);
longaverage2 = longaverage/tel;
SerialUSB.print(" long average = ");
SerialUSB.println(longaverage2);
longaverage = 0;
tel = 1;
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment