Arduino sketch only running on upload

Hi, I am a total noob with arduino. I have a TinyZero board with a microSD and GPS receiver shield, and I am trying to run a sketch to log GPS data to the card every second. However, the sketch only runs when I upload it from the Arduino editor.
I know the sketch works, because I can see the LED indicator flashing and I am able to access the GPS data after removing the SD card.
I have done quite a bit of searching and from what I understand, the setup() loop should be running each time the board is reset or powered on, yet this does not seem to be the case. I would greatly appreciate any help or advice!

#include "SoftwareSerialZero.h"
#include <SD.h>


//pins used by the GPS module
static const int GPS_ONOFFPin = A3;
static const int GPS_SYSONPin = A2;
static const int GPS_RXPin = A1;
static const int GPS_TXPin = A0;
static const int GPSBaud = 9600;
static const int chipSelect = 10;

//GPS signal is connected using a software serial port
SoftwareSerial Gps_serial(GPS_RXPin, GPS_TXPin);


void setup()
{ 
     
  // Init the GPS Module to wake mode
  pinMode(GPS_SYSONPin, INPUT);
  pinMode(GPS_ONOFFPin, OUTPUT);
  digitalWrite( GPS_ONOFFPin, LOW );   
  delay(5); 
  if( digitalRead( GPS_SYSONPin ) == LOW )
  {
     // Need to wake the module
    digitalWrite( GPS_ONOFFPin, HIGH ); 
    delay(5); 
    digitalWrite( GPS_ONOFFPin, LOW );      
  } 
     
  
  // Open the debug serial port at 9600
  Serial.begin(9600);
 
  // Open the GPS serial port  
  Gps_serial.begin(GPSBaud);  
   
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");   
}


int inByte = 0;
byte pbyGpsBuffer[100];
int byBufferIndex = 0;

void loop()
{
  byte byDataByte;
  
  if (Gps_serial.available())
  {
     byDataByte = Gps_serial.read();
    
     Serial.write(byDataByte);
     pbyGpsBuffer[ byBufferIndex++ ] = byDataByte;
     
     if( byBufferIndex >= 100 )
     {
       byBufferIndex = 0;       
       File dataFile = SD.open("gps.txt", FILE_WRITE);
    
       // if the file is available, write to it:
       if (dataFile) {
        dataFile.write(pbyGpsBuffer, 100);
        dataFile.close();
      }  
      // if the file isn't open, pop up an error:
      else {
        Serial.println("error opening gps.txt");
      }        
     }      
  }
}

What is your evidence that the sketch is not running ?

Please post an example of the output produced by your program - copy it from the Serial Monitor.

...R

UKHeliBob:
What is your evidence that the sketch is not running ?

When I upload the sketch, I know that it runs until I power off the board--the LED flashes constantly while running, and I am able to view the GPS data saved to the card after powering it off.
Neither of these things are true when I power it off and on again. I have tried adding a simple blink statement just to see if the loop is executing at all, but that didn't seem to work either.

Robin2:
Please post an example of the output produced by your program - copy it from the Serial Monitor.

...R

The program isn't supposed to output to the Serial Monitor, it logs GPS data in NMEA format to a .txt file on the microSD card. I can provide that if necessary.

The program isn't supposed to output to the Serial Monitor,

Yes but it could output messages, are you saying that nothing is produced?

That's a good point. I just re-uploaded the sketch with the Serial Monitor open, but I don't see anything showing up at all. Here's a screeenshot:

If you don't see the message "Initializing SD card..." then something is terribly wrong. You should also see either "Card failed, or not present" or "card initialized.".

I'm a little confused about the need for a "SoftwareSerialZero.h". Since the Tiny Zero shares a processor with the Arduino Zero, can't it use SoftwareSerial.h from the Arduino libraries?

johnwasser:
If you don't see the message "Initializing SD card..." then something is terribly wrong. You should also see either "Card failed, or not present" or "card initialized.".

I'm a little confused about the need for a "SoftwareSerialZero.h". Since the Tiny Zero shares a processor with the Arduino Zero, can't it use SoftwareSerial.h from the Arduino libraries?

Interesting, it definitely runs as I can view the GPS logs both in the txt file and as a track after importing into Google Earth. I don't know why it wouldn't send the message...

As for the SoftwareSerialZero library, I'm using that one because the standard library doesn't support the clock rate of the TinyZero (48MHz). When I tried to use the SoftwareSerial library, it was unable to compile.

altitudelost:
The program isn't supposed to output to the Serial Monitor,

It has lots of Serial.print() statements - presumably they display useful info on the Serial Monitor for debugging purposes.

...R

Robin2:
It has lots of Serial.print() statements - presumably they display useful info on the Serial Monitor for debugging purposes.

...R

That makes sense. Any idea why the Serial Monitor remains blank even when the program is uploaded and run?

altitudelost:
Any idea why the Serial Monitor remains blank even when the program is uploaded and run?

From the product information page https://learn.tinycircuits.com/Processors/TinyZero_Setup_Tutorial/:-

To send text to the Arduino IDE Serial Monitor, use the SerialUSB object instead of Serial
If you were able to upload a sketch to the TinyZero and now it does not respond, you may need to force this into bootloader mode. To do this, power off the TinyZero using the slide switch. Plug the USB cable into the TinyZero and your computer. Then press and hold the button on the bottom on the TinyZero while sliding the switch to the ON position. Then try uploading your program to the TinyZero and it should work. You may need to try this several times if it does not work the first time.
The hardware between the TinyZero and TinyScreen+ share some similarities, so you may see some things labeled 'TinyScreen' which are actually for the TinyZero.

So that means you will not be able to see anything on the serial monitor with the code you have.

Go to this page:- GPS Tracker and Data Logger - TinyCircuits Docs and down load the GPS Tracker Arduino Sketch. You will see code in that how to talk to the serial monitor.

This includes the lines at the start:-

#define SerialMonitorInterface SerialUSB

In the setup function you should have

SerialMonitorInterface.begin(115200);
  if(!SerialMonitorInterface) { delay(5000);}; // Pauses 5 seconds if Serial Monitor is not opened

And to print to the monitor use:-

SerialMonitorInterface.println("Your message here");

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

  • What GPS?
  • How are you powering the GPS?
  • What SD Card module?
  • How are you powering the SD card Module?

When you say you power OFF the ON, how long between OFF then ON?
If you leave it OFF for 30seconds then ON, what happens?

Tom... :slight_smile:

TomGeorge:
Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

  • What GPS?
  • How are you powering the GPS?
  • What SD Card module?
  • How are you powering the SD card Module?

When you say you power OFF the ON, how long between OFF then ON?
If you leave it OFF for 30seconds then ON, what happens?

Tom... :slight_smile:

Hello, I'm just using the standard TinyZero board board coupled with their GPS receiver and microSD adapter shields. The setup is powered by a 150mAh battery. Here's a picture:

When I power cycle it, I wait about 10 seconds. I just tried waiting 30 seconds before powering on again, but nothing happened.

Grumpy_Mike:
From the product information page https://learn.tinycircuits.com/Processors/TinyZero_Setup_Tutorial/:-
So that means you will not be able to see anything on the serial monitor with the code you have.

Go to this page:- GPS Tracker and Data Logger - TinyCircuits Docs and down load the GPS Tracker Arduino Sketch. You will see code in that how to talk to the serial monitor.

This includes the lines at the start:-

#define SerialMonitorInterface SerialUSB

In the setup function you should have

SerialMonitorInterface.begin(115200);

if(!SerialMonitorInterface) { delay(5000);}; // Pauses 5 seconds if Serial Monitor is not opened



And to print to the monitor use:-


SerialMonitorInterface.println("Your message here");

Thanks, I'll take a look at this when I have time later.

Thanks, I'll take a look at this when I have time later.

Well I would have thought it was the first thing you should do because it will tell you how far the sketch is running, and if you are having trouble with the SD card when you try and run it after a power cycle.
This is the crux of understanding your problem.

Figured it out with some help from my Dad. Just needed to add a 1sec delay at the beginning of the setup loop. Also, apparently the TinyZero does use SerialUSB and not Serial to print to the Serial Monitor. Thanks for the help guys.

altitudelost:
the setup loop.

The what? :wink:

Just needed to add a 1sec delay at the beginning of the setup loop.

  1. it is not a setup loop, it is a setup function.
  2. A fixed delay will not always work you need:-
Serial.begin(9600);
  while (!Serial) delay(100);

This ensures that you don't go any further until the USB device enumerates. ( starts up and registers with what ever it is plugged in to )

Yeah my bad. I meant to say setup function, I'm aware of the difference between the terms.

Grumpy_Mike:
2) A fixed delay will not always work you need:-

Serial.begin(9600);

while (!Serial) delay(100);



This ensures that you don't go any further until the USB device enumerates. ( starts up and registers with what ever it is plugged in to )

Thanks for the advice, I'll edit it next time I work on it. Could you explain why this delay is necessary, and why it needs to be dynamic? I discovered it accidentally while trying to blink the LED as a form of debugging. Still trying to learn all this stuff.

Could you explain why this delay is necessary,

It gives the processor time to process the connection instead of constantly asking “are we there yet”. Something you will understand if you have kids.

It has been found by many people to be needed on some processors but not all.

and why it needs to be dynamic?

Not sure what you mean by that. Rather than a long delay this finishes as soon as possible once the connection has been made, thus getting on with it.