SDA & SCL pins as outputs without false triggers

Hello,

I’m using an Arduino Mega to control several outputs. For easy circuit trace routing, I decided to use pins 20 & 21 for outputs 15 & 16 which also happen to be the SDA & SCL pins.

I set each pin low before initializing them to prevent false triggers upon startup. However, I’m finding that the SDA & SCL pins consistently fire together at startup before resetting soon after.

How can I handle initializing these pins differently as to avoid the false triggers upon startup?

Thanks!


//This array contains the pin numbers that corresponds to the output.
const int output[numOfOutputs] =    {12,11,10,9,8,7,6,5,14,15,16,17,18,19,20,21,36,34,32,30,28,26,24,22,38,40,42,44,46,48,50,52};


void setup() {
  
  //---------------PIN MODES---------------
  for (int pin = 0; pin < numOfOutputs; pin++){
    digitalWrite(output[pin], LOW);
    pinMode(output[pin], OUTPUT);}
    
}

From the Mega R3 schematic:

I believe if you think about that schematic fragment for a moment, you'll see why you're seeing a HIGH on startup on both SCL and SDA.

The hardware solution should be equally obvious.

That may not work. After startup, there is a pause while the Mega's bootloader waits to see if new code is to be uploaded from the PC, and if not, your code begins running. During this pause, the pins are floating, which means that anything connected to them might get a false trigger. To prevent that, you could add pull-down resistors to the pins, like 10K.

I'm not sure, but this may be a hardware feature of the ATMega chip, and nothing you can do about it, except maybe add those pull-downs.

After any AVR reset, all GPIO pins come up as inputs (high-impedance) with internal pullups disabled. This indicates that SCL/SDA are effectively floating unless something else on the bus is pulling them.

Why you might see activity anyway, What people often interpret as “toggling” is usually one of these: External I2C pullups hold the lines high If you have 4.7k/10k pullups on the bus (or a module with pullups), both lines will sit HIGH immediately after reset.
A device is doing something during power-up

Noise / ringing on long wires

High-impedance inputs + long leads + scope probe ground lead = easy to “see” spikes that aren’t real logic-level signaling.

Your sketch (or libraries) starts Wire early
The first time Wire.begin() runs, the TWI hardware takes over the pins and you can then see real edges.

Bootloader note:
The Mega bootloader normally uses UART0 and often blinks LED on D13; it doesn’t use I2C, so it should not be clocking SCL/SDA.

Quick way to prove where it’s coming from Disconnect SDA/SCL from the rest of the I2C bus and reset the Mega:
If the “toggling” disappears: it was the external devices/bus. If it remains: it’s likely measurement noise or an unusual board/wiring issue.

A possible way to keep the bus quiet at startup

Make sure peripherals don’t back-power through I2C when the Mega is off (power sequencing).

See post #2. Until the pins are made outputs, they will be high.

Something not covered by the answers you already have is what you are doing with the other pins: you can't assume that the other pins are guaranteed not to give the false triggering you are concerned about on start up. Those pins are floating until your code initialises them so could possibly be high on startup. If you want to guarantee they are low then they need pulldown resistors.

Considering the default state for the I2C bus is HIGH that really makes no sense. Setting them LOW will create false triggers.