Defining SCL and SDA pins on NANO

HI Guys. I am teaching myself I2C.

I am using a NANO as a MASTER to control a Pro-Mini. The Pro-Mini is the SLAVE. The Pro-Mini needs to just blink an LED.

A) Do I need to define pins 18 and 19 on the NANO as SDA and SCL respectively?

If yes, then should I put the following in void setup() ?

#define SCL 19
#define SDA 18

B) If I dont need to define the pins as SDA and SCL respectively, how does the sketch know what is going on?
Ty for your replies.

// MASTER CODE IN NANO.
#include <Wire.h>

void setup() 
{
  Wire.begin();
}

void loop() 
{
  // send command to start blinking
  Wire.beginTransmission( 0x08);
  Wire.write( 100);
  Wire.endTransmission();

  delay( 5);

  // send command to stop blinking
  Wire.beginTransmission( 0x08);
  Wire.write( 101);
  Wire.endTransmission();

  delay( 5);
}
// SLAVE CODE IN NANO.
#include <Wire.h>

volatile bool enableBlinking = false;

void setup() 
{
  Wire.begin(0x08);   // join I2C bus as Slave with address 0x08
  Wire.onReceive( receiveEvent);

  pinMode( 13, OUTPUT);
}

void loop() 
{
  if( enableBlinking)
  {
    digitalWrite( 13, HIGH);
    delay( 250);
    digitalWrite( 13, LOW);
    delay( 250);
  }
}

void receiveEvent( int howMany)
{
  if( howMany == 1)   // extra safety check, expecting just one byte
  {
    int x = Wire.read();
    switch( x)
    {
      case 100:
        enableBlinking = true;
        break;
      case 101:
        enableBlinking = false;
        break;
    }
  }
}
1 Like

A) Do I need to define pins 18 and 19 on the NANO as SDA and SCL respectively?

run this on your Nano:

void setup() {
  Serial.begin(115200);
  Serial.println(F("\nI2C PINS"));
  Serial.print(F("\tSDA = ")); Serial.println(SDA);
  Serial.print(F("\tSCL = ")); Serial.println(SCL);
}

void loop() {}

if that compiles and work (serial monitor @ 115200 bauds), then you know SDA and SCL are predefined.

B) If I dont need to define the pins as SDA and SCL respectively, how does the sketch know what is going on?

it uses default values that are expected for this board. for example if you were on a MEGA, the variants file pins_arduino.h would define different pins

Ty. I ran the code.

The Serial monitor displayed the following:

I2C PINS
SDA = 18
SCL = 19

I did have to manually change the baud rate from 9600 to 115200.

So are you saying that because I called Wire.h, the NANO automatically reserves pin 18 and 19 for SDA and SCL? Ty

No, pretty sure the Arduino Core specific to the Nano maps those numbers to the SDA, SCL names. You'll note that @J-M-L's code does not #include wire.h.

Wire.h will be using hardware I2C, and the hardware I2C pins are A4 (18) and A5 (19).

and then it's used in the code you compile

void twi_init(void)
{
  // initialize state
  twi_state = TWI_READY;
  twi_sendStop = true;		// default value
  twi_inRepStart = false;
  
  // activate internal pullups for twi.
  digitalWrite(SDA, 1);
  digitalWrite(SCL, 1);
....

1 Like

No. The I2C signals are on specific pins of the board's ATmega328 processor. They set by the chip's design. They happen to be connected to 18 & 19 on this particular Arduino board.

1 Like

When you program the NANO using the picture provided by in one of the replies above, do you use the numbes in purple ( on the far left and right of the picture ) or grey ( closest to the NANO diagram) for defining pin numbers?

TY

When I program the Nano, I use purple digital pin numbers (2, 3, 4 ..., 13) and green analog pin numbers (A0, A1, A2 ..., A7).
By the way, these numbers are written on the board near the pins.

Seems all resolved :wink:

I2cnano
Figure-1:

Refer to Fig-1 given above:
1. On the NANO Board, we have legends/inscriptions: A4 and A5, which are repectively connected with physical pin-27 and pin-28 of the microcontroller (MCU).

2. A4 and A4 have numerical values, which are respectively 18 and 19. A4/18 and A5/19 are known to Compiler as Digital IO Pins (DPin) when Port-C is enabled by closing K2. (K1 and K3 remaiin open.)

3. There are three individual electronics modules within the MCU and these are:
(1) Port-C (Digital IO Port)
(2) I2C Interface (Inter-integrated Circuit)/TWI (Interface (Two Wire Interface)
(3) Ananlog-to-Digital Controller

4. All the above three modules share common two pins (27 and 28) of the MCU.

5. By default (after power up), K1 and K3 are at open condiion; K2 is at close condition. As a result, pc4 and pc5 IO lines of Port-C are routed to DPin-A4 and DPin-A5. This is the reason for the NANO board to have the inscriptions: A4 (DPin-A4) and A5 (DPin-A5). These pins are also given unseen numerical values of 18 and 19 in order to allow a programmer to use for() loop to configure the directions of a group of IO lines.

6. If we want to activate the I2C Interface, then we close K1 by storing 1 (HIGH) at TWEN-bit of TWCR Register, which is equivalent to Wire.begin() instruction. As a result, the SDA (Serial Data) and SCL (Serial Clock) line of the I2C Logic are routed to DPin-A4/18 and DPin-A5/19. (K2 and K3 remain opened.)

7. If we want to activate the ADC Module, then we close K3 by storing 1 (HIGH) at ADEN-bit of ADCSR Register. As a result, the ADC4 (Ch-4) and ADC5 (Ch-5) of the ADC are routed respectively to DPin-A4 and DPin-A5. (K1 and K2 remain opened.)

I would appreciate to receive any corrections/modifications/additions needed.

1 Like

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