Hi,
I'm trying to get my Arduino Uno board to work with the Huzzah Feather ESP8266 board using I2C. I've been reading through other posts and websites if I could find a similar problem, but nothing seemed to have helped. I really hope anyone of you could help me out.
First I connected 2 Arduino Uno's with eachother and used this code:
Master Uno:
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
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);
}
Slave Uno:
// Include the required Wire library for I2C
#include <Wire.h>
#include <Wire.h>
int LED = 13;
int x = 0;
void setup() {
// Define the LED pin as Output
pinMode (13, OUTPUT);
// Start the I2C Bus as Slave on address 9
Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent(int bytes) {
x = Wire.read(); // read one character from the I2C
//Serial.println("received event");
}
void loop() {
delay(1);
//If value received is 0 blink LED for 200 ms
//Serial.print("x is ");
//Serial.println(x);
if (x == 0) {
Serial.println("x = 0");
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);
}
//If value received is 3 blink LED for 400 ms
if (x == 3) {
Serial.println("x = 3");
digitalWrite(LED, HIGH);
delay(400);
digitalWrite(LED, LOW);
delay(400);
}
}
This worked like a charm. However, when I use the same code from the master Arduino Uno in the Feather ESP8266, my slave Uno doesn't always catch up on what is being send. It sometimes catches what is being sent by the ESP8266 and displays it on the serial monitor, but most of it isn't received/displayed.
I also tried adding the following code in the ESP8266 setup, using 15000 and 40000. Nothing seemed to matter.
Wire.setClockStretchLimit(40000);
So it looks like this:
// Include the required Wire library for I2C
#include
#include <Wire.h>
int x = 0;
void setup() {
// Start the I2C Bus as Master
Wire.begin();
//Wire.setClock(10000);
Wire.setClockStretchLimit(40000);
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(9); // transmit to device #9
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
Serial.println(x);
x++; // Increment x
if (x > 5) x = 0; // `reset x once it gets 6
delay(3000);
}
The same holds when I am using the Uno Master requesting data from de Uno slave --> this works perfectly. When I request data with ESP8266 (master) from the Uno (slave), I again doesn't receive anything, or at least not all of it.
Code Feather ESP8266 (master)
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(4,5); // join i2c bus (address optional for master)
Wire.setClockStretchLimit(15000);
Serial.begin(9600); // start serial for output
}
void loop() {
Serial.println("requesting");
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);
}
Code uno slave:
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
//Serial.begin(9600);
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
//Serial.println("received");
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}
I'm all situations (uno vs uno and Huzzah Feather ESP8266 vs Uno) I'm using a bidirectional logic level converter (I2C bidirectional logical converter.
The wiring is of the boards is shown below:
.
Now, I have to add that the bi-directional logic level converter looks different from the one I used. The GND from the LV is OE (as can seen in the html link above. I have the OE connected to either the 3.3V(LV) or 5.0V(HV), as was recommended. If this is not connected, it wouldn't transmit data at all.
I hope anyone can help me with this problem as I've been trying to figure it out for a week if not longer, and it's getting a bit frustrating.