[solved] SD card and DC motor clash?

Hi everyone,

I'm currently working on a simple project where I use a DC motor (controlled by a H-Bridge) to rotate samples. I want to add a DHT sensor and a SD card reader to the project to monitor temperature and moisture. I thought that would be simple but since I have added these two, I'm unable to rotate the DC motor!

When I remove everything related to the SD card, the motor and temperature reading work fine. (Which I think also proves the wiring of the motor is not at fault). But, if I leave only the parts relative to the SD card and the motor, the motor won't rotate anymore...

List of used material: Arduino Uno ; 12V DC motor ; DHT22 sensor ; DFRobot MicroSD module; H-Bridge

Here is the simple piece of code I use:

#include <SD.h>

// SD card:
const int SDCARD = 4;
// Motor:
const int MOTOR_SPEED = 10;
const int MOTOR_DIR1 = 11; // direction 1
const int MOTOR_DIR2 = 12; // direction 2

/////////////////////////////////////////////

void setup() {

  Serial.begin(9600);

  pinMode(MOTOR_SPEED, OUTPUT);
  pinMode(MOTOR_DIR2, OUTPUT);
  pinMode(MOTOR_DIR1, OUTPUT);


  // SD card initialization (copy-pasted from some tutorial: https://www.arduino.cc/en/Tutorial/Datalogger):
  while (!Serial) {
    ;
  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(SDCARD)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }

  Serial.println("card initialized.");
}

/////////////////////////////////////////////

void loop() {

  
  digitalWrite(MOTOR_SPEED, HIGH);
  digitalWrite(MOTOR_DIR1, HIGH);
  digitalWrite(MOTOR_DIR2, LOW);
  Serial.println("reading main loop now...");

}

The serial monitor returns the following:

Initializing SD card...card initialized.
reading main loop now...
reading main loop now...
[...]

But the motor does not rotate... If I ever comment the piece of code initializing the SD card, the motor rotates.

I can't understand what's going on. If someone has any idea, it would be much appreciated.
Thanks!

EDIT: Forgot to mention that the SD is connected via the ICSP pins (except for the SS one on digital 4)

SOLUTION: The error was that the ISCP-1 and ISCP-4 (which I used to wire the SD card reader) actually correspond to digital 12 and 11 respectively (which I used to wire the motor).

SD code spend a lot of storage
How much free memory do you have after compile ?

I use Arduino Uno. Here is what the Arduino IDE returns:

Sketch uses 6712 bytes (20%) of program storage space. Maximum is 32256 bytes.
Global variables use 895 bytes (43%) of dynamic memory, leaving 1153 bytes for local variables. Maximum is 2048 bytes.

I just know I could not get the SD action on my Uno
Too small memory
If you attach you total sketch I can try it on my Uno :wink:

I use the better SDFat library on UNO and MEGA repeatedly with no (space) issue.

sidhabo:
I just know I could not get the SD action on my Uno
Too small memory
If you attach you total sketch I can try it on my Uno :wink:

This is really surprising. I've been able to use the SD library on Arduino Uno before, to simply log the output of a DHT sensor.
Meaning I had not only the SD library loaded onto the board, but also the DHT library... Everything worked like a charm.

adwsystems:
I use the better SDFat library on UNO and MEGA repeatedly with no (space) issue.

I see, I'll give a try to this library also

how do you connect the SD card if you use SPI pins for motor? the pins on ICSP header are 11,12,13

Juraj:
how do you connect the SD card if you use SPI pins for motor? the pins on ICSP header are 11,12,13

Give that man a cookie! Thank you, that solved it!

I didn't realize that the ICSP pins were actually connected to digital pins 11, 12 ,13. I even secretly hoped I could save some digital pins by using the ICSP ones...

Thanks again!

thieu:
Give that man a cookie! Thank you, that solved it!

I didn't realize that the ICSP pins were actually connected to digital pins 11, 12 ,13. I even secretly hoped I could save some digital pins by using the ICSP ones...

Thanks again!

if you eventually switch to Mega, the SPI pins are 50, 51 and 52. on ICSP header too