alexandros
Well-known member
I'm trying to establish communication between two Teensy LC via I2C. I've connected 4.7k pull-up resistors on pins 18 and 19, and connected the ground pins. I tried both powering up the two Teensies via USB,or connecting the 5V pins, so that the master (which is also printing to the serial monitor) provides power to the slave.
I'm trying the following code for the master:
and for the slave:
Which I got from this instructable https://www.instructables.com/id/I2C-between-Arduinos/
But I can't get them to work. Is there something I'm missing here?
I'm trying the following code for the master:
Code:
#include <Wire.h>
int x = 0;
void setup() {
// Start the I2C Bus as Master
Wire.begin();
Serial.begin(115200);
}
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(500);
}
Code:
#include <Wire.h>
int LED = 13;
int x = 0;
void setup() {
// Define the LED pin as Output
pinMode (LED, 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);
}
void receiveEvent(int bytes) {
x = Wire.read(); // read one character from the I2C
}
void loop() {
//If value received is 0 blink LED for 200 ms
if (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') {
digitalWrite(LED, HIGH);
delay(400);
digitalWrite(LED, LOW);
delay(400);
}
}
But I can't get them to work. Is there something I'm missing here?