Arduino no response using SD card and servo motor

Hello, I am creating a program that records altitude and temperature on an SD card using BMP180, SD module, and servo motor, and turns the servo motor when the rocket differs from the maximum altitude by 20m. However, when I run the code, there is no response in the serial monitor and there is no recorded information when I check the SD card. What's the problem?

#include <Servo.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <time.h>
#include <Adafruit_BMP085.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <DS1307RTC.h>

Adafruit_BMP085 bmp;
static float altitudemax = 0;
static float altitude = bmp.readAltitude();

File myFile;
Servo servo;

void setup() {
  Serial.begin(9600);
  setSyncProvider(RTC.get);
  setTime(10, 42, 0, 24, 12, 26);
  servo.attach(7);
  Serial.print("Initializing SD card...");
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {}
  }

  if (!SD.begin(4)) {                          
   // Initialize the SD card module.
    Serial.println("initialization failed!");  //If initialization of the SD card module fails, an error is output.
    while (1);
  }
  Serial.println("initialization done.");
  while (1) {
    // Open the file and prepare it for writing. You can only open one file at a time.
    myFile = SD.open("PRESSURE.txt", FILE_WRITE);  // If there is a second argument, it is in write mode.

    if (myFile) {
      // If the file opens normally, write (add) text to the file.

      Serial.print("Writing to test.txt...");
      myFile.print(year());
      myFile.print("year");
      myFile.print(month());
      myFile.print("month");
      myFile.print(day());
      myFile.print("day");
      myFile.print(hour());
      myFile.print("hour");
      myFile.print(minute());
      myFile.print("minute");
      myFile.print(second());
      myFile.println("second");
      myFile.print("Temperature = ");
      myFile.print(bmp.readTemperature());
      myFile.println(" *C");
      myFile.print("Pressure = ");
      myFile.print(bmp.readPressure());
      myFile.println(" Pa");
      myFile.print("Altitude = ");
      myFile.print(bmp.readAltitude() + 87.5);
      myFile.println(" meters");
      myFile.println();
      myFile.close();
      if (altitude > altitudemax) {
        altitudemax = altitude;
      }
      if (altitudemax - altitude > 20) {
        servo.write(180);
      }
      delay(500);


    } else {
      // If the file cannot be opened, an error is output.
      Serial.println("error opening pressure.txt");
    }
  }
}

void loop() {


}

You don't see anything at all? Do you have the right port selected and the right baud rate?

Show us how you have things connected. Are you sure you have a good connection from the board to the PC?

Why? Why not use the loop function? Especially when you're wanting other people to read your code and help.

Welcome to the forum

Do you see any output at all in the Serial monitor such as "Initializing SD card..." ?

Arduino and PC were well connected and the correct port was connected. And the result would be the same even if the code was inside a loop function.

No, I couldn't see any messages.

Or don't. Best of luck to you.


Here is the circuit diagram.

Your Arduino is NOT a power supply. You can't power the servo off the 5V pin like that. That may be browning out the Arduino and resetting it.

Then what should I do?

Use an external supply for the servos.

Don't ever be afraid to check the documentation. There's tons to be learned if you just look.

The Arduino tutorial linked above suggests to use a 9V battery, which will not work to power a servo. A much better idea is to use a 4xAA battery pack, which also obeys the power supply voltage restrictions for typical servos.

1 Like

I connected external power but it still doesn't work. However, the Arduino's 'L' LED blinks periodically.

You never see that printed to the screen?

Show how you've got it connected now. Maybe post some pictures of the setup. You must have something wrong but I don't know how to tell without seeing it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.