Simple analogead and LowPower. not working...

Hi all,

I am an Arduino beginner ... :confused:
I have an UNO compatible board and loaded the example sketch which reads the analog A0 and displays the voltage.
This works fine.
I added a delay(2000) at the end of the loop to see what happens. Works OK as well.

I then included the LowPower library and replaced the delay with :
LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF);

Now, it displays only garbage approximatively every 2s.

Shouldn't this work and introduce a sort of 2s delay ? Is it a wakeup time problem ?
I tried to add a delay after the sleep, but that didn't help. Only the TX LED is not permanently ON.

Thanks for any help.

I bet you probably have to turn the ADC back on.

Thanks Isaac

But is this not the case when we do a analogRead(A0) ??

Just to be sure, i tried on another board i received today with same result... Both boards have the MEGA328P.

I edited this later... I found out that the problem is the Serial.println() function that is used to display the result... When i add a delay(200) after the Serial.println, the voltage value is well displayed on the screen.
So i suppose that the chip goes to sleep before all the datas have been sent to the serial port..

Adding a delay() is not a good idea as it sucks power for nothing !

I increased the serial speed, but this is quite unstable as well..

Any solution ?

Cheers,

Setting a higher Baudrate should work fine. I use 115200 most of the times. Also, this is the speed of the Arduino Uno bootloader (and that seems to work fine).

Try to add while(!Serial)

So

Serial.println("I just put a lot of text here.");
while(!Serial){
}
gotosleepFunction()

But yeay, you have to wait until serial is done talking before going to sleep. But sucking power... What are you building!?

Hi Septillion,

The if(!serial) didn't help... >:(

I still have to add a small amount of delay after the println and before the gotosleep... Increasing to baudrate to 115200bds helps to reduce this delay, but it is still needed.

I'm starting a project that will need a AA battery supply and a lifetime of several months. So i build my project from start with low power in mind.

Thanks,

Yes, the serial port takes some time to send a message.

Yes, it takes some time, but how to deal with this ?

The while (!serial) trick doesn't make the job...

Ok, i can introduce a small delay, but that's not the best way to handle the problem !

Thanks,

while (!Serial) makes no sense at all. That simply waits until the Serial object exists, which is pretty much always will, so it's effectively nothing more than a VERY small delay. What you need to do is Serial.Flush(), which will not return until the transmit buffer is empty.

Regards,
Ray L.

Ah, of course, should have known that. Was confused about the line "Indicates if the specified Serial port is ready.". Indeed, while( Serial.flush() ) is the way to go. But I think the name flush is very, very confusing! Judging by the name I would have guessed it flushes the buffer (like it did in old Arduino, but did not know).

So to make it go to sleep fast, use a high baudrate and use while( Serial.flush() ) to check when it's save to go to sleep.