collect2: error: ld returned 1 exit status exit status 1?

I am very confused I keep getting this error when I am trying to verify my code is there issues with what I have written? Thanks.


Why make it so difficult for us to help you? I don't want to go to some other site to see your code. I can't verify your code to see the errors.

Read the forum guidelines.

Post the code here.

Post the entire text of any errors.

I am very sorry i am now aware of how to post here is my code:

#include "RTClib.h"         // real time clock
#include <SD.h>            // sd card
#include <Wire.h>

                                                                                         // A simple data logger for the Arduino analog pins
#define LOG_INTERVAL  1800000 // *mills between entries (reduce to take more/faster data) currently set to 30 mins between logs*
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!

#define SYNC_INTERVAL 10000 // mills between calls to flush() - to write data to the card 600000 = 10 minutes
uint32_t syncTime = 0; // time of last sync()
RTC_DS1307 RTC; // define the Real Time Clock object
int n = 1;
int val;    
const int chipSelect = 10;                                                                           // for the data logging shield, we use digital pin 10 for the SD cs line

File logfile;

void error(char *str)
  Serial.print(F("error: "));

            ;Serial.begin(9600);                                                                  // start serial port
  Serial.println(F("FlowRate and WaterHeight"));                                         //F = macro function to save dynamic memory

  // initialize the SD card
  Serial.print(F("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);

  if (!SD.begin(chipSelect)) {                                                                     // see if the card is present and can be initialized:
    error("Card failed, or not present");
  Serial.println(F("card initialized."));

  char filename[] = "LOGGER00.CSV";                                                                // create a new file
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i / 10 + '0';
    filename[7] = i % 10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile =, FILE_WRITE);
      break;  // leave the loop!

  if (! logfile) {
    error("couldnt create file");

  Serial.print(F("Logging to: "));

  // connect to RTC

  if (!RTC.begin()) {
    logfile.println("RTC failed");
    Serial.println(F("RTC failed"));
#endif  //ECHO_TO_SERIAL

  logfile.println("Date / Time ,Water Height ,mm ,reading ,psi,reading ");


void loop(void)

  int trigPin = 13; //Sensor Trip pin connected to Arduino pin 13
  int echoPin = 11; //Sensor Echo pin connected to Arduino pin 11
  float pingTime;  //Time for ping to travel from sensor to target and return
  float targetDistance; //Distance to Target in millimeters
  float flowRate; //Flowrate in lps
  float realDistance; //water height in mm
  float speedOfSound = 755.4; //*Speed of sound in miles per hour when temp is 10 degrees celcius.*

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state

  pingTime = pulseIn(echoPin, HIGH);  //PingTime is presented in microceconds
  pingTime = pingTime / 1000000; //Convert pingTime to seconds by dividing by 1000000 (microseconds in a second).
  pingTime = pingTime / 3600; //Convert pingtime to hours by dividing by 3600 (seconds in an hour).
  targetDistance = speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour.
  targetDistance = targetDistance / 2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance = targetDistance * 1609344; //Convert miles to millimeters by multiplying by 1609344 (millimeters in a mile).

  realDistance = (1000 - targetDistance);// *Ajust value (1000) to be level above base of weir that sensor is mounted - distance recorded = water height.*

  double P; //height of water over bottom of weir (realDistance)
  double Q; // flow rate in lps
  P = (realDistance);

  if (P < 300) Q = 2.49 * pow(P, 2.48); // Formula for v-notch weir used under 300mm.

  if (P > 300) Q = 1.8 * 700 * pow(P, 1.5); // Formula for a rectangular notch weir used over 300mm. *Change (700) value to be width of rectangular weir in mm.*

  flowRate = Q;

  Serial.print("WaterHeight= ");
  Serial.print(" mm  ");
  Serial.print("FlowRate= ");
  Serial.print(" lps ");

  DateTime now;
  now =;                                                                       // fetch the time
  delay((LOG_INTERVAL - 1) - (millis() % LOG_INTERVAL));                                 // delay for the amount of time we want between sd card readings

  int H = now.hour();                                                                   // Get the hours right now and store them in an integer called h
  int M = now.minute();                                                                 // Get the minutes right now and store them in an integer called m
  int S = now.second();                                                                 // Get the seconds right now and store them in an integer called s

  logfile.print(now.year(), DEC);
  logfile.print(now.month(), DEC);
  logfile.print(, DEC);
  logfile.print(" ");
  if (H < 10) {                                                                          // Add a zero, if necessary, as above
  logfile.print(now.hour(), DEC);
  if (M < 10) {                                                                        // Add a zero, if necessary, as above
  logfile.print(now.minute(), DEC);
  if (S < 10) {                                                                       // Add a zero, if necessary, as above
  logfile.print(now.second(), DEC); // logfile.print('"');

  Serial.print(now.year(), DEC);
  Serial.print(now.month(), DEC);
  Serial.print(, DEC);
  Serial.print(" ");
  if (H < 10) {                                                                      // Add a zero, if necessary, as above
  Serial.print(now.hour(), DEC);
  if (M < 10) {                                                                       // Add a zero, if necessary, as above
  Serial.print(now.minute(), DEC);
  if (S < 10) { // Add a zero, if necessary, as above
  Serial.print(now.second(), DEC);


  logfile.print(", ");
  logfile.print(" WaterHeight= ");
  logfile.print(" mm ");
  logfile.print(" FlowRate= ");

  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();



and here is the error message:

/tmp/ccACvSl1.ltrans1.ltrans.o: In function `main':

/home/builder/.arduino15/packages/arduino/hardware/avr/1.6.23/cores/arduino/main.cpp:43: undefined reference to `setup'

collect2: error: ld returned 1 exit status

exit status 1

Every Arduino sketch must have a setup and a loop function. Even if you don't need to put in any code in the function, you still need to add an empty function definition. Your sketch does not have a setup function, thus the error.

Where would this go in my code? Sorry, I don't have a grasp of C++ yet.

The setup() function can go anywhere in the code as long as it is outside of any other function (like loop() or your error() function). The setup() function is usually put just before loop().

Like so:

 logfile.println("Date / Time ,Water Height ,mm ,reading ,psi,reading ");

void setup()

void loop(void)

This code really belongs in setup() as it needs to be executed only one time as startup:

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

Along with the SD card and RTC initializations.

Thank you, everyone, for your help I now have it all up and running.

You're welcome. I'm glad to hear it's working now. Enjoy!