Abend zusammen,
ich will mit meinem PI3 den Strom eines Verbrauchers messen (AC). Dazu habe ich einen MCP3008 genommen.
Der Hall Sensor ACS712 hängt über ein Poti an den CH0 des ADC... (Poti weil der ADC 3,3V max. braucht... der ACS aber max. 5V ausgibt)
wenn ich im Leerlauf den Wert über analogRead (BASE + 0); auslese, bekomme ich genau 512... das ist richtig...
aber wenn ich dann mit der u.a. Funktion arbeite bekomme ich im Leerlauf ca. 10Watt..
weiß leider nicht weshalb... den Code aus irgendeinem Block.. dort scheint der zu funktionieren....
Code
void messen()
{
long lNoSamples=0;
long lCurrentSumSQ = 0;
long lCurrentSum=0;
// sample loop - with inital parameters, we will get approx 800 values in 100ms
unsigned long ulEndMicros = micros()+gulSamplePeriod_us;
while(micros()<ulEndMicros)
{
// start sampling and wait for result
// make sure that we read ADCL 1st
long lValue = analogRead (BASE + 0);
// printf("%d\n", lValue);
lValue -= giADCOffset;
lCurrentSum += lValue;
lCurrentSumSQ += lValue*lValue;
lNoSamples++;
}
if (lNoSamples>0) // if no samples, micros did run over
{
// correct quadradic current sum for offset: Sum((i(t)+o)^2) = Sum(i(t)^2) + 2*o*Sum(i(t)) + o^2*NoSamples
// sum should be zero as we have sampled 5 cycles at 50Hz (or 6 at 60Hz)
float fOffset = (float)lCurrentSum/lNoSamples;
lCurrentSumSQ -= 2*fOffset*lCurrentSum + fOffset*fOffset*lNoSamples;
if (lCurrentSumSQ<0) {lCurrentSumSQ=0;} // avoid NaN due to round-off effects
float fCurrentRMS = sqrtf((float)lCurrentSumSQ/(float)lNoSamples) * gfACS712_Factor * gfLineVoltage / 1024;
printf("%f\n", fCurrentRMS);
// correct offset for next round
giADCOffset=(int)(giADCOffset+fOffset+0.5f);
}
}
Alles anzeigen