Pin (10, 11, 12) conflict/collision

I try to use poor atmega 168 mini for recording data from GY521 on SD memory card. The pins 10, 11, 12 are used by USBasp programmer, the same pins are used to communicate with SD memory card. The pin 4 is used by GY521 and SD memory card. There are another free data pins, but how to assign them properly without collision?

The SPI bus (digital pins 10, 11 and 12) is intended to be shared, but some SD card modules don't share properly. In any case, most people would disconnect the programmer before using the MCU with other devices.

Since the MPU-6050 uses the I2C bus (SDA and SCL on pins A4 and A5), there is no conflict with the SD card.

However, the MPU-6050 and the SD card use 3.3V logic and you must use bidirectional logic level shifters with a 5V Arduino. In some cases, these are built in to the MPU-6050 and SD card module. If not, you may destroy them.

Otherwise, run the ATmega168 on 3.3V.

Thank You jremington for clear answer. Of course i disconnect USBasp after programming, but even the simplest "write - hi word" doesn't work on SD card. It is more convenient to have USBasp connected to look for the errors (instead switching USBasp/SDcard. The pin 4 is used for programming and for CS pin SD card. The problem 5V(arduino)/3.3V(SD card) is resolved by 5/3.3 converter.

For more informed help, please follow the directions in the "How to get the best out of this forum" post. Post the code, using code tags, and a wiring diagram (hand drawn is preferred, with all pins, parts and connections clearly labeled).

Shouldn't that be pins 11, 12, and 13, for MOSI, MISO, and SCK?

YES, they should and thy are! However my "test set" with USBasp work well and use 11, 12, 13 for programming atmega 168! After program upload I disconect programmer and freed 11, 12, 13 connect to SD card module via converter 5v/3.3v.
I've noticed that my test code (using blink) work strange. Code:

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

const int chipSelect = 4;

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(200);  
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(200);
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(2000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(2000);
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(200);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(200);
    // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);  
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);  
  }
  else {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(300);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(300);  
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(300);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(300);  
  }
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "zapis testowy arduino -> sd pamięć";

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("zapis_gbk.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(300);  
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(300);  
  }
  // if the file isn't open, pop up an error:
  else {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(300);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);  
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(300);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);  
  }
}

The strange is, that only 200/2000/200 blinking is performed and neither if (!SD.begin(chipSelect)) nor else case is blinking 1000/300 !!!
I can not imagine such a logic...
Can anybody help?

If the SD module is improperly wired, defective or has been destroyed by applying 5V to any logic pins, SD.begin() could be causing a processor reset.

That would lead to running setup() repeatedly and give the blink pattern that you observe.

Thank You for the ideas/tips. My experience is quite poor - setup() isn't running repeatedly - only once. How to omit 4,11,12,13 pins collision? 4 is used by GY521 and SD, 11,12,13 are used by USBasp and SD.

You can change that to any available pin for the SD, example sketches commonly use pin 4 because that is how a lot of shields for the UNO are wired. It can probably be changed for the GY521 also, but without knowing which library you are using its hard to tell how easily that can be done..

Thanks David,
I use standard SD, SPI, GY libraries, but I'm pretty sure there is possible another pins assignment, but...I don't know how/where. I was looking for those pins assignment and found nothing. More valuable would be 11,12,13 pins reassignment, while the USBasp use them and I prefer to leave the programmer for fast change/test/improve the code.

Why? The GY-521 is a I2C device.

It is my sensor. I want to record x,y,z accelerations.

It is time to take a step back and learn how to use the i2C interface to gather x,y,z accelerations from the GY-521. Arduino digital pin 4 has nothing to do with I2C communications.

This is the line that assigns the pin for the chip select on the SD cardD Try rewiring the SD adapter to a different pin that is not currently in use.

I don't really understand why pin 4 would be needed for the GY-521, unless you are attempting to use multiple GY-521 modules and are driving the address line with an output pin to select/deselect which module to use.

I tried to apply such a drawing from:

This project uses 4 and 5 pins od arduino.

You mean it uses A4 and A5, which are the II2C pins ?

In your own picture the module connected to pins A4 and A5, rather than 4 & 5. The pins A4, A5 are not the same as 4 & 5

Thank You! Very much.
The main question is still relevant:
how to record (on SD card?) data from GY521???
(it's my mistake 4/5. I use A4 & A5 indeed.)

You can learn to use the SD card library by reading the docs and studying and running the examples in the library folder.

On the Arduino, open File>Examples>Examples for any board>SD>(pick any example)

Do that after you have learned to collect and print out data from the GY-521, of course

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