Devices & Components
Arduino Uno Rev3
Jumper wires (generic)
Software & Tools
Arduino IDE
Project description
Code
SLAVE_CODE
c_cpp
Upload to SLAVE Arduino.
1// Include the required Wire library for I2C 2#include <Wire.h> 3int LED = 13; 4int x = 0; 5void setup() { 6 // Define the LED pin as Output 7 pinMode (LED, OUTPUT); 8 // Start the I2C Bus as Slave on address 9 9 Wire.begin(9); 10 // Attach a function to trigger when something is received. 11 Wire.onReceive(receiveEvent); 12} 13void receiveEvent(int bytes) { 14 x = Wire.read(); // read one character from the I2C 15} 16void loop() { 17 //If value received is 0 blink LED for 200 ms 18 if (x == 0) { 19 digitalWrite(LED, HIGH); 20 delay(200); 21 digitalWrite(LED, LOW); 22 delay(200); 23 } 24 //If value received is 3 blink LED for 400 ms 25 if (x == 3) { 26 digitalWrite(LED, HIGH); 27 delay(400); 28 digitalWrite(LED, LOW); 29 delay(400); 30 } 31} 32
MASTER_CODE
c_cpp
Upload to MASTER Arduino.
1// Include the required Wire library for I2C<br> 2#include<Wire.h> 3int x = 0; 4void setup() { 5 // Start the I2C Bus as Master 6 Wire.begin(); 7} 8void loop() { 9 Wire.beginTransmission(9); // transmit to device #9 10 Wire.write(x); // sends x 11 Wire.endTransmission(); // stop transmitting 12 x++; // Increment x 13 if (x > 5) x = 0; // `reset x once it gets 6 14 delay(500); 15} 16
SLAVE_CODE
c_cpp
Upload to SLAVE Arduino.
1// Include the required Wire library for I2C 2#include <Wire.h> 3int LED = 13; 4int x = 0; 5void setup() { 6 // Define the LED pin as Output 7 pinMode (LED, OUTPUT); 8 // Start the I2C Bus as Slave on address 9 9 Wire.begin(9); 10 // Attach a function to trigger when something is received. 11 Wire.onReceive(receiveEvent); 12} 13void receiveEvent(int bytes) { 14 x = Wire.read(); // read one character from the I2C 15} 16void loop() { 17 //If value received is 0 blink LED for 200 ms 18 if (x == 0) { 19 digitalWrite(LED, HIGH); 20 delay(200); 21 digitalWrite(LED, LOW); 22 delay(200); 23 } 24 //If value received is 3 blink LED for 400 ms 25 if (x == 3) { 26 digitalWrite(LED, HIGH); 27 delay(400); 28 digitalWrite(LED, LOW); 29 delay(400); 30 } 31} 32
Downloadable files
CIRCUIT_DIAG
CIRCUIT_DIAG
CIRCUIT_DIAG
CIRCUIT_DIAG
Comments
Only logged in users can leave comments