Emonlib library with standalone atmega

Hi guys

I am using Emonlib to measure AC voltage using atmega32.

First of all I tried my circuit on Arduino uno board as follows:

1- Preparing a pcb including: - transformer(220/15)

                                          - voltage divider for 15vac(2 resistors:100k&10k)

                                          - voltage divider for 5vdc to maintain 205 dc offset(2 resistors:470k both)

2- this pcb has 220 vac input in addition to 5vdc and ground from the uno board, and the output to be measured is connected to an analog input in the arduino uno.

3- an lcd 16*2 is connected to the board.

when loading the emonlib example (measure voltage and current) to measure voltage, and after calibration, all goes fine, the results are very accurate and stable (Shone on LCD display), the results are very close to 220v.

BUT when loading the same sketch to atmega32 (of course after modifying the arduino ide to deal with it), and when connecting the hardware, the results where very unstable and not accurate, they fluctuate near 100 V.

I though it was a resolution problem but the result of a normal adc was 1024 for a 5 vdc supply.

I am sure it is not a power supply problem since i am powering the atmega from the uno board, and i am using necessary capacitors.

Please Help

 

Robert Wall's picture

Re: Emonlib library with standalone atmega

I suspect that your problem might be the absence of a reference to the ATMega32 in the readVcc( ) method in the file EmonLib.cpp

I would need to look in detail at the data sheet to see what the multiplexer in your processor requires.

An easy test would be, where you have:

   #if defined emonTxV3
	int SupplyVoltage=3300;
   #else 
	int SupplyVoltage = readVcc();
   #endif

remove those lines (twice) and use simply:

	int SupplyVoltage=5000;

always assuming your supply voltage is stable at 5.000 V.

Another possibility is that you are accidentally using the differential input mode of the ADC. Again, that would need careful study of the data sheet to check what is actually happening.

M.Bz's picture

Re: Emonlib library with standalone atmega

Hi Robert

Your post was very helpful and your method worked perfectly.

Thank you so much

Robert Wall's picture

Re: Emonlib library with standalone atmega

That's proved what the problem is. If you can, will you please try restoring

   #if defined emonTxV3
	int SupplyVoltage=3300;
   #else 
	int SupplyVoltage = readVcc();
   #endif

and to the end of the line in EnergyMonitor::readVcc()

#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB1286__)

add

|| defined(__AVR_ATmega32__)

and try that. If it works, I'll get emonLib changed so that everyone can benefit.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.