Connecting EV3 and Arduino
Sometimes, when creating robots, it becomes necessary to use several different platforms in one project, for example, EV3 and Arduino. So you can use the strengths of each of the platforms and parallelize the tasks performed. I suggest you read the translation of the article, which describes how to connect EV3 and Arduino and how to use Arduino with EV3.
In the example, we are using a breadboard. You can use an adapter to connect the NXT to the breadboard so you don’t damage the NXT cable in your kit. The adapter can be ordered on the manufacturer’s website here or on Amazon.
In this tutorial, we are using Arduino Uno and LEGO Mindstorms EV3 to read and write data. We will also demonstrate how to use the Arduino to read analog values and send them to EV3. You can easily modify our example to send and receive any data to EV3, from accelerometer readings to Geiger counter values. This is probably the fastest way to add your own sensors and gadgets: when you connect EV3 and Arduino, the whole world is yours!
Communication between EV3 and Arduino can be done via I2C. In this tutorial, we will set up the Arduino as an I2C slave (i.e. slave) and EV3 as an I2C master (i.e. master).
In the example, we will show you how to send commands to the Arduino from EV3, and how to write a program that will request data from the Arduino.
The I2C bus is called a “two-wire interface” because it uses two wires for communication. One wire transmits clocks: this ensures that both devices send information at the same rate. The clock line is usually called “SCL” (short for Serial CLock). The second wire is used for data transmission (data is sent and received by both devices at the same time), and is usually called “SDA” (short for Serial DAta). The master (always EV3) is always in control of the connection, and always sets the clock for both devices. The controlled device is usually called a slave. When the master sends commands, the slave drops everything and listens. When the master asks for information, the slave gives that information.
In the example, we do not use pull-up resistors on the I2C lines. However, if I2C doesn’t work at all, you can use 47K ohm pull-up resistors on the SDA and SCL lines to pull up I2C to 4.7V on the VCC line.
First, here’s the schema we’re aiming for. The left side of the schematic shows the EV3 plug, while the right side shows the Arduino. We need to connect GND, VCC, SDA and SCL.
The connected wires will look like this: EV3 -> Development Board -> Arduino. The picture below shows what this connection looks like.
In the example, we will only connect the SDA and SCL lines (for communication), VCC to power the Arduino from EV3, and the ground (GND) line for voltage equalization.
Connecting to Arduino
First, connect a wire to the GND port of the Arduino. Then connect one wire to A5 (SCL, clock line) and one to A4 (SDA, data line). In the picture you can see that we have connected the red wire to VCC, the black wire to GND, the yellow wire to SDA (A4) and the green wire to SCL (A5).
Connecting to EV3
The easiest way to connect an EV3 is to use an adapter. All colors go to their respective pins: the yellow wire in the example is connected to SDA on the breadboard. Same for SCL, GND and VCC.
For clarity: SDA –> SDA, SCL –> SCL.
You can make your own adapter very easily (although you will ruin the wire): cut off one end of the standard EV3 black wire and strip off the insulation. SDA and SCL are yellow and blue wires, ground is black and red, VCC is green.
In this picture, the colors correspond to the following lines:
- Black: earth
- Red: VCC
- Green: SCL
- Yellow: SDA
On the Arduino side, we will connect SDA, SCL, VCC and ground to the Arduino headers.
The software for pairing EV3 and Arduino consists of two parts. The first part is an Arduino sketch, the second part is a program for the LEGO MINDSTORMS EV3 environment with blocks installed that send and receive data to/from the Arduino. All code can be downloaded and used from the Github EV3 repository.
Setting Up an Arduino for I2C Operation: Assigning an Address
In the example, we need to set up a few things to go further. We need to select the address of the slave. In the example, we set the slave address to 0x04 (in hexadecimal).
Arduino as a slave sender/receiver
In our example, the Arduino will receive data from the master. EV3 just says “here’s some data” and Arduino says “thanks for the data”. The setup is very simple and most of the sensors work as slave receivers and senders. This example can be very helpful if you are setting up your Arduino to control a motor or LED.
EV3 sends some data to the Arduino, which receives it and outputs it to the Serial Monitor. If EV3 requests some data, the Arduino sends one byte in response. You can easily modify this code to send or receive any number of bytes. And here is an example for receiving and sending 8 bytes.
Here the function setup() initializes the serial port and specifies which functions will be called when sending and receiving data from the EV3.
Function receiveData() used to receive data from EV3, and sendData() – to send data to EV3. Upload this Arduino sketch to use with the EV3 software.
EV3 like a master
Setting up EV3 as a master is very simple. Download the Dexter Industries EV3 blocks (Dexter.ev3b) and import them into the Lego Mindstorms EV3 Software (Tools -> Block Import Wizard). Use block Dexter Industries I2C to interact with Arduino.
To read a single byte, simply create the following sequence of blocks with “Read 1 Byte“. Write in the field “addr» address of the slave, the same as in the sketch for Arduino (in our case 0x04) and run the program to view the data coming from the Arduino on the EV3 screen.
To send a single byte, change the block mode to “Write 1 Byte” and enter the byte you want to send to the Arduino. When you run the program, EV3 will send the data to the Arduino and the Arduino will display it on the serial port terminal.
Similarly, you can upload an 8-byte read / write sketch to the Arduino and use the “8 byte Read/Write“.
Sometimes it’s very useful to capture analog data on the Arduino and send it to the EV3. If you want to do this, then upload this sketch to the Arduino, and in the EV3 program, select the mode in the block as “Analog Read Block“. Then specify the pin number from which you want to read data and run the program. After that, analog data will come to EV3.
That’s all about connecting EV3 and Arduino.
Translation: Alexey Valuev
The original article is here.