emonTx with PLL firmware and rfm not working

Hi, I just read the excellent post from Martin Robert about using PLL. I tried the firmware but unfortunately sending data via rfm is only working if I remove the line
ADCSRA &= 0xf8; // remove bits set by Arduino library
from the sketch, which breaks PLL.

Does anyone know what’s the problem here and how to fix it?


That’s pretty odd. Not that it breaks the PLL but that your rfm doesn’t work without removing it.

If you remove the statement you are not clearing the ADC control register, so the next statement adds the 0x06 to whatever the default is. This register is used to control the speed of the AD converter, so it’s no wonder the PLL breaks.

Are you running the code on a standard emonTX or some other hardware? And have you made any other modifications? I’m asking this because I and I’m sure many others haven’t had any issues with Martin’s code either in it’s standard form or modified to do additional things, in my case to read more temperatures.


What is your supply frequency?

I’ve not studied that particular branch (by PetriK), I’ve not heard of anyone else using it but equally I’ve not heard of any other report of problems with it.

Which RFM module are you trying to use? The RFM69CW will not work with that code, which is for the RFM12B only - I have low-level equivalent code for the newer module.

Hi Simon and Robert,

thank you for your fast reply!
I tried the original sketch from Martin Robert and it is indeed working without a problem.
The only modification I did in both sketches was to exchange rfm_write(0x80D7) with rfm_write(0x80E7) as I’m using 868 MHz with the RFM12b. The hardware is not an original emontx one, as I rebuild it to understand how it works.

I also noticed that I get slightly different frequency values with both sketches, the one from the modified version by PetriK are a bit lower which seems not so accurate as the one from Martin.

Do you have an idea what might be wrong? I’d love to have a three phase version for my home in germany :slight_smile:

Just to be clear - you have rfm_write(0x80E7) in Martin’s sketch and it works correctly; you have rfm_write(0x80E7) in Petrik’s 3-phase sketch and it works except that it does not transmit.

Is that correct?

[Edit] I have not had time to do a complete test, but PetriK’s sketch does not transmit for me on any frequency.

Hi Robert,

thank you for testing that! Yes that’s exactly how it is.
PetriK’s sketch transmit for me also only if I remove ADCSRA &= 0xf8; from his sketch.

I seem to remember that PetriK did struggle to get his version to work, so I’ll need to look carefully at his version and compare it, and hope that I can see something that is stopping it from working.

When you say you “rebuilt” the emonTx, what exactly do you have now? It will help me if I know how you connected the inputs, if you are reading temperature, and how you connected the radio.

The different frequency values might be because PetriK appears to have included a correction factor - the frequency of his crystal (or resonator) may have been slightly wrong. With that set to 1, you must get the same values, or the loop is not locked.

Are you using if only for measurement, or are you using the energy diverter also?

Ah good point with the correction factor for the frequency. That looks much better now :slight_smile:
I’m using it only for measurement and no temperature measurement.

The radio is connected as follows:
SCK → D13
SDO → D12
SDI → D11
IRQ → D2
SEL → D10

I cannot see anything obviously wrong with PetriK’s sketch - which is worrying. I’ve got a version that was a “work-in-progress” at the end of last year, when I had to stop work on it. I’ll send you a PM shortly.

Does someone have correct values for #define I1LEAD ? I tried a lot but the best power factor I got with a resistive load was 0,9915. With emonLib and phase cal set to 1.7 I get a power factor of 1. Or am I on the wrong path?

I think you’re being too fussy. Unless you have loads that are way down in the power factor list (i.e. almost purely reactive) the difference between 0.9917 and 1.00 will make no real difference - and remember the phase errors, which are a sizeable proportion what we’re correcting, change with voltage and current anyway.

That represents a 7.4° error, so with a power factor of 0.5, which is quite poor, you’ll actually read a power factor of 0.6 or 0.38, and it’s only when the p.f gets down below 0.13 that you’re at risk of seeing load as generation (or vice versa).

In real terms, it means there’s a discrepancy of 8.3 when measuring a 1 kW / 1 kVA load, or 0.83%.

Ok that makes sense, thank you for that good explanation!