Problems with Serial Monitor and Sleep functions


Problem: Garbage coming out of serial monitor when sleep function included in code.

Details of problem:

When the Sleep function is included in the sketch, even before the sleep function is called, I start recieving rubbish through the Serial monitor. If I comment the sleep function out then I get the correct result through the serial monitor.
In my sketch below, I print out variables z, xinc and yinc to the serial monitor before the first sleep function is called. The output I get through the serial monitor can be seen in the attached image “Output from Serial Monitor with Sleep function”.
The output I get through the Serial monitor when I comment out the sleep function is attached in the image “Serial output with no Sleep function”.

A few notes:
i. I’m using an arduino nano
ii. I’m using an ADS1115 16 bit ADC
iii.I’m using an Adafruit SD Card reader
iv. The z, xinc and yinc variables are correctly saved on the SD card even when garbage is coming out of the serial monitor
v. When the sleep function is called, the arduino’s Tx led stays lit up for 8 seconds.
vi. When powering this via battery and having no connection to the computer, the Tx led behaves the same (i.e. stays lit up for 8 seconds).

The code I am using is:

#include <LowPower.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <SPI.h>
#include <SD.h>
int pinCS = 10;

int16_t adcx1;
int16_t adcx2;
int16_t adcy1;
int16_t adcy2;
float xinc;
float yinc;
int z;
File file;
Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
//Adafruit_ADS1015 ads;     /* Use thi for the 12-bit version */

void setup(void) 
pinMode(pinCS, OUTPUT);
  ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 0.1875mV (default)

void loop(void) 
z = z + 1;


void ReadADC(void) {

      adcx1 = ads.readADC_SingleEnded(1);
      adcx2 = ads.readADC_SingleEnded(1);
xinc = float(adcx1)+float(adcx2);
      adcy1 = ads.readADC_SingleEnded(2);
      adcy2 = ads.readADC_SingleEnded(2);
      yinc = float(adcy1)+float(adcy2);

void SDSave(void) {

        if (!SD.begin(pinCS)){
              file ="DATA.csv", FILE_WRITE);file.print(z);file.print(",");file.print(xinc);

void Sleep(void) {
         LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 
         LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 

Does anyone have some insight to share relating to this problem?

Serial.print() returns almost immediately, the bytes are just copied to an internal buffer. If you call Sleep() directly after several Serial.print() calls the data will still be in the buffer when the processor goes to sleep and you don't see any real data on the serial interface. Insert a Serial.flush() before the Sleep() call and you will get normal output.

Excellent, "Serial.flush();" worked perfectly. Thanks.

Just out of curiosity I tried a delay function in place of the Serial.flush() (Now that you've identified the problem area) and that worked also.

Just out of curiosity I tried a delay function in place of the Serial.flush() (Now that you've identified the problem area) and that worked also.

Yes, that works too but Serial.flush() blocks only for as long as it's necessary while delay() might block for much to long, so using much more energy.