LMS-ESP32 and LEGO EV3

Anton

Updated on:

lms-esp32 and ev3

LMS-ESP32 is a microcontroller board that acts as an intermediary between non-LEGO electronics and the LEGO MINDSTORMS bricks – LMS is short for LEGO MINDSTORMS. Think of it as a translator who helps two people speak different languages to understand each other perfectly. The EV3 brick is pretty versatile and speaks UART, i2c and analog in. It is, however, a 5V unit and can’t communicate with 3V3 I2C devices or hobby servos. Those are cases when translation is relevant. In this article, you’ll read how to connect EV3 and LMS-ESP32 for adding custom electronics to your EV3.

Why Integrate ESP32 with EV3?

Integrating ESP32 with your EV3 brick brings several benefits. You can connect a whole new range of sensors and electronics to your EV3 hub. This connectivity transforms what you can achieve with your EV3 projects, turning simple robots into smart, interconnected systems. While the ESP32 offers Wi-Fi and Bluetooth, the EV3 has a USB port where you can easily add a BT5 or Wi-Fi dongle.

Soldering a wire

The LMS-ESP32 does not have an EV3 socket. You have to solder a wire or use an adaptor – more on those later. Soldering a wire is probably the fastest and cheapest way to connect your LMS-ESP32. A breakout EV3 wire is super handy in general. You could also connect a HuskyLens with that wire.

I cut an EV3 wire in half and then connected DuPont Arduino jumper wires to them. The colors are a bit confusing! Red is GND and green is 4v7.

EV3 wire colors

Take great care when connecting an EV3 hub to the LMS-ESP32. A wiring mistake can fry your LMS-ESP32, I know that by experience. The EV3 brick is pretty solid, I wasn’t able to fry one of those (yet).

EV3 connected to LMS-ESP32 with jumper wires

EV3 adaptor boards for LMS-ESP32

We’ve handed out some limited edition adaptor boards to make the same connection without soldering. Using this board, you can also fry your LMS-ESP32 if you connect it wrong. We’re sorry to have made it extra complicated with the V2 board. Some design consideration led us to switch around pins. This means you have to use the adaptor boards differently on LMS-ESP32v1.0 and LMS-ESP32v2.0 (with USB-C)

Correct adaptor board placements on LMS-ESP32 v1.0 (left) and v2.0 (right)

SerialTalk for communication between EV3 and LMS-ESP32

SerialTalk is the library of choice for facilitating the communication between EV3 and LMS-ESP32. You can send and receive sensor data in near real-time. You can use the library with Pybricks-MicroPython in VS Code with the MINDSTORMS Extension enabled. Read the SerialTalk documentation on our documentation site, or explore the example project below.

EV3 sensor emulation with LMS-ESP32

If you would rather not use ev3dev and Pybricks, it is electrically possible for an LMS-ESP32 to emulate the i2c and UART sensors for EV3. I just don’t have the time to reverse engineer it all, and write the libraries. Some help would be welcome here. Sensor emulation would benefit almost any MCU running MicroPython.

Example project: A simple communication demo with LMS-ESP32 over a sensor port.

This Pybricks program changes the color of the LED using the buttons of the EV3. It also demonstrates the ‘echo’ call for testing communication. The full code and description is on GitHub.

#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Stop, Direction, Button, Color
from serialtalk.auto import SerialTalk

ev3 = EV3Brick()
ev3.speaker.beep()

st = SerialTalk(Port.S1)

print(st.call("echo", "Hello from ev3!"))

hue = 0

while 1:
    st.call("led", "i", hue)
    if Button.LEFT in ev3.buttons.pressed():
        hue += 10
    if Button.RIGHT in ev3.buttons.pressed():
        hue -= 10
    hue %= 359

Alternative means of connecting an LMS-ESP32 if you would rather not use up a sensor port

USB Connection

The LMS-ESP32 has a USB port, and so has the EV3 brick. With a USB-A to USB-C wire, you can connect them both. Using SerialTalk will be hard, however. The LMS-ESP32 uses USB Serial for REPL and you would have to give direct MicroPython commands to the LMS-ESP32 board. This works, but it can hang communication if you give a wrong command. More information about REPL is in the MicroPython docs. Be sure to read the part about Raw REPL: that is probably the way to go.

Reading serial from within Python3 on ev3dev is easy, but motor drivers are lousy. Motor drivers are better in Pybricks-MicroPython, but then serial communication is hard. To use Pybricks, set the baud rate in a terminal stty -F /dev/ttyUSB0 115200. Then you can open the /dev/ttyUSB0 file and use uselect. I don’t know how you read and write to the same file.

import uselect
p = uselect.Poll()
infile = open('/dev/ttyUSB0','rb')
p.register(infile, uselect.POLLIN)
if p.poll(0):
  infile.read(1)

On most of My EV3 bricks I have a wifi dongle, which uses up the USB port. You would have to add an USB hub or program the EV3 over the other usb port instead of WiFi

MQTT and Wi-Fi for sensor readings

Both EV3 and LMS-ESP32 can be on the same Wi-Fi. You could therefore set up communication via MQTT, which is fast and efficient. I haven’t tried that, but I’m looking forward to your projects.

Go and Hack EV3!

As you read in this article, there are plenty of possibilities for connecting LMS-ESP32 (or any MCU) to LEGO MINDSTORMS EV3. It’s up to you to choose the way that fits your project best. I’d love to see your projects, so please share them on social media and send me a note. You can also join our Discord server and discuss your project there!

Like this article? Help us make more!

Your support matters

We appreciate your support on Patreon and YouTube! Small things count.

Become a Patron

Don't miss a thing

Subscribe below to get an email when we publish a new article.

Share this with someone who needs to know

Leave a Reply

Item added to cart.
0 items - 0.00