How can I use all 6 analog read pins and I2C in the same program on the Arduino WiFi rev 4 maxima?

I want to use all 6 Analog Inputs and an external ds1307 Real Time Clock that uses I2C. I want to use the external RTC because I have read about the bad accuracy of the RTC included with the Rev4.

The I2C uses the last two Analog input pins. When I try to read AI 5, the RTC outputs bad data. It works correctly if I don’t try to read AI 5.

The AI read and the clock read are in different functions. It isn’t necessary to read both at the same time. Is there a way I can switch between using the ports as AI and I2C? That would disable I2C while doing analog reads and disable analog while reading the clock.

You can use an external 4051 analog multiplxer chip to acquire as many as 8 analog signals over any one of A0-A3 pins, and you use A4-A5/SDA-SCL as I2C Bus.

The QWIIC Connector (Fig-1) of R4UNOWiFi supports an I2C (I could not try due to lack of special 4-pin cable) Bus.
Qwiic Connector
Figure-1:

1 Like

Perhaps a quad ADC on the same I2C bus as your RTC would work?

2 Likes

You can also create a 3rd I2C bus on D0/D1...

#include <Wire.h>
#define WIRE2_SCL_PIN 0
#define WIRE2_SDA_PIN 1
TwoWire Wire2(WIRE2_SCL_PIN, WIRE2_SDA_PIN, ADDRESS_MODE_7_BITS, true);

void setup() 
{
   Wire2.begin();
}
1 Like

I have tested your code of post #4 (with SDA = DPin-4, SCL = DPin-5) and UNOR3 as an I2C Slave. The UNOR4 does not find the Slave at address 0b00100011. (The Slave is nicely found when A4/SDA, A5/SCL I2C Bus is used.)

I2CMaster-UNOR4 Sketch:

#include <Wire.h>
#define WIRE2_SDA_PIN 4
#define WIRE2_SCL_PIN 5
#define slaveAddress 0x23

TwoWire Wire2(WIRE2_SCL_PIN, WIRE2_SDA_PIN, ADDRESS_MODE_7_BITS, true);

void setup() 
{
   Serial.begin(9600);
   Wire2.begin();
   Wire2.beginTransmission(slaveAddress);
   byte busStatus = Wire2.endTransmission();
   if(busStatus !=0 )
   {
    Serial.println("Slave is not found!");
    while(true);
   }
   Serial.println("Slve is found.");
}

void loop() 
{
  
}

I2CSlave-UNOR3 Sketch:

#include<Wire.h>

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

void loop(){}

OUtput:

Slave is not found!
Slave is not found!

Thanks! This is exactly what I was looking for. Do I need to add these to my sketch, or do I need to add them to one of the libraries?

Hi @GolamMostafa - you can find the original discussion thread on the 3rd I2C bus here...
UNO R4 WiFi 3rd I2C Bus?

The Advanced Section (last page) of the UNO R4 WiFi FULL Pinout shows D0 (P301) has the optional SCI-SCL2 I2C definition and D1 (P302) has the optional SCI-SDA2 I2C definition.

I have not tried using the function on any other digital pins than D0/D1 as shown in the pinout diagram.
But I can confirm it works on D0/D1 with the UNO R4 WiFi as the Master.

Hi @franksxxx - just define and begin Wire2 from the sketch.

Note that to my knowledge the 3rd I2C bus only works on D0 / D1 as defined in the pinout diagram.

I have tried the following sketches with 3rd I2C Bus on DPin-0 (SCL) and 1 (SDA). After uploading, I have disconnected the USB cable from PC and powered the UNOR4 from 5V supply of UNOR3. The result is that the Slave is not found as is indicated by the continuous flashing of onboard LED at 100 ms interval. Please, post the sketches that you had executed along with your setup (did you keep the USB cable with the UNOR4?).
Master Sketch:

#include <Wire.h>
#define WIRE2_SDA_PIN 1
#define WIRE2_SCL_PIN 0
#define slaveAddress 0x23

TwoWire Wire2(WIRE2_SCL_PIN, WIRE2_SDA_PIN, ADDRESS_MODE_7_BITS, true);

void setup()
{
  pinMode(13, OUTPUT);
  Wire2.begin();
  Wire2.beginTransmission(slaveAddress);
  byte busStatus = Wire2.endTransmission();
  if (busStatus != 0 )
  {
    while (true)//error message for Slave not found
    {
      digitalWrite(13, HIGH);
      delay(50);
      digitalWrite(13, LOW);
      delay(50);
    }
  }
}

void loop()//I2C device found
{
  digitalWrite(13, HIGH);
  delay(5000);
  digitalWrite(13, LOW);
  delay(5000);
}

Slave Sketch:

#include<Wire.h>

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

void loop()
{}

Hi @GolamMostafa - I did this quickly to show it working.
4 wires connecting 5v, GND, SCL (D0) and SDA (D1) to an Adafruit LPS33HW Pressure Sensor

The LPS33HW uses an address of 0x5D...

Run code to scan I2C bus for Wire2:

#include <Wire.h>

int sda_pin = 1;
int scl_pin = 0;
TwoWire Wire2(scl_pin, sda_pin, ADDRESS_MODE_7_BITS, true);

void setup() {
  Wire2.begin();

  Serial.begin(9600);
  while (!Serial); // Leonardo: wait for Serial Monitor
  Serial.println("\nI2C Scanner");
}

void loop() {
  int nDevices = 0;

  Serial.println("Scanning...");

  for (byte address = 1; address < 127; ++address) {
    // The i2c_scanner uses the return value of
    // the Wire.endTransmission to see if
    // a device did acknowledge to the address.
    Wire2.beginTransmission(address);
    byte error = Wire2.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.print(address, HEX);
      Serial.println("  !");

      ++nDevices;
    } else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  } else {
    Serial.println("done\n");
  }
  delay(5000); // Wait 5 seconds for next scan
}

And the Serial Monitor results:

Scanning...
I2C device found at address 0x5D !
done

I have used this 3rd I2C bus many times for sensors without issue.

Hi @GolamMostafa - and if you need example code to read the sensor over Wire2 on D0/D1 I used:

#include <Wire.h>
#define WIRE2_SCL_PIN 0
#define WIRE2_SDA_PIN 1
TwoWire Wire2(WIRE2_SCL_PIN, WIRE2_SDA_PIN, ADDRESS_MODE_7_BITS, true);

#include <Adafruit_LPS35HW.h>
Adafruit_LPS35HW lps35hw = Adafruit_LPS35HW();

void setup() {
  
  Serial.begin(9600);

  Wire2.begin();

  lps35hw.begin_I2C(93, &Wire2);
}

void loop() {

  Serial.println(lps35hw.readPressure());

  delay(5000);
}

In my post #9, I connected UNOR4WiFi with UNOR3 using your proposed 3rd I2C Bus on DPin-0 and DPin-1. Unfortunately, the Master could not detect the UNOR3-Slave at address 0x23. Moreover, the DPin-0/1 is the UART Port which remains engaged with PC and IDE and is good for program deugging. Theoretically, the 3d I2C Bus should work for any DPin.

Thank you for your sketche which I will try with BME280 (PTH) sensor.

Hi @GolamMostafa - I hope it works out for you and on other digital pins.
I personally used D0 & D1 because Arduino called out their I2C function in the Advanced section of the Full Pinout, and I only otherwise need the Serial Monitor which goes through the ESP32 to the USB-C port...

Referring to post #11 @flynace, the following code contains arguments that allow to define any DPin for the SDA/SCL lines of the proposed 3rd I2C Bus. The code/sketch is compiled/uploaded, but it (post #5) does not work.

In the User's Manual, I have been able to discover that there are two I2C Buses (Fig-1) supported by the R7FA4M1 MCU. I undrstand that IIC0 bus is available on A4/A5 (SDA/SCL) pins and IIC1 is available on the 4-pin Qwiic connector of the of UNOR4WiFi Board. I am curious to know if the 3rd I2C Bus metioned by @flynace is a sofftware I2C Bus?


Figure-1:

Let that curiosity lead you to reading the data sheet and you will learn that the chip also has SCI capability that can be programmed to implement Simple IIC (Master only). D0/D1 have this capability.

2 Likes

Now, I understand as:

DPins             I2C Bus Channel            Bus Signals
A4/A5             IIC0                       SDA/SCL (SDA0/SCL0)

Qwiic             IIC1                       SDA1/SCL1

1/0               IIC2                       SDA2/SCL2
7/0               IIC2                       SDA2/SCL2

11/2              IIC3                       SDA3/SCL3

It is now 1:28 AM. Inshallah, I will test the above findings (of @flynace , @EmilyJane , and @Delta_G) tomorrow and will appear with results/difficuties.

Test Results:

I2C Channel             Status
IIC0                    OK
IIC1                    No Qwiic cable
IIC2                    Does not recognize UNO Slave at 0x23 (0100011)

Setup: (Fig-2)


Figure-2:

UNOR4-Master Sketch:

#include<Wire.h>
#define slaveAddress 0x23
#define WIRE2_SCL_PIN 0
#define WIRE2_SDA_PIN 1
TwoWire Wire2(WIRE2_SCL_PIN, WIRE2_SDA_PIN, ADDRESS_MODE_7_BITS, true);

void setup()
{
  Serial.begin(9600);
  Wire2.begin();
}

void loop()
{
  Wire2.beginTransmission(slaveAddress);
  byte busStatus = Wire2.endTransmission();
  if (busStatus != 0)
  {
    Serial.println("Slave is not found!");
  }
  else
  {
    Serial.println("Slave is found!");
  }
  delay(1000);
}

UNOR3-Slave Sketch:

#include<Wire.h>
#define slaveAddress 0x23

void setup() 
{
  Serial.begin(9600);
  Wire.begin(slaveAddress);
}

void loop() 
{

}

I have followed these codes of post #11 @flynace:

Really odd behavior.

Using Wire2 initialized as @GolamMostafa does in his sketch, the I2C scanner can find an SSD1306 OLED display, DS3231 RTC, and the EEPROM chip on the DS3231 board, but not a Nano running as a slave. All of these devices are connected to the I2C bus simultaneously.

Using Wire, all devices are found.

Using Wire2, the Adafruit_SSD1306 sample sketch runs properly on the OLED.

1 Like