Multiple items on SDA/SCL pins? Mega 2560

So I have some code that relies on SDA SCL for some timing, and I also want to use an external RTC clock (DS3231). Is there any way to use both? They both want to use pins 20 and 21.

I2C is a bus, you can have a gazillion items on there long as they have different addresses.

But you need to figure out a way of physically connecting them like eg taking SDA and SCL from Arduino to a breadboard row each, and connecting each device to those rows.

edit... I don't know the Mega, but an Uno has 2 pairs of pins connected to the same bus so you can use 2 devices without resorting to breadboard or whatever; perhaps Mega has those as well, I don't know.

further edit, added pic, source I2C Background - Total Phase

image

In understand, thank you. The problem is, the application Im using relys on pins 20 and 21 but they are being used as standard digital pins and I can not change them. (The arent using i2c functionality as I understand it).

Then move the unnamed/mystery non-I2C application to a different pair of pins. You've got a Mega, so plenty of pins to choose from.

I think I found your answer on this website at: SoftWire - Arduino Reference
You can define the pins with the Software I2C library to almost anything you want.
From the webpage:
"Software I2C library.
SoftWire is a software I2C implementation for Arduino and other Wiring-type environments. It utilises the pinMode(), digitalWrite() and digitalRead() functions. The pins to be used for the serial data (SDA) and serial clock (SCL) control lines can be defined at run-time. Alternatively it is possible to pass references to functions which read and control the SDA and SCL lines, thereby allowing direct port manipulation to be used if preferred. Multiple objects (for multiple software I2C buses) and clock-stretching by slave devices are supported. A timeout feature is included to prevent lockups by faulty or missing hardware. The microcontroller must function as the master device, multiple masters are not supported. GNU LGPL v2.1."
Let us know how it works out.

There are a few more software I2C libraries: https://github.com/Testato/SoftwareWire/wiki/Arduino-I2C-libraries.

Sometimes a few code lines in the sketch are enough to change to a software I2C library, sometimes you have to change all the library code.

The Mega has so many pins, and you happen to use pin 20 and 21 for something else ?

It is possible to use the SCL pin for other things when the I2C bus is not busy.

The problem is, the application Im using relys on pins 20 and 21 but they are being used as standard digital pins and I can not change them.

There are multiple I2C peripherals: maybe PD0/PD1?

https://duino4projects.com/wp-content/uploads/2013/04/ardunio_mega_pinout.jpg

DPin-20 and DPin-21 of MEGA turn into SDA and SCL lines (Fig-1) respectively of the I2C Bus when the following codes are added at the appropriate places of the sketch:

#include<Wire.h>
Wire.begin();

I2cMegaA

Figure-1:

So you have pre-compiled code that you're linking into your Arduino project? This is the only instance in which you cannot change the pins (easily). In all other cases - it can be done.
Instead of making things hard on yourself by having to resort to things like SoftSerial which may or may not work reliably, why not tell a bit more about this mystery application that blocks pins 20 & 21? Perhaps there's a way around this.

Perhaps the OP's arrangement includes a shield, with some stuff hard-wired into those pins. (shields are a pita...)

Tell all, OP....

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