Notifications
Clear all

i2c AND spi do not work together

29 Posts
3 Users
0 Likes
218 Views
henrik_t7
(@henrik_t7)
Member
Joined: 3 years ago
Posts: 21
Topic starter  

I have a sensor DS18B where I read temperature both in c and f on an OLED with I2c and store the temperature on an SD card with SPI, but I2c and SPI do not work together, how do you solve this?

On a Arduino Uno R4 wifi

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define ONE_WIRE_BUS 2 // DS18B20 pin
#define OLED_RESET    -1 // Reset pin for OLED (not used)
#define SCREEN_WIDTH  128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32  // OLED display height, in pixels

#define OLED_ADDRESS 0x3C // OLED I2C address, default for 128x32 OLED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

File dataFile;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    delay(10); // Wait for serial port to connect
  }

  // Initialize the OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }

  display.display(); // Display initialization

  // Initialize the DS18B20 sensor
  sensors.begin();

  // Initialize the SD card
  if (!SD.begin(4)) { // CS pin for SD card
    Serial.println(F("SD initialization failed!"));
    return;
  }

  Serial.println(F("Initialization complete"));
}

void loop() {
  sensors.requestTemperatures();
  float celsius = sensors.getTempCByIndex(0);
  float fahrenheit = celsius * 9.0 / 5.0 + 32.0;

  // Print temperature to serial monitor
  Serial.print(F("Temperature (C): "));
  Serial.print(celsius);
  Serial.print(F(" | Temperature (F): "));
  Serial.println(fahrenheit);

  // Print temperature to OLED display
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print(F("Temp (C): "));
  display.println(celsius);
  display.print(F("Temp (F): "));
  display.println(fahrenheit);
  display.display();

  // Store temperature on SD card
  dataFile = SD.open("temperature.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.print(F("Temperature (C): "));
    dataFile.print(celsius);
    dataFile.print(F(" | Temperature (F): "));
    dataFile.println(fahrenheit);
    dataFile.close();
    Serial.println(F("Data written to SD card"));
  } else {
    Serial.println(F("Error opening file"));
  }

  delay(5000); // Delay for 5 seconds
}


   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6827
 

@henrik_t7 We need a lot more info. What exactly is happening or not happening?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6827
 

@henrik_t7 I do see one thing but don't know if it's the problem. You are opening and closing the SD card every pass through the loop. Unless you have a high wear card, that will destroy the card quickly. 

It's a tricky problem with continuous loop applications. Using an SD card as storage is also problematic. If you want reliability, I would use a USB based solution and maybe a timer to periodically close a time stamped file and open a new file. You might do that once per hour, or once per 5, 10, 15 minutes, your choice.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@scsiraidguru)
Member
Joined: 2 weeks ago
Posts: 22
 

I had 7 Arduino UNO devices, 2 Arduino Mega devices and a Raspberry Pi communicating on I2C with 4 I2C LCD 20x4 screens.  I setup the master to loop communication on the addresses for each slave.   I was able to send and receive information from the slaves to the master and display it on the I2C LCD screens.  

image

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6827
 

@scsiraidguru And?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@scsiraidguru)
Member
Joined: 2 weeks ago
Posts: 22
 

Did you try to get I2C working first and making sure SPI was not conflicting with it.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6827
 

Posted by: @scsiraidguru

I had 7 Arduino UNO devices, 2 Arduino Mega devices and a Raspberry Pi communicating on I2C with 4 I2C LCD 20x4 screens.  I setup the master to loop communication on the addresses for each slave.   I was able to send and receive information from the slaves to the master and display it on the I2C LCD screens.  

-- attachment is not available --

What does all that stuff do?

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6827
 

@henrik_t7 I have attached a screen grab of the Arduino UNO R4 WiFi specs. I see no evidence they designed the board to not be able to use all bus protocols at the same time. If you are seeing failures, I would look at the wiring and code.

Screenshot 2024 04 01 at 20.37.51

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
henrik_t7
(@henrik_t7)
Member
Joined: 3 years ago
Posts: 21
Topic starter  

Thanks for all the replies, it seems that the sensor reads the temperature and shows it on my oled display and on the serial monitor. The program does not make the last if statement and takes the else statement. lines 72 to 80

test sensor

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6827
 

@henrik_t7 No surprise. Did it ever open the file? I am 100% sure Arduino did not design their product in a way that you can't use SPI and I2C at the same time. If the file was never successfully opened and written to I would suspect a bad card, or bad wiring. If it did open and write at the beginning then that likely means the card is full.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
henrik_t7
(@henrik_t7)
Member
Joined: 3 years ago
Posts: 21
Topic starter  

I have found a sketch on the web where you can test whether my SD card works and you could write to the SD card. so it works

* Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page:  https://arduinogetstarted.com/tutorials/arduino-write-variable-to-sd-card 
 */

#include <SD.h>

#define PIN_SPI_CS 4

File myFile;
int myInt = -52;
float myFloat = -12.7;
String myString = "HELLO";
char myCharArray[] = "ArduinoGetStarted.com";
byte myByteArray[] = {'1', '2', '3', '4', '5'};

void setup() {
  Serial.begin(9600);

  if (!SD.begin(PIN_SPI_CS)) {
    Serial.println(F("SD CARD FAILED, OR NOT PRESENT!"));
    while (1); // don't do anything more:
  }

  Serial.println(F("SD CARD INITIALIZED."));
  Serial.println(F("--------------------"));
  SD.remove("arduino.txt"); // delete the file if existed

  // create new file by opening file for writing
  myFile = SD.open("arduino.txt", FILE_WRITE);

  if (myFile) {
    myFile.println(myInt);    // write int variable to SD card in line
    myFile.println(myFloat);  // write float variable to SD card in line
    myFile.println(myString); // write String variable to SD card in line
    myFile.println(myCharArray); // write char array to SD card in line
    myFile.write(myByteArray, 5);
    myFile.write("\n"); // new line

    for (int i = 0; i < 5; i++) {
      myFile.write(myByteArray[i]); // new line

      if (i < 4)
        myFile.write(","); // comma
    }
    myFile.write("\n"); // new line

    myFile.close();
  } else {
    Serial.print(F("SD Card: error on opening file arduino.txt"));
  }

  // open file for reading
  myFile = SD.open("arduino.txt", FILE_READ);
  if (myFile) {
    while (myFile.available()) {
      char ch = myFile.read(); // read characters one by one from Micro SD Card
      Serial.print(ch); // print the character to Serial Monitor
    }
    myFile.close();
  } else {
    Serial.print(F("SD Card: error on opening file arduino.txt"));
  }
}

void loop() {
}

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6827
 

@henrik_t7 You need to post the entire sketch, there are things you have to do in advance of using the SD card. It's all there in the example.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
henrik_t7
(@henrik_t7)
Member
Joined: 3 years ago
Posts: 21
Topic starter  

I don't understand,

when I only use I2c with Oled display and temperature sensor, it works super fine together with arduino uno r4, reads the temperature on the Oled display fine.

When I then use the same SPI with SD card and temperature sensor, it cannot write temperature to the SD card?? It seems that the SD card does not open.

That's only if I have both wires for I2c and wires for SPI at the same time .

 

If I do a test of SPI card with SPI wires mounted and no wires for I2c, can it write data on the SD card??

Has anyone made a test whether the arduino uno r4 can use I2c and SPI at the same time???


   
ReplyQuote
(@scsiraidguru)
Member
Joined: 2 weeks ago
Posts: 22
 

You could setup the pin change interrupt and setup the buffer to store the SPI data then set the interrupt to change to the I2C bus.  They won't work at the same time.  The Giga has 3 IRQs that might be able to use SPI on one IRQ and I2C on another IRQ.  The Mega should have enough memory and flash to accomplish it.  I usually use my Mega devices for the I2C Master.  The Arduino UNO/Hero as slaves with the I2C devices like my LCD Screens for output.  Remember I2C is half duplex and SPI is full duplex.   I have used my Raspberry Pi 4B as the I2C Master because it has more resources.  


   
ReplyQuote
(@scsiraidguru)
Member
Joined: 2 weeks ago
Posts: 22
 

I2C Sensors
You might check for I2C Sensors instead of using SPI and I2C.  


   
ReplyQuote
Page 1 / 2