Hi all,
I am trying to make 2 Wemos D1 mini cards communicate bidirectionally via I2C, bu i can`t figure why is not working.
On both boards I have LEDs on pin D3 and a push button on pin D6.
The D1 (SCL) is connected directly to D1 of the other board, and D2 (SDA) to D2.
PB and LED tested and working.
The Master:
#include<Wire.h>
const int pbPin = D6;
const int ledPin = D3;
int slaveCh = 8;
int MasterRX = 0;
int MasterTX = 0;
void I2Cscan() {
Serial.println("\n------I2C Scan Start------");
byte error, address;
int nDevices;
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.println(address,HEX);
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("------I2C Scan end------\n");
}
void setup() {
delay(2000);
Serial.begin(115200);
Wire.begin();
Wire.status();
I2Cscan();
pinMode(ledPin, OUTPUT);
pinMode(pbPin, INPUT_PULLUP);
}
void loop(){
Wire.requestFrom(slaveCh,2); // Slave Address, Q.ty of byte (int=2)
MasterRX = Wire.read();
Serial.println("RX: "+String(MasterRX));
digitalWrite(ledPin,MasterRX);
MasterTX = digitalRead(pbPin);
Wire.beginTransmission(slaveCh); // start transmit
Wire.write(MasterTX);
Wire.endTransmission(); // stop transmitting
Serial.println("TX: "+String(MasterTX));
delay(500);
}
The Slave:
#include<Wire.h>
const int pbPin = D6;
const int ledPin = D3;
int slaveCh = 8;
int SlaveRX = 0;
int SlaveTX = 0;
void setup() {
delay(2000);
Serial.begin(115200);
Wire.begin(8); //Slave Address
Wire.onReceive(RX_Event); //Function call when Slave receives value from master
Wire.onRequest(RQ_Event); //Function call when Master request value from Slave
Wire.status();
pinMode(D3, OUTPUT);
pinMode(D6, INPUT_PULLUP);
}
void RX_Event (int howMany){ //This Function is called when Slave receives value from master
SlaveRX = Wire.read();
Serial.println("RX: "+String(SlaveRX));
}
void RQ_Event(){ //This Function is called when Master wants value from slave
SlaveTX = digitalRead(D6);
Wire.write(SlaveTX);
Serial.println("TX: "+String(SlaveTX));
}
void loop(void){
delay(100);
}
So after the upload, the master can see that there is a slave connected to the address 8, but don't receive any data (doesn't change the RX value when i push the button).
23:58:04.138 -> ------I2C Scan Start------
23:58:04.138 -> I2C device found at address 0x08
23:58:04.138 -> ------I2C Scan end------
23:58:04.138 ->
23:58:04.138 -> RX: 255
23:58:04.138 -> TX: 1
23:58:04.652 -> RX: 255
23:58:04.652 -> TX: 1
23:58:05.166 -> RX: 255
23:58:05.166 -> TX: 1
23:58:05.681 -> RX: 255
23:58:05.681 -> TX: 1
The slave also don`t receive or transmit any data, so there is no data on the serial.
Can anyone help me?