loading

The Arduino and most other microcontroller boards, and microcomputers are usually equipped with at least one I2C communication channel. The I2C is typically used to connect sensors to the microcontroller, and the microcontroller to operate as a Master for the I2C bus. Most microcontrollers however can be configured as Slave I2C devices. This allows for the I2C bus to be used for communication between the controllers.

I have posted a number of Tutorials on how to connect I2C devices to Arduino and ESP8266 controllers. Multiple people have asked me for example of Arduino to Arduino I2C communication. Finally I have found time to prepare this Tutorial.

Here I will show you how you can connect 2 Arduino Nano boards, and configure one of them to control the second one over I2C bus. The I2C Slave controller will measure distance with Ultrasonic Ranger, and when asked will send the distance to the Master controller over the I2C bus.

Step 1: Components

  1. 2 Arduino compatible board (I used 2 Arduino Nanos, because I have them, but any other will be just fine)
  2. One Ultrasonic Ranger Sensor Module - I used US-015, but HC-SR04, or very much any other will also work
  3. 4 Female-Female jumper wires

Step 2: Connect Ground, and I2C Wires to the Master I2C Arduino Nano

We need to connect Ground and the 2 I2C wires between the Arduino boards. We will start with the Master I2C Arduino board:

  1. Connect Female-Female Ground jumper wire (Black Wire) to the Ground pin of the Master Arduino Nano board (Picture 1)
  2. Connect Female-Female SCL jumper wire (Yellow Wire) to the SCL/Analog pin 5 of the Master Arduino Nano board (Picture 1)
  3. Connect Female-Female SDA jumper wire (Green Wire) to the SDA/Analog pin 4 of the Master Arduino Nano board (Picture 1)
  4. Picture 2 shows where are the Ground, SDA/Analog pin 4, and SCL/Analog pin 5, pins of the Master Arduino Nano

Step 3: Connect Ground, and I2C Wires to the Slave I2C Arduino Nano

We need to connect Ground and the 2 I2C wires to the Slave I2C Arduino board:

  1. Connect the other end of the Ground wire (Black Wire) to the Ground pin of the Slave Arduino Nano board (Picture 1)
  2. Connect the other end of the SCL wire (Yellow Wire) to the SCL/Analog pin 5 of the Slave Arduino Nano board (Picture 1)
  3. Connect the other end of the SDA wire (Green Wire) to the SDA/Analog pin 4 of the Slave Arduino Nano board (Picture 1)
  4. Picture 2 shows where are the Ground, SDA/Analog pin 4, and SCL/Analog pin 5, pins of the Slave Arduino Nano

Step 4: Connect the Ultrasonic Ranger to the Slave Arduino Nano

  1. Connect Ground(Black wire), Power(Red wire), Trigger(Gray wire), and Echo(Purple wire) to the Ultrasonic Ranger Sensor Module (Picture 1)
  2. Connect the other end of the Power wire(Red wire) to the 5V power pin of the Slave Arduino Nano board (Picture 2)
  3. Connect the other end of the Ground wire(Black wire) to Ground pin of the Slave Arduino Nano board (Picture 3)
  4. Connect the other end of the Trigger wire(Gray wire) to Digital pin 2 of the Slave Arduino Nano board (Picture 3)
  5. Connect the other end of the Echo wire(Purple wire) to Digital pin 3 of the Slave Arduino Nano board (Picture 3)
  6. Picture 4 shows in Red where are the Ground, 5V Power, Digital 2, and Digital 3 pins of the Slave Arduino Nano(In Blue are shown the connections made in the previous step)

Step 5: Start Visuino, and Select the Arduino Board Type

Since the Arduino Nano has only one Serial port, and it is needed to program the Arduino, you will need to program the Arduino Nano before the GPS is connected.

To start programming the Arduino, you will need to have the Arduino IDE installed from here: http://www.arduino.cc/ .

Make sure that you install 1.6.7 or higher, otherwise this Tutorial will not work!

The Visuino: https://www.visuino.com also needs to be installed.

  1. Start Visuino as shown in the first picture
  2. Click on the "Tools" button on the Arduino component (Picture 1) in Visuino
  3. When the dialog appears, select "Arduino Nano" as shown in Picture 2

Step 6: In Visuino: Configure the I2C As Slave

  1. In the Object Inspector expand the "I2C" property (Picture 1)
  2. In the Object Inspector set the value of the "Address" sub property to "22" (Picture 1) - You can set another number. Just make sure the Master Arduino project uses the same number so the 2 devices will be able to talk
  3. In the Object Inspector set the value of the "Is Slave" sub property to "True" (Picture 2)

Step 7: In Visuino: Add and Connect Make Structure Component

We need to send a raw analog value over the I2C. The simplest option is to packet it as a structure with one floating point element:

  1. Type "struct" in the Filter box of the Component Toolbox then select the "Make Structure" component (Picture 1), and drop it in the design area
  2. Connect the "Out" output pin of the MakeStructure1 component to the "In" input pin of the "I2C" channels of the Arduino component (Picture 1)
  3. Connect the "Request" output pin of the "I2C" channel of the Arduino component to the "Clock" input pin of the MakeStructure1 component (Picture 2)

Step 8: In Visuino: Add Analog Channels to the Make Structure Component

  1. Click on the "Tools" button of the MakeStructure1 component (Picture 1) to open the "Elements" editor (Picture 2)
  2. In the "Elements" editor select the “Analog” element on the right, and then clickon the "+" button on the left (Picture 2) to add Analog element (Picture 3)
  3. Close the "Elements" editor.

Step 9: In Visuino: Add and Connect Ultrasonic Ranger Component

  1. Type "sonic" in the Filter box of the Component Toolbox then select the "Ultrasonic Ranger(Ping)" component (Picture 1), and drop it in the design area
  2. Connect the "Ping(Trigger)" pin of the UltrasonicRanger1 component (Picture 3) to the "Digital" input pin of the Digital[ 2 ] channel of the Arduino component (Picture 2)
  3. Connect the "Out" pin of the Digital[ 3 ] channel of the Arduino component to the "Echo" input pin of the UltrasonicRanger1 component (Picture 3)
  4. Connect the "Out" output pin of the UltrasonicRanger1 component to the "In" input pin of the "Elements.Analog1" of the MakeStructure1 component (Picture 4)

Step 10: Generate, Compile, and Upload the Arduino Code for the I2C Slave Arduino

Picture 1 shows the complete Visuino diagram of the Slave I2C Arduino project.

  1. Connect the Slave Arduino Nano (The one with the Ultrasonic Ranger attached) to the computer
  2. In Visuino, Press F9 or click on the button shown on Picture 2 to generate the Arduino code, and open the Arduino IDE
  3. In the Arduino IDE, click on the Upload button, to compile and upload the code (Picture 3)

Step 11: In Visuino: Add and Connect I2C Device Component

We need send I2C Requests to the Slave Arduino specifying the device address and asking for 4 bytes of data (floating point value. For this we need to add I2C Device component:

  1. Start a new project, and select Arduino Nano board as you did in Step 5.
  2. Type "i2c" in the Filter box of the Component Toolbox then select the "I2C Device" component (Picture 1), and drop it in the design area
  3. In the Object Inspector set the "Address" property of the I2CDevice1 component to "22" (Picture 2) - this address should be the same as the one set in the Slave Arduino project
  4. Connect the "Out" output pin of the I2CDevice1 component to the "In" input pin of the "I2C" channel of the Arduino component (Picture 3)

Step 12: In Visuino: Add I2C Request to the I2C Device Component

Next we need to add a request element to the I2C Device component and specify the size of the request:

  1. Click on the "Tools" button of the I2CDevice1 (Picture 1) to open the "Elements" editor (Picture 2)
  2. In the "Elements" editor select the “Request” element on the right, and then clickon the "+" button on the left (Picture 2) to add I2C Request element (Picture 3)
  3. In the Object Inspector set the value of the "Size" property to "4" (Picture 3) - This will specify 4 bytes (floating point value is 32 bits = 4 bytes) to be requested from the I2C bus
  4. Close the "Elements" editor

Step 13: In Visuino: Add Split Structure Component, and Add Analog Channel to It

The I2C data will arrive from the Slave Arduino as floating point binary data.We need to decode it properly. For this we need a “Split Structure” component with 1 “Analog” elements in it.

  1. Type "struct" in the Filter box of the Component Toolbox then select the "Split Structure" component (Picture 1), and drop it in the design area
  2. Click on the "Tools" button of the SplitStructure1 component (Picture 2) to open the Elements editor (Picture 3)
  3. In the "Elements" editor select the “Analog” element, and then click on the "+" button (Picture 3) to add Analog element (Picture 4)
  4. Close the "Elements" editor

Step 14: In Visuino: Connect the Split Structure Component

  1. Connect the "Out" output pin of the "Operations.Request1" element of the I2CDevice1 component to the "In" input pin of the SplitStructure1 component (Picture 1)
  2. Connect the "Out" output pin of the "Elements.Analog1" element of the SplitStructure1 component to the "In" input pin of the "Serial[ 0 ]" channel of the Arduino component (Picture 2)

Step 15: In Visuino: Add and Connect Clock Generator Component

Next we need to specify when the request for data will be sent. In my case I decided to request the data once a second. For this I used a Clock Generator with its default frequency of one clock per second:

  1. Type "clock" in the Filter box of the Component Toolbox then select the "Clock Generator" component (Picture 1), and drop it in the design area
  2. Connect the "Out" pin of the ClockGenerator1 component to the "Clock" input pin of the I2CDevice1 component (Picture 2)

Step 16: Generate, Compile, and Upload the Arduino Code for the I2C Master Arduino

Picture 1 shows the complete Visuino diagram of the Master I2C Arduino project.

  1. Connect the Master Arduino Nano to the computer
  2. In Visuino, Press F9 or click on the button shown on Picture 2 to generate the Arduino code, and open the Arduino IDE
  3. In the Arduino IDE, click on the Upload button, to compile and upload the code (Picture 3)

Step 17: And Play...

Congratulations! You have completed the project.

Picture 1 shows the connected and powered up project. In my case I used a USB Power Bank to power the Slave controller so it is visible that only the Master controller is connected with USB to the computer, but you can power both Arduinos with USB cable from the computer.

If you connect with a Serial Terminal you will see the distance measured with the Ultrasonic Ranger printed once a second (Picture 2)

You can connect with Visuino too by selecting the serial port, and clicking on the Connect button (Picture 3). You should see the distance printed once a second (Picture 4), and if you switch to the Scope view, you will see the distance plotted over time (Picture 5)

Also attached are the Visuino projects, that I created for this Tutorial. You can download and open them in Visuino: https://www.visuino.com

About This Instructable

5,284views

110favorites

License:

More by BoianM:Assemble Kuman Wi-Fi Robot With Camera and Manipulator Arduino and Visuino: Long Distance Remote Light Sensor With RFM95W/RFM98W Makerfabs LoRa Shields  Arduino Nano and Visuino: Animate the Colors of RGB LED 
Add instructable to: