Conflict with I2C pins

Hi everyone,

I'm trying to make the classic "snake-game" with the help of a LED matrix (https://www.kiwi-electronics.com/en/32x16-rgb-led-matrix-panel-6mm-pitch-3748?search=led%20matrix). It's the first time I'm working with an Arduino (Leonardo), but I have some code writing experience. I will post the information which I think might be helpful. If you would require more info, I'll post that too.

Overview of the pins I use:
A0, output --> LED matrix 'A'
A1, output --> LED matrix 'B'
A2, output --> LED matrix 'C'
A3, output --> LED matrix 'LAT'
A4, input_pullup --> joystick VRx (to move horizontal)
A5, input_pullup --> joystick VRy (to move vertical)

D0, input --> bistable switch (difficulty selector(1) together with D1, 4 positions possible)
D1, input --> bistable switch (difficulty selector(2) together with D0)
D2, output --> LED matrix 'R1'
D3, output --> LED matrix 'G1'
D4, output --> LED matrix 'B1'
D5, output --> LED matrix 'R2'
D6, output --> LED matrix 'G2'
D7, output --> LED matrix 'B2'
D8, output --> LED matrix 'CLK'
D9, output --> LED matrix 'OE'
D10, output --> mini speaker
D11, output --> 4x 7 segment display (with shifting registers: DS)
D12, output --> 4x 7 segment display (with shifting registers: SHCP)
D13, output --> 4x 7 segment display (with shifting registers: STCP)

SDA & SCL --> 1 'device' to get the input of 'a startbutton', 'a resetbutton' and 'a bistable powerswitch'

To explain my reasoning:
At first I thought of making the coding of the LED matrix myself (without a library), since I don't need all the functions and so I wouldn't require all the pins. It did kind of work, but the result wasn't so stable and I didn't really know how to make it better. At that point I had just enough pins, but since it didn't really work the way I wanted, I used a library (GitHub - adafruit/RGB-matrix-Panel: Arduino library and example code for the 16x32 RGB matrix panels in the shop). This meant I was 3 pins short, luckily the I2C protocol (SDA and SCL) exists so I could use that.

When I was testing, I always did it piece by piece, so my Arduino never had all the pins connected and it worked fine. When I then put everything together and tested every piece again, the I2C protocol wasn't working. So after searching for a bit I found that the SDA and SCL pins are connected to D2 and D3 respectively. After pulling my LED matrix out of these pins, my code worked perfectly.

I'm working on this project for a few months now and I have learned and researched a lot, but I have no idea how to solve the double use of the D2/D3 pins. Since I already have everything soldered on a general PCB, my best thought would be to simply drop the 'startbutton (= used to start a new game; I would use a timer instead)', 'restbutton (=used to reset highscore; manually by reuploading)' and 'powerswitch (=used to turn on apparatus; turn on instantly)'. If someone would know an elegant solution which wouldn't require too much adjustments, I would love to hear it.

I don't know if it is needed but here is the code I used to test the I2C protocol.
Code:

/*EEPROM memory is standard set on 255 (1 byte). Since I need a max int of 509 (16 * 32 - 3), I need to use 2 bytes (adresses 0 and 1).
This value gets compared to the highsore and if needed changed. Initially a reset is needed, so the comparing with the highscore is correct.*/

#include <EEPROM.h>                           //needed for reading/writing highscore (=permanent memory)
#include <Wire.h>                             //communication with the I2c protocol

int highscore;
bool powerOn = false;                         //To indicate when aparatus has been set on/off
bool gamePlaying = false;                     //To indicate when a game is being played

int dataI2c;                                  //Data received from I2c-chip

void setup() {
  //delay(1000);                              //for testing purpose
  //while (!Serial);                          //for testing purpose

  Wire.begin();
  Serial.begin(9600);

  //Serial.println("Setup");                  //for testing purpose
  //delay(5000);                              //for testing purpose
}

void loop() {
  //asking data of I2c-device
  Serial.println("New Loop");
  Wire.requestFrom(0x38, 1);
  if (Wire.available()) {dataI2c = Wire.read();}
  else {Serial.println("Failed");}

  //set buttons on true/false when activated
  bool startButton = dataI2c & B00000100;
  bool powerButton = dataI2c & B00000010;
  bool resetButton = dataI2c & B00000001;
  Serial.print("Status start:");
  Serial.println(startButton);
  Serial.print("Status reset:");
  Serial.println(resetButton);
  Serial.print("Status power:");
  Serial.println(powerButton);

  //gamecode gets executed here
  if (powerButton) {
    if (resetButton) {reset();}         //WARNING: limited number of cycles, EEPROM-memory (set in // for testing)

    if (startButton && !gamePlaying) {
      //Start the game sequence
    }
  }

  else {
    //execute end code, standard off
  }
}


//reset function will reset the highscore, which is stored in EEPROM
void reset(){
  EEPROM.update(0, 0);                    //reset the 2 bytes (= highscore) to 0
  EEPROM.update(1, 0);
}

I didn't upload the wiring, because it's a mess and I didn't really think it's useful for the question.

If I understand correctly, you have a problem because you use the I2C pins for something else, but you also want an I2C connection. Look for a library for software I2C - there are some for Arduino.

What type of Arduino? That might be a important detail.

But it's literally what your question is about. How to get around a wiring mistake. Am I missing something?

Yes, that's exactly what I meant. It sounds like that might work, I will take a deeper look at it. Thank you.

Thank you for your response, I'm sorry for the confusion. I have an Arduino Leonardo. The problem is that I thought the SDA en SCL pins were 2 extra pins (apart from the 6 analog and 14 digital pins). So I was looking for something to reduce the total number of pins I needed (hence all the extra information). I will take a look at these libraries flashko (post #2) suggested and hopefully that will solve my problem.

When using software I2c, you will not need to free pins D2 and D3, but you will still need some other two pins - and I don’t see any free pins on your board.

You are lucky, the Leonardo has three additional digital pins available on the ICSP header

MISO = D14
SCK = D15
MOSI = D16

2 Likes

Check your reference, the SDA and SCL are not on the analog pins, not the digital ones.

On Leonardo? On Leonardo SDA is D2 and SCL is D3.

Aren't those connected also at D11, D12, and D13? I don't have a Leonardo here to check.

1 Like

Not on the Leonardo. The hardware SPI pins are only on the ICSP header. The SS pin is not available at all, as it is used for the Rx LED.

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