Multiple microcontrollers communication, I2C, UART or other?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
sermann
 
Posts: 18
Joined: Wed Jul 08, 2020 5:24 am

Multiple microcontrollers communication, I2C, UART or other?

Post by sermann »

Hi,

I am working on a mediumly complex art project that will require that multiple microcontrollers collaborate. Currently thinking about a PyPortal for the control user interface, a couple of Feather Express M4 to drive neo-pixels strings and a few I2C sensors, one or two Matrix Portal M4 for, well, LED matrixes and maybe a Circuit Playground Express. Probably 7-8 I2C sensors that will be connected to the Feathers to detect presence, movement and participant's height.

Need the controllers to be loosely connected, with messages flowing from the main controller (the PyPortal, probably) to the other devices, and messages from the other devices to the main controller. The controllers (and sensors) will be in relative close proximity, but there will be 2m cable runs between some controllers. Plan on programming in CircuitPython.

Thinking how these microcontrollers will communicate with each other, I am thinking of two alternatives:
  • one UART TX/RX loop linking all microcontrollers in one loop. I2C sensors on one or two I2C buses attached to the Feather Express M4s
  • or one I2C bus driven by the PyPortal, where the I2C devices and the other microcontrollers are set up as devices
  • or one I2C bus that links the microcontrollers and one or two independant I2C busses for the devices
Wondering if any of you had built something similar, if you had any issues and how you solved them?

Should I expect any issues with using both the UART and one or two I2C busses on the Feather Express M4s? Should I expect issues with running multiple I2C busses on a Feather Express M4 (one as device and one or two as host)?

Thoughts, suggestions and ideas are welcome!

User avatar
bludin
 
Posts: 97
Joined: Thu Apr 16, 2020 8:57 am

Re: Multiple microcontrollers communication, I2C, UART or ot

Post by bludin »

I'm planning a somewhat similar setup with two SAMD51 microcontrollers. Like you, I want to us a Pyportal for the UI and a self-made SAMD51J20 board to control some hardware via I2C, SPI and PWM.

I decided to use UART because it's buffered so the communication can be asynchronous. Moreover, in my preliminary tests with an itsybitsy M4 talking to itself, it can go up to 3MBaud which is significantly faster than I2C.

I have an application with 4 I2C buses, UART and SPI (Dotstar) running on a SAMD51G19. No problems there.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Multiple microcontrollers communication, I2C, UART or ot

Post by adafruit_support_bill »

2 meter cable runs is really pushing it for i2c communication. You would probably need to use active terminators for reliable operation: https://www.adafruit.com/product/4756
It is also a bit less flexible than basic UART serial. I'd probably go with the UART for the higher-level communication and just use i2c for your sensors.

User avatar
sermann
 
Posts: 18
Joined: Wed Jul 08, 2020 5:24 am

Re: Multiple microcontrollers communication, I2C, UART or ot

Post by sermann »

Thank you both for your suggestions.

Will go with an UART loop between the controllers and local I2C buses for the sensors.

Plan on building a simple prototype tonight with maybe a Circuit Playground and two Feathers M4 Express in an UART loop and see how reliably messages flow.

Are you, or anyone else, interested in me documenting the results?

Lastly, would you be aware of an existing UART loop networking library?

Take care, stay safe,

Stephen

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: Multiple microcontrollers communication, I2C, UART or ot

Post by tannewt »

If you don't need a lot of data, you could consider BLE as well. BLE advertising is a pretty simple way of broadcasting data between devices.

User avatar
sermann
 
Posts: 18
Joined: Wed Jul 08, 2020 5:24 am

Re: Multiple microcontrollers communication, I2C, UART or ot

Post by sermann »

tannewt wrote:If you don't need a lot of data, you could consider BLE as well.
You are absolutely right, but I don't think that the PyPortal and Adafruit Matrix Portal support BLE.

I did consider setting up a Wifi hotspot for the project, but see that more as a plan C or D.

User avatar
aob003
 
Posts: 4
Joined: Tue Apr 13, 2021 11:15 pm

Re: Multiple microcontrollers communication, I2C, UART or ot

Post by aob003 »

I'd be keen to understand how you managed to get the UART working on the Pyportal?

I assume you can use the JST-4 connector, how do you change from I2C to UART?

Thanks.

User avatar
bludin
 
Posts: 97
Joined: Thu Apr 16, 2020 8:57 am

Re: Multiple microcontrollers communication, I2C, UART or ot

Post by bludin »

aob003 wrote:I'd be keen to understand how you managed to get the UART working on the Pyportal?
I used the D3 and the D4 lines on the JST-3 connectors.
Cheers,
Beat

User avatar
aob003
 
Posts: 4
Joined: Tue Apr 13, 2021 11:15 pm

Re: Multiple microcontrollers communication, I2C, UART or ot

Post by aob003 »

Thanks Beat, I haven't done this before. Any chance you can post some code or a link to where I can read more?

User avatar
sermann
 
Posts: 18
Joined: Wed Jul 08, 2020 5:24 am

Re: Multiple microcontrollers communication, I2C, UART or ot

Post by sermann »

Thanks Beat,

Dear aob003,

My project got postponed due to the COVID lockdowns, but I did manage to get communication running among multiple notes using the UARTs.

I did manage to build a test case with 3 notes wired in a loop: node 1 transmits to node 2, which transmits to node 3, which transmits to node 1, using the UART.

The code below is for node #1, the PyPortal. It receives messages from node #2 on pin D4 and sends messages to node #3 on pin D3.

The messages themselves are a packed structure ended with an "\n" with:
- From (integer)
- To (integer)
- Type (6 char array)
- Value (float)

Hope this code helps:

Code: Select all

import board
import busio
import struct

# initialization
# Initialize the serial port for communication with other devices
# Receive from node 2 on pin D4 and send to node 3 on pin D3
uart = busio.UART(board.D3, board.D4, baudrate=9600)

# Main loop
while True:
    # is there incoming data in the UART buffer?
    if uart.in_waiting:
        # read a full line
        dataLine = uart.readline(32)
        if dataLine:
            try:
                msgData = struct.unpack("BB6sfx",dataLine)
            except RuntimeError as Err:
                print("[E] Runtime error when unpacking message")
                print("    >{}< {}".format(dataLine,len(dataLine)))
                pass
            # break into individual fields
            msgTo, msgFrom, msgType, msgValue = msgData
            # convert chararray into string
            msgType = ''.join([chr(b) for b in msgData[2]])
            if msgTo == 1:
                # is this message for me?
                print("[I] Message from {} for me, type: {}, value: {}".format(msgFrom, msgType, msgValue))
            else:
                # if not, pass to the next node
                uart.write(dataLine)
                print("[I] passing message to next node")

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”