you're reading...

Connecting to an Arduino with I2C

2014-05-17 15.37.05

There are lots of tiny format Arduinos available that are good for implementing custom sensors or motor controls for your EV3 (or NXT).

It is very easy to connect your NXT to an Arduino using I2C.  I used half a NXT cable, some terminal strip and a couple of 82k resistors. The cable is one of Mindsensors flexible cable as they are easier to cut up. The 82k pull-up resistors go between the power feed (green wire; wire 4)  and the two I2C wires (wires =5, and 6; yellow and blue).

There is a good article from Dexter Industries on this, that uses their breadboard connector and a NXT.

Here is an EV3 program that requests data from the Arduino:

import lejos.hardware.Button;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.I2CSensor;

public class Main {
   static int I2CSlaveAddress = 8;
   static byte[] buffReadResponse = new byte[8];
   public static void main(String[] args) {    
      System.out.println("Arduino Connection Test");
      I2CSensor arduino = new I2CSensor(SensorPort.S1, I2CSlaveAddress);
      while (Button.ESCAPE.isUp()) {
    	  int id = Button.waitForAnyPress();  
    	  if (id == Button.ID_ENTER) {  
    		  arduino.getData('A', buffReadResponse, buffReadResponse.length);
    		  System.out.println(new String(buffReadResponse));

Note that addresses are 8-but on the EV3, and 7-bit on the Arduino, so that address 8 on the EV3 corresponds to address 4 on the Arduino.

Here is a very simple Arduino sketch to process the request:

#include <Wire.h>

#define SLAVE_ADDRESS 0x04
void setup() 

byte request;
byte reply[] = "Testing";

void loop(){

void receiveData(int byteCount){
  request = Wire.read();

void sendData(){
  if (request = 'A') Wire.write(reply,sizeof reply);

You can connect sensors or actuators to your Arduino and drive them this way. The EV3 can power the Arduino so you can remove the USB power cable when you are not downloading sketches or debugging,



2 thoughts on “Connecting to an Arduino with I2C

  1. Thanks for this nice demonstration. It works perfectly! Here two remarks:

    – Do not use the VIN pin to power the Arduino from the EV3. If you plug an external power-supply to the Arduino while this line is still in use, it may destroy the EV3 because VIN gets the external supply voltage that may exceed 5V
    – Change the line request = ‘A’ to request == ‘A’, because in C assigments are always true.


    Posted by Aegidius Pluess | 2015/04/29, 17:24
  2. @Aegidus best to change to ‘A’ == request – assignments are true if the value assigned is non-zero (which is the case here). If you get the operator wrong, as in the original post, the “reversed” comparison will throw a compile-time error because you can’t assign to a constant.

    Posted by me | 2016/01/09, 23:14

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: