you're reading...

Arduino EV3 UART Emulation Library

The previous article explained how to create LEGO Mindstorms EV3 UART sensors using an Arduino. There is now an Arduino library called EV3UARTEmulation that makes wrting the Arduino program a lot simpler. For the Nose sensor, the program is now:

#include <SoftwareSerial.h>
#include <EV3UARTEmulation.h>

EV3UARTEmulation sensor(10, 11, 99, 38400); // rx pin, tx pin, type, bit rate

void setup() {
  // Single short value with 3 decimal digits
  sensor.create_mode("NOSE", true, DATA16, 1, 3, 0); 

unsigned long last_reading = 0;

void loop() {
  if (millis() - last_reading > 100) {
    sensor.send_data16(0, analogRead(0)); // Read MQ-2 every 100 milliseconds
    last_reading = millis();


16 thoughts on “Arduino EV3 UART Emulation Library

  1. Hi Lawrie,

    What kind of sensor is the gas sensor, analogue, serial, I2C. Does the library work with all these different protocols?

    Posted by Aswin | 2014/06/05, 18:13
    • Hi Aswin, Only just noticed this comment. The gas sensor is analog, but the library does not care – it just does the communication with the EV3. If your look at the sketch it reads the analog value from the MQ-2 and uses send_data16 to send the value to the EV3. Lawrie

      Posted by Geek Grandad | 2014/08/13, 07:58
  2. -maybe its a stupid question, but how (what pins) are the Arduino and and EV3 connected with

    Posted by philipjuhl | 2014/08/12, 22:19
    • The TX and RX pins are pins 5 and 6 which need to be connected to the Arduino RX and TX pins. GND and the 5v supply come from pins 3 and 4. Pin 1 needs to be connected to GND for the EV3 to recognize the sensor as a UART sensor.

      Posted by Geek Grandad | 2014/08/13, 08:05
      • Thanks, I finally got it to work. Didnt realize that the Arduino Library EV3UartEmulation assigns the RX/TX pins, so I used the “normal” RX/TX pins.
        Thanks for the help.

        Posted by philipjuhl | 2014/08/16, 08:03
  3. Next question 🙂
    When I look at the “API” it seems that I can call switchMode(mode, delay) from LejOS to the UART sensor. But I cant seem to get it to work. My Arduino sensor (Using EV3UartEmulation).
    My EV3 program throws an exception when I try to create the UartSensor.

    ——————–Arduino code———————

    void setup() {
    sensor.create_mode(“TEST”, true, DATA16, 1, 3, 0);
    sensor.create_mode(“GYRO”, true, DATA16, 1, 3, 0);

    void loop() {
    if (millis() – last_reading > 100) {

    if(sensor.get_current_mode() == TEST_MODE)
    sensor.send_data16(0, TEST_VALUE);
    sensor.send_data16(0, GYRO_VALUE);

    if (data > 99) data = 0;
    last_reading = millis();

    —————–EV3 LEJOS code——————-

    public class ArduinoSensor extends UARTSensor implements SensorMode{
    public ArduinoSensor(Port port) {

    public enum ArduinoMode {
    Test(0), Gyro(1);

    private int value;
    private ArduinoMode(int value) {
    this.value = value;

    public int getValue() {
    return value;

    public void SetMode(ArduinoMode mode) {
    this.switchMode(mode.getValue(), 100);

    Posted by philipjuhl | 2014/08/17, 08:23
    • There does see to be a problem switching modes. I don’t think I had tested it. I am looking into it. I also found a bug in send_data_16, which I will fix when I have mode switching working.

      Out of interest, what type of sensor are you building? Is it a Gyro sensor?

      I have been experimenting with a few cheap sensors off ebay, using an Sparkfun Arduino Pro Micro and a Dexter Industries breadboard adapter. I will write another article on it soon.

      I have a version of the library that uses hardware serial rather than software serial as that works better with some Arduinos including the Pro Micro – I will probably add it to the repository. It does not seem easy to write a single Arduino library that uses either software or hardware serial.

      Posted by Geek Grandad | 2014/08/19, 19:00
      • I have been trying to build a Gyro sensor with the MPU6050. First I tried to use I2C,communication, but I could not get the programming of the DMP to work.
        So I now use and Arduino (Nano) as a bridge between the MPU6050 and the EV3.
        Is there a documentation of the UART protocol (the messages that passes between EV3 and the UART sensor). ?

        Posted by philipjuhl | 2014/08/19, 19:08
    • Try setting the first parameter of send_data16 to sensor.get_current_mode(). I think that will fix your problem. I don’t think the send_data methods should have a mode parameter as the mode must always be the current mode. The Lego kernel uart module (which leJOS uses, with some modifications), rejects data that is not for the current mode and eventually times out.

      I will let you know when I have committed a new version of the library.

      Posted by Geek Grandad | 2014/08/19, 20:16
      • There is documentation of the protocol in the comments at http://sourceforge.net/p/lejos/ev3sdcard/ci/master/tree/modules/lms2012/d_uart/Linuxmod_AM1808/d_uart.c, but I think it is a little out of date. In particular the clock sync at the start no longer happens.

        If you edit EV3UARTEmulation.h to uncomment the define of DEBUG, add a Serial.begin(baud-rate) to your sketch and connect the USB cable, you can see the diagnostics in the Arduino console, which shows the messages.

        I have just bought a MPU-6050 and will try it. It will take a while to arrive from Hong Kong. It should work via i2c from the EV3. What is DMP?

        Posted by Geek Grandad | 2014/08/19, 20:40
    • I have updated the library in the Git repository to remove the mode parameter from the send data commands, and fixed a couple of bugs. Multiple modes now work. There is a more complete example in the UART Compass Sensor post, which includes multiple modes and multiple data items. It also includes the EV3 Java code.

      Posted by Geek Grandad | 2014/08/25, 18:02
  4. Hi
    Thanks for the link to the documentaion for the protocol.

    The DMP is a Digital Motion Processor, that can be programmed from the I2C Master (MPU6050 is setup as an IC2 slave).
    You can read my experience with the MPU6050 from my blog. (http://buildingrobot.wordpress.com/2014/08/16/connection-mpu6050-gyro-with-ev3-mindstorm/)

    Posted by philipjuhl | 2014/08/22, 21:32
  5. I’m really struggling here. All the samples and examples I try and compile in the Arduino keep failing saying #include can not be found. I first extracted the EV3_Dexter_Industries_Sensors.zip file to my \Documents\Arduino\libraries that failed to find the EV3UARTEmulation.h. So I move all the sub folders to the libraries folder. I just got my Arduino so working with that is new to me. I did test some and successfully communicate over I2C with my EV3 and Arduino using the CONNECTING THE EV3 AND THE ARDUINO found at http://www.dexterindustries.com/

    Current Arduino software is 1.6.0
    Window 7 Home
    Using Lejos 0.9.0

    Posted by Corey | 2015/04/22, 21:06
  6. Hi,
    I tried to follow the instructions, but it just doesn’t seem to work.
    I hooked everything up on the Arduino Uno as explained.
    (Blue EV3 cable connected to Arduino Pin0, yellow to Pin1, red to GND.)
    Init is done by
    EV3UARTEmulation sensor(1, 0, 99, 38400);
    (I also tried in vain EV3UARTEmulation sensor(0, 1, 99, 38400);)
    Starting the protocol (with DEBUG defined), the Arduino console outputs cyclically

    Cmd: 40 63 DC
    Cmd: 49 0 0 B6
    Cmd: 52 0 96 0 0 3B
    Cmd: 90 0 54 45 53 54 79
    Cmd: 90 80 1 1 2 0 ED
    Sending ACK

    Starting then Nose on the EV3 leads to an “Invalid Sensor Mode” exception,
    upon execution of the first instruction
    NoseSensor nose = new NoseSensor(SensorPort.S1);

    I.e., the EV3 just doesn’t recognize the Arduino.
    Is there anything I can do?
    Help would be greatly appreciated.

    Posted by Holger Schlingloff | 2016/02/18, 17:54
    • Its quite a while since I worked on this. I will have a look t it, tomorrow.

      Posted by Lawrie Griffiths | 2016/02/18, 19:32
    • I don’t think using pins 0 and 1 on the Arduino will work as they are the hardware uart pins that are used by USB and are displaying the diagnostics. The library uses software serial, so you can use other digital pins. My example uses pins 10 and 11. I would try those.

      Posted by Lawrie Griffiths | 2016/02/18, 20:04

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: