Introduction: I2C Arduino Lab6

Picture of I2C Arduino Lab6

This lab will connect two Arduinos together using I2C to send a RGB value from one to the other and control a RGB LED using those values.

Step 1: Connecting the Two Arduinos

Picture of Connecting the Two Arduinos

step 1. You need 2 Arduinos and 2 bread boards

step 2. connect the ground on the controller to the breadboard.

step 3. Connect the A5 and A4 ports to the bread board from the Controller

step 4. with the other breadboard and Arduino, copy steps two and three.

step 5. After both are hooked up to their bread boards, connect them together end to end making sure each wire is in the same row.

Step 2: Setting Up Slave_Sender Controller

Picture of Setting Up Slave_Sender Controller

a1 2 3 grn power

step 1. you will need 3 potentiometers for this section.

step 2. place a red wire from the 5v port to the positive rail on the bread board.

step 3. place a black wire from the ground to the negative rail on the bread board.

step 4. place a potentiometer on the bread board.

step 5. connect a wire from the A1 port on the Arduino to the middle prong on the potentiometer.

step 6. connect a red wire from the top prong of the potentiometer to the positive rail of the breadboard.

step 7. connect a black wire from the top prong of the potentiometer to the positive rail of the breadboard.

step 8. repeat steps 4-7 for the rest of the potentiometers.

function: The potentiometers will create an analog value that will be sent to the Master Reader. in the code the vales will have to be changed from 1024 to 255 so that it can be written and sent to the other arduino controller. With the potentiometers hooked up properly, you will be able to adjust the LED on the other end with different brightnesses of red, green and blue.

Step 3: Setting Up Master_Reader Controller

Picture of Setting Up Master_Reader Controller

  1. Connect the RGB LED to the breadboard.
  2. Connect the common cathode to the ground rail of the breadboard
  3. Connect a 220Ω resistor to the other legs of the LED
  4. Connect a lead from each of the resistors to the correct pins on the Arduino
    1. Red -> 9
    2. Green -> 10
    3. Blue -> 11
  5. Connect the ground rail to the ground on the Arduino
  6. Place the button over the middle separator on the breadboard
  7. Connect the top-left side to the positive rail on the breadboard and connect it to the 5V pin on the Arduino
  8. Connect the bottom-left pin to pin 2 on the Arduino, connect a 10KΩ resistor from the bottom-right side to the ground rail on the breadboard

Step 4: Add Code for the Slave_Sender Controller

// Wire Slave Sender

// by Nicholas Zambetti

// 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 int potRPin = 1; //pin for the red potentiometer int potGPin = 2; //pin for the green potentiometer int potBPin = 3; //pin for the blue potentiometer int valR =0; int valG =0; int valB =0;

void setup() {

Wire.begin(8); // join i2c bus with address #8 Wire.onRequest(requestEvent); // register event Serial.begin(9600);

}//end setup()

void loop() {


}//end loop()

// function that executes whenever data is requested by master // this function is registered as an event, see setup() void requestEvent() {

Serial.println("requested"); valR = analogRead(potRPin); valG = analogRead(potGPin); valB = analogRead(potBPin); valR=map(valR, 0, 1024, 0, 255); valG=map(valG, 0, 1024, 0, 255); valB=map(valB, 0, 1024, 0, 255); String red = String(valR); String green = String(valG); String blue = String(valB); String color = "" + red+","+blue+","+green; char colorArray[color.length()+1]; color.toCharArray(colorArray, color.length()+1); Serial.println(color); Wire.write(colorArray);

}//end requestEvent()

Step 5: Add Code for the Master_Reader Controller

Upload this code to the Master Arduino with the LED attached.

Step 6: Use the Circuit

Use the 3 potentiometers to control the values of red, green, and blue for the LED. Push the button to request the values from the other Arduino and update the LED with the current values.