SD card initialisation stops motor from working.

Why is it that the following code stops the motor from working, specifically the first line (see full code further down) ...

if (!SD.begin()) {
  Serial.println("initialisation failed!");
  while (true);                     // Stay here.
}

... but I still get printouts of the following every second in the COM3 window?

12:40:16.931 -> Motor running forwards ...
12:40:17.955 -> Motor running backwards ...
12:40:18.982 -> Motor running forwards ...
12:40:19.970 -> Motor running backwards ...

I am using an Adafruit Motor Shield (HW-130), an Arduino Uno clone, and a Micro SD TF Card Memory Shield Module.

The SD card initialises fine, and "initialisation failed!" is never displayed. But this line of code just stops the motor from working at all. As soon as I remove it, the code works fine!

#include <SPI.h>
#include <SD.h>
#include <AFMotor.h> 

AF_DCMotor motor(1, MOTOR12_64KHZ);   // Define motor on channel 1 with 64KHz PWM.

void setup() {
  motor.setSpeed(255);
  Serial.begin(9600);                 // Open serial communications and wait for port to open.
  
  while (!Serial) {
    ;                                 // Wait for serial port to connect. Needed for native USB port only.
  }

  // Initialise SD card.
  Serial.print("Initialising SD card... ");
  
  if (!SD.begin()) {
    Serial.println("initialisation failed!");
    while (true);                     // Stay here.
  }
  
  Serial.println("initialisation succeeded!"); 
}

void loop() {
        motor.run(FORWARD);
        Serial.println("Motor running forwards ...");
        delay(1000); 
        motor.run(BACKWARD); 
        Serial.println("Motor running backwards ...");
        delay(1000); 
}

The SD card on the SPI interface uses pins 10, 11, 12, and 13. The ancient Adafruit Motor Shield V1 (Adafruit Motor/Stepper/Servo Shield for Arduino kit [v1.2] : ID 81 : $19.50 : Adafruit Industries, Unique & fun DIY electronics and kits) connects to pins 10, 11, and 12 so some uses to the motor shield conflict with the SD card. You could switch to an Adafruit Motor Shield V2: Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit [v2.3] : ID 1438 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits

johnwasser:
The SD card on the SPI interface uses pins 10, 11, 12, and 13. The ancient Adafruit Motor Shield V1 (Adafruit Motor/Stepper/Servo Shield for Arduino kit [v1.2] : ID 81 : $19.50 : Adafruit Industries, Unique & fun DIY electronics and kits) connects to pins 10, 11, and 12 so some uses to the motor shield conflict with the SD card. You could switch to an Adafruit Motor Shield V2: Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit [v2.3] : ID 1438 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits

Excellent, thank you, exactly the answer I was looking for :slight_smile: I checked out the V2 motor sheild details, but it didn't mention which pins it uses, only "two data pins SDA and SCL". Which pins are those? And will it definitely solve this particular issue?

jimixter:
Excellent, thank you, exactly the answer I was looking for :slight_smile: I checked out the V2 motor sheild details, but it didn't mention which pins it uses, only "two data pins SDA and SCL". Which pins are those? And will it definitely solve this particular issue?

On an Uno, SDA and SCL are the same as A4 and A5 (analog pins). The version 2 motor shield communicates with the board over the two wire interface (TWI) which is the wire library. It receives commands from your board and does all the PWM on the shield so you don't need to use any other pins or resources of your board. That is one of the main reasons they came out with V2.

Ok thank you. Could I please ask if it sounds like my V1 motor shield could be causing the problems in my other post below too? Sounds like I need to get a V2 board on order...

https://forum.arduino.cc/index.php?topic=734112.new#new

jimixter:
Could I please ask if it sounds like my V1 motor shield could be causing the problems in my other post below too?

Yes. Any time you are using Motor1 and an SD card or other SPI device you will have problems.

Ok, really appreciate your help, thanks!

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