0

I am trying to send data as a string from an Arduino Uno to another Arduino Mega. I have read about the I2C connection where we connect the RX to TX oppositely.

How to start and send and receive data between them? I have no idea how the code can be written.

asked Apr 18, 2018 at 9:14

1 Answer 1

0

At instructables there is a good post.

In short the code is

Master code:

// Include the required Wire library for I2C
#include <Wire.h>
int x = 0;
void setup() {
 // Start the I2C Bus as Master
 Wire.begin(); 
}
void loop() {
 Wire.beginTransmission(9); // transmit to device #9
 Wire.write(x); // sends x 
 Wire.endTransmission(); // stop transmitting
 x++; // Increment x
 if (x > 5) x = 0; // `reset x once it gets 6
 delay(500);
}

Slave code

// Include the required Wire library for I2C
#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);
 }
}

In the instructables post a lot more explanation is shown.

answered Apr 18, 2018 at 9:21
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.