I'm using the example from https://www.arduino.cc/en/Tutorial/MasterReader with an Arduino Uno, but ultimately want to run the master directly on a 328p without the Arduino-stuff.
The ESP8266 and the Uno communicate via a level-shifter, with 4k7 pullup resistors on both lines, on both sides of the level shifter.
I tried lowering the clock rate, but that didn't help.
Current state:
// ESP8266
#include <Wire.h>
void setup() {
Wire.pins(0, 2); // Select GPIO0 as SDA and GPIO2 as SCL
Wire.begin(8); // join i2c bus with address #8
Wire.setClock(14000L);
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}
//Uno
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Wire.setClock(14000L);
Serial.begin(9600); // start serial for output
}
void loop() {
Serial.println("blobb");
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
Did I make any obvious mistakes?
/e: Since (at least on the arduino uno) Wire.setClock(); doesn't like values < 14k, I set it to 50k and then set the TWI-prescaler to 4 (TWSR |= 1;), so it should run @ 12.5 kHz. The handheld osciloscope says ~13 kHz, close enough.
I used another Arduino (Nano) instead of the ESP8266, and the data transfer works, even with the level shifter.
Sadly, connecting the ESP to the bus messes up the bus and neither the Nano, nor the ESP8266 respond the master (yes, the slaves have different IDs.)