the ESP32 works well on 21/22 default I2C pins, but the config other pin as I2C pins as description, lets say:
I2C_0.begin(14 , 15 ); never work. why?
Thanks
Adam
Code: Select all
#include <Wire.h>
#include "Adafruit_MCP23017.h"
#define MCPAddress 0x20 // **** new add for ESP32CAM
Adafruit_MCP23017 mcp; //default scan address 000=0x20 ,001=0x24
#define I2C_Freq 100000
#define SDA_0 14
#define SCL_0 15
TwoWire I2C_0 = TwoWire(0);
void setup() {
Serial.begin(115200);
Serial.print("File : "), Serial.println(__FILE__);
const char compile_date[] = __DATE__ " " __TIME__;
Serial.print("Compile timestamp: ");
Serial.println(compile_date);
mcp.begin(); // use default address 0
setup_gpio_mode();
I2C_0.begin(SDA_0 , SCL_0 );
Wire.setClock(100000); //frequencia
i2c_scan(); // got the address and put it behind the serial to monitor //ได้ address แล้วปิดไว้ ไว้หลัง serial เพื่อ monitor
}
int i = 0;
void loop() {
Serial.println("Write 16 IO = 1");
test_io(1);
delay(1000);
Serial.println("Write 16 IO = 0");
test_io(0);
delay(1000);
i++;
if (i == 6) sleep(); //sleep เพื่อเช็ค status ตอนรันเทสไม่ต้อง sleep
}
void i2c_scan() {
byte error, address;
int nDevices;
Serial.println();
Serial.println("Scanning...");
nDevices = 0;
for (address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.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
}
void setup_gpio_mode() {
mcp.pinMode(0, OUTPUT);
mcp.pinMode(1, OUTPUT);
mcp.pinMode(2, OUTPUT);
mcp.pinMode(3, OUTPUT);
mcp.pinMode(4, OUTPUT);
mcp.pinMode(5, OUTPUT);
mcp.pinMode(6, OUTPUT);
mcp.pinMode(7, OUTPUT);
mcp.pinMode(8, OUTPUT);
mcp.pinMode(9, OUTPUT);
mcp.pinMode(10, OUTPUT);
mcp.pinMode(11, OUTPUT);
mcp.pinMode(12, OUTPUT);
mcp.pinMode(13, OUTPUT);
mcp.pinMode(14, OUTPUT);
mcp.pinMode(15, OUTPUT);
}
void test_io(int val) {
mcp.digitalWrite(0, val);
mcp.digitalWrite(1, val);
mcp.digitalWrite(2, val);
mcp.digitalWrite(3, val);
mcp.digitalWrite(4, val);
mcp.digitalWrite(5, val);
mcp.digitalWrite(6, val);
mcp.digitalWrite(7, val);
mcp.digitalWrite(8, val);
mcp.digitalWrite(9, val);
mcp.digitalWrite(10, val);
mcp.digitalWrite(11, val);
mcp.digitalWrite(12, val);
mcp.digitalWrite(13, val);
mcp.digitalWrite(14, val);
mcp.digitalWrite(15, val);
}
void sleep() {
Serial.println("Sleeping... 30 sec");
// ESP.deepSleep(30 * 1000000, WAKE_RF_DEFAULT);//Sleep for 60 seconds
}