I am apparently having some kind of conflict with I2C and SPI ports.
I have Arduino MKR 1500 and MKR GPS connected in I2C port, and RFID Reader connected in SPI bus. Everything seems to be correct, but when I initialize SPI (RFID), GPS stops working or vice versa. It seems that one interrupts the other.
Is this something to do with interrupts? I don't quite understand how interrupts work.
The GPS model is MKR GPS and RFID model is MFRC522
Could anyone please give me any idea on what could be the reason I am facing this problem?
Thank you.
Code:
#include <Arduino_MKRGPS.h>
#include <SPI.h>
#include <MFRC522.h>
//Pins for RFID
const int pinRST = 15;
const int pinSDA = 11;
MFRC522 mfrc522(pinSDA, pinRST); // Set up mfrc522 on the Arduino
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if (!GPS.begin(GPS_MODE_I2C)) {
Serial.println("Failed to initialize GPS!");
while (1);
}
SPI.begin();
//Innitialize RFID Reader// Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
}
void loop() {
// check if there is new GPS data available
if (GPS.available()) {
// read GPS values
float latitude = GPS.latitude();
float longitude = GPS.longitude();
float altitude = GPS.altitude();
float speed = GPS.speed();
int satellites = GPS.satellites();
// print GPS values
Serial.print("Location: ");
Serial.print(latitude, 7);
Serial.print(", ");
Serial.println(longitude, 7);
Serial.print("Altitude: ");
Serial.print(altitude);
Serial.println("m");
Serial.print("Ground speed: ");
Serial.print(speed);
Serial.println(" km/h");
Serial.print("Number of satellites: ");
Serial.println(satellites);
Serial.println();
}else{
//Serial.println("==================");
}
}
Sorry my ignorance but I didn't understand. Shouldn't I use SDA pin on arduino? Which one should I use?
You can use it but not twice for different tasks. The SDA pin is part of the I2C interface so if you use the I2C interface to connect to the GPS you must not use it again for the RFID reader.
That link I've asked for. I never buy hardware without the necessary documentation. breakout boards must have their schematics as part of the documentation in my opinion.
Thanks for sending this. You caught my attention for the SDA PIN so I went to research further.
So I decided to use the RX PIN (in a desperate moment because I had to present the prototype to my boss! lol) and both modules (GPS and RFID) worked just fine. This was Friday, I didn't use the device until today Monday, and for my surprise, the problem has returned. GPS works but the RFID doesn't.
So I decided to use TX PIN today and now both modules are working fine again.
Which part of my explanation didn't you understand? You cannot use the same signal (even if it's available on multiple locations on that board) for two different tasks.
Sorry, if I didn't understand wasn't because I didn't pay attention or ignored your explanation, but maybe because of lack of experience at my end.
You said that wasn't very clever using SDA as chip select because SDA is I2C port, so as I mentioned I changed to RX and both worked fine for a few minutes and then it stopped work, and then I changed to TX port, both worked great and then stopped work. I am just trying to understand the logic here.
So maybe I misunderstood in the sense of that either TX and RX pins are also I2C and cannot be used because they will conflict with the I2C protocol being used by the GPS?
I might be again making more silly questions. Sorry, that's I am here in a forum seeking for assistance.
You said that wasn't very clever using SDA as chip select because SDA is I2C port, so as I mentioned I changed to RX and both worked fine for a few minutes and then it stopped work, and then I changed to TX port, both worked great and then stopped work. I am just trying to understand the logic here.
I did not realize that you stayed at using the RX pin, I got the impression that you changed back to the SDA pin.
So maybe I misunderstood in the sense of that either TX and RX pins are also I2C and cannot be used because they will conflict with the I2C protocol being used by the GPS?
No, these pins should be available for you to use.
I might be again making more silly questions. Sorry, that's I am here in a forum seeking for assistance.
Sure, I apologize, I interpreted your last post incorrectly.
Can you provide more details about the state if you use another pin. How long does it work? What happens if it doesn't work? Post the exact output of the serial monitor (also using code tags)!
Please post a wiring diagram of the changed wiring (may be hand drawn).
I am sorry for the long delay in responding. I had to stop working on this project but now I am back.
I am managed to get both modules working together, the problem was something that I had wired wrongly. I have attached the wiring diagram for reference.
But unfortunately, another issue arrived:
This MKR 1500 board has a known issue that makes the board freeze at nbAccess.begin() == NB_READY instruction. People say the problem is related to power-wise. That's why I had ditched the RFID module from my project.
However, now I am required to use the RFID again and the board has hanged ALWAYS at nbAccess.begin() == NB_READY.
Can anyone please give any clue how to resolve this?
However, now I am required to use the RFID again and the board has hanged ALWAYS at nbAccess.begin() == NB_READY.
You haven't posted any code yet that contains this line.
According to the wiring diagram you have also connected an SD card reader. Most cheap Chinese boards have a bug in the level converter which don't tri-state MISO if CS is pulled HIGH. Some of them can be fixed if you're good in hand soldering, otherwise you might have to buy a new board from one of the serious suppliers (Adafruit, Sparkfun).
mirandax:
I believe the SD Card Reader must be good.
Why make that assumption when it's so easy to verify it for yourself? Open File > Examples > SD > CardInfo. Upload the sketch to your MKR NB 1500. Open serial monitor and verify the SD card is recognized.
Sorry but I never said that the SD Card Reader is not recognized. I haven't had any issues with it. The SD Card is not part of the problem.
I only posted the code because Pylon asked for it, which makes sense as I didn't have post so far.
The issue that I have is that the board freezes when reaches if ((nbAccess.begin("2412", APN) == NB_READY)) { --- line 638 by the way.
Maybe my sentence "must be good" caused an eventual slackness of my part, and it should have been "should be good as I haven't had any issues". (English is not my first language).
Some of the problematic boards look exactly like the linked one. I would expect problems if more than just that once device is connected to the SPI bus.
Try to change the call to:
if ((nbAccess.begin("2412", APN, true, false) == NB_READY)) {
Does it still freeze? If you tell it to freeze if there's no connection, it does exactly that.
Just to update you that I've been having much better results after I added an external Li-Po battery connected to the board. The main point where the code used to freeze was at the modem.begin, but now it rarely freezes. Only sometimes.
Thanks for your suggestion, I will try with those parameters and get back to you. Thanks again.