Dear community,
In my project I use a GPIO expander for a keypad, called MCP23017 and an external RTC module, called DS3231. For the MCP23017 I use arduino_keypads library (GitHub - joeyoung/arduino_keypads: arduino libraries for keypad interface on I2C bus), for the RTC module I use RTClib (GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library). Both of them works beautiful separately, the problem is when I use simultaneously. Both of the libraries use I2C communication on the default pins GPIO 21 (SDA) and GPIO 22 (SCL). My question is how to set the RTC module to use the second I2C bus of the ESP32(for example on GPIO 25, GPIO 26)?
I tried different solutions:
- I rewrite in the RTClib.h the "bool begin(TwoWire *wireInstance = &Wire);" to "bool begin(TwoWire *wireInstance = &I2Ctwo);"
- rtc.begin(&I2Ctwo)
- rtc.begin(0x57, &I2Ctwo) //0x57 is the address of the RTC module
- I tried to use the MCP23017 on GPIOs 25 and 26
Nothing works, I'm stuck for a few days, any help would be great.
This is my code:
#include <Keypad_MC17.h>
#include <Wire.h>
#include <Keypad.h>
#include "RTClib.h"
#define I2CADDR 0x20
DS3231 myRTC;
TwoWire I2C1 = TwoWire(0); //I2C1 bus
TwoWire I2C2 = TwoWire(1); //I2C2 bus
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {0, 1, 2, 3};
byte colPins[COLS] = {4, 5, 6, 7};
Keypad_MC17 keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS, I2CADDR );
void setup(){
Serial.begin(57600);
while( !Serial ){/*wait*/}
I2C1.begin(21, 22, 100000UL);
I2C2.begin(26, 25, 100000UL);
keypad.begin();
rtc.begin(&I2C2);
rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop(){
char key = keypad.getKey();
if (key){
Serial.println(key);
rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);*/
Serial.print(myRTC.getYear(), DEC);
Serial.print(' ');
}
}