Go Down

Topic: Issue with sending SD card data over I2C bus in Slave mode (Read 602 times) previous topic - next topic


My project uses Arduino mini pro (Atmega328p) , Esp8266 and an SD card. Now SD card is connected to the Arudino through SPI (basically it's hardwired) and what I wanted to do is send the data logged in the SD to the Esp8266 through I2C so Arduino is in slave mode and Esp is in Master mode. When I initialize and open my "test.txt" in the "void setup()" function, it works fine but when I open my "test.txt" inside my "requestEvent() " function it gets stuck. Why I wanted to open my"data.txt" inside " requestEvent() " is because I wanted to send data only when there is an interrupt and read the data of sd data simultaneously and send the data through I2C.

Here's my code for ESP Master:

Code: [Select]

#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output

void loop() {
  Wire.requestFrom(8,2);    //2 request bytes from slave device #8
  while (Wire.available()) { // slave may send less than requested
  String c = Wire.readString(); // receive a byte as character
    Serial.println(c);         // print the character


My code for Arduino Slave with SD card :

Code: [Select]

#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#define SS 10
char f;
File myFile;

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
  if (!SD.begin(SS)) {
    Serial.println("initialization failed!");

void loop(){

void requestEvent() {

myFile = SD.open("test.txt");
  if (myFile) {

    while (myFile.available()) {
      f = myFile.read();

It gets stuck before "while(myFile.available)"   (stuck as in if use Serial.print() to check where the code is getting stuck it stops at that line and doesn't execute the lines following it) and also the Esp8266 stops sending Request events.


You realize that requestEvent() is an interrupt service routine, right? You realize that interrupts are disabled while your interrupt service routine is running, right?

Do you suppose that, just maybe, SPI uses interrupts?
The art of getting good answers lies in asking good questions.


Spi could be used in interrupt mode because what I've read is it could be used in either polling mode or interrupt mode. Well, how do I find out for sure what it's using? Also, I knew that there was something wrong in the requestEvent function because it works fine if the sd card is initialized in void setup but even if I write a single line which opens the file "myFile=SD.open("test.txt")" in my ISR I wasn't able to receive the data at the master side (Esp8266), the strange part is that this time it doesn't get stuck, it gets executed sends value but at the master end it shows some error symbol like "؟؟؟؟" (from which we can say that the interrupt is not being disabled)

Go Up