Notifications
Clear all

i2c AND spi do not work together

29 Posts
3 Users
0 Reactions
2,086 Views
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 8047
 

@scsiraidguru He is using an I2C temp sensor, but the SD is SPI.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


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

Posted by: @scsiraidguru

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.  

Please point out where in the datasheets it says you can't use SPI and I2C together.

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


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

@henrik_t7 FYI @scsiraidguru They work together just fine, here is an example of a weather station using !2C for temperature readings and SPI for SD card operations. https://simple-circuit.com/arduino-weather-data-logger-sd-card/ . I suggest you try two things, the first is to shorten the filename to 8 chars, if that does not work, remove all the I2C code and make sure the SD card logic works with YOUR code.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


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

@henrik_t7 Your FILENAME is too long, SD uses 8.3 old DOS style names. Here is what the Arduino SD library documentation says. I highly recommend reading documentation.

Screenshot 2024 04 04 at 06.21.17

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@scsiraidguru)
Member
Joined: 1 year ago
Posts: 63
 

Does it explain how to accomplish it?   Many users have reported the same issues that you have with no solution.  You could try using a Mega 2560 instead an Uno.   I would try using interrupts to accomplish it.   I have had 14+ devices communicating with no issues on I2C.   That is why I posted the various I2C devices you could replace your SPI devices with.  


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

@scsiraidguru We have no idea who you are addressing your comments to. Try using the Reply link bottom right pic enclosed.

The second pic shows he has an 11.3 filename, my reading of the code tells me it is old DOS format of 8.3. Fix that and see if it works. I will bet a lot it does as I have seen many sketches using SD and I2C with no 'tricks'.

In case you or the OP missed it, attached is the screen shot of the documentation explaining the 8.3 rule. It can be found in the Arduino documentation at https://www.arduino.cc/reference/en/libraries/sd/

 

Screenshot 2024 04 04 at 13.05.13
Screenshot 2024 04 04 at 13.04.05
Screenshot 2024 04 04 at 06.21.17

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


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

@henrik_t7 Did you fix the filename length? It has to be old DOS 8.3 format, you have more than 8 chars in your name.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
henrik_t7
(@henrik_t7)
Member
Joined: 4 years ago
Posts: 26
Topic starter  

After I shortened the name to only 4 letters and when I saw the verify code the IDE comes up with a strange error?? What happens now??

#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
  SD.begin(4); // CS pin for SD card
    
  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.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("temp.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.println(F("Temperature (C): "));
    dataFile.println(celsius);
    dataFile.println(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
}

Fejl

In file included from m:\Arduino Sketch\libraries\Adafruit_GFX_Library/Adafruit_GFX.h:12:0,
from M:\Arduino Sketch\Test af DS18B20 sensors\sketch_apr2a_Oled_SD_DS18B\sketch_apr2a_Oled_SD_DS18B.ino:6:
m:\Arduino Sketch\libraries\Adafruit_BusIO/Adafruit_I2CDevice.h:10:36: error: 'TwoWire' has not been declared
Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire = &Wire);
^~~~~~~
m:\Arduino Sketch\libraries\Adafruit_BusIO/Adafruit_I2CDevice.h:30:3: error: 'TwoWire' does not name a type; did you mean 'TwoWire_h'?
TwoWire *_wire;
^~~~~~~
TwoWire_h
m:\Arduino Sketch\libraries\Adafruit_BusIO/Adafruit_I2CDevice.h:10:56: error: 'Wire' was not declared in this scope
Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire = &Wire);
^~~~
In file included from M:\Arduino Sketch\Test af DS18B20 sensors\sketch_apr2a_Oled_SD_DS18B\sketch_apr2a_Oled_SD_DS18B.ino:7:0:
m:\Arduino Sketch\libraries\Adafruit_SSD1306/Adafruit_SSD1306.h:132:42: error: 'TwoWire' has not been declared
Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi = &Wire,
^~~~~~~
m:\Arduino Sketch\libraries\Adafruit_SSD1306/Adafruit_SSD1306.h:175:3: error: 'TwoWire' does not name a type; did you mean 'TwoWire_h'?
TwoWire *wire; ///< Initialized during construction when using I2C. See
^~~~~~~
TwoWire_h
m:\Arduino Sketch\libraries\Adafruit_SSD1306/Adafruit_SSD1306.h:132:58: error: 'Wire' was not declared in this scope
Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi = &Wire,
^~~~
M:\Arduino Sketch\Test af DS18B20 sensors\sketch_apr2a_Oled_SD_DS18B\sketch_apr2a_Oled_SD_DS18B.ino:16:56: error: 'Wire' was not declared in this scope
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
^~~~

exit status 1

Compilation error: 'Wire' was not declared in this scope


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

@henrik_t7 Let me copy your sketch and compile it to see any errors. Glad to see I was right about the filename, I was pretty sure I2C and SPI would work together.

Give me a few, I am still tired form my afib attack this AM.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


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

@henrik_t7 It compiles fine. I did notice when installing the DallasTemperature library, it prompted me for a dependency on Wire and your last error says the Wire library is missing. Just do a Remove followed by an Install of the DallasTemperature library in the Library Manager. If that does not fix it, maybe you made the same mistake with other libraries.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


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

@henrik_t7 Since the DallasTemperature library includes the Wire library you can remove that first include of Wire, it is redundant.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
henrik_t7
(@henrik_t7)
Member
Joined: 4 years ago
Posts: 26
Topic starter  

Hello

I have tried to do as you write, but it is the same error every time,

see 04/09 2024 post.??


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

@henrik_t7 I got your sketch to compile with no errors, see https://forum.dronebotworkshop.com/arduino/i2c-and-spi-do-not-work-together/paged/2/#post-46818

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


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

@henrik_t7 If you get errors, and I do not, what do you think is the possible reason for that difference?

I am enclosing a screen grab of a successful verify (compile)

BTW, you need to change the Title, it has nothing to do with I2C and SPI working together. They work together just fine. The original problem was you violated the 8.3 filename standard. And the latest problem is an incorrectly installed Library as I have demonstrated.

Either you failed to follow my instructions (Remove DallasTemperature and then re-install it BUT reply INSTALL ALL see attached pic

The last few pics show the library dependencies. You may need to enlarge it, but that is how the IDE knows about needing  the OneWire library.

Screenshot 2024 04 13 at 06.55.58
Screenshot 2024 04 13 at 07.04.02
Screenshot 2024 04 13 at 07.03.23

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Page 2 / 2