I need to read two USB devices

Hi
I want to read signals from a joystick and a rudder pedals set, both USB HID devices.
I know there are some USB host shields but they allow me to read only one device.
Should I use a 2-Port USB Hub and connect it to the USB shield?
Thanks

yes you can use an USB HUB but make sure it is a powered hub

see this video at 5:45

this document may be of relevance as well HID Usage Tables for Universal Serial Bus (USB)

maybe worth looking at the ESP32-S3 File>Examples>USB>CompositeDevice

thinking again rather than device you probably need OTG Host
have a look at EspHostExamplesHID

might help to know what board you are using?

I can use a MEGA or an UNO.

I doubt if a UNO or Mega would support two USB OTG Host devices

the ESP32-S3 will - e.g.


// ESP32-S3 USB OTG Host CompositeHID example

// library https://github.com/tanakamasayuki/EspUsbHost/tree/master

// file>Examples>CEspUsbHost>HID>EspUsbHostCompositeHID

#include "EspUsbHost.h"

EspUsbHost usb;

static int32_t posX = 0;
static int32_t posY = 0;

static void onKeyboard(const EspUsbHostKeyboardEvent &event)
{
  if (!event.pressed)
  {
    return;
  }

  Serial.printf("keyboard: address=%u iface=%u keycode=0x%02x",
                event.address,
                event.interfaceNumber,
                event.keycode);

  if (event.ascii == '\r')
  {
    Serial.print(" ascii=Enter");
  }
  else if (event.ascii >= 0x20 && event.ascii != 0x7f)
  {
    Serial.printf(" ascii='%c'", (char)event.ascii);
  }

  Serial.println();
}

static void onMouse(const EspUsbHostMouseEvent &event)
{
  if (event.buttonsChanged)
  {
    uint8_t pressed = event.buttons & ~event.previousButtons;
    uint8_t released = event.previousButtons & ~event.buttons;
    if (pressed & ESP_USB_HOST_MOUSE_LEFT)
      Serial.printf("mouse: address=%u iface=%u left press\n", event.address, event.interfaceNumber);
    if (released & ESP_USB_HOST_MOUSE_LEFT)
      Serial.printf("mouse: address=%u iface=%u left release\n", event.address, event.interfaceNumber);
    if (pressed & ESP_USB_HOST_MOUSE_RIGHT)
      Serial.printf("mouse: address=%u iface=%u right press\n", event.address, event.interfaceNumber);
    if (released & ESP_USB_HOST_MOUSE_RIGHT)
      Serial.printf("mouse: address=%u iface=%u right release\n", event.address, event.interfaceNumber);
    if (pressed & ESP_USB_HOST_MOUSE_MIDDLE)
      Serial.printf("mouse: address=%u iface=%u middle press\n", event.address, event.interfaceNumber);
    if (released & ESP_USB_HOST_MOUSE_MIDDLE)
      Serial.printf("mouse: address=%u iface=%u middle release\n", event.address, event.interfaceNumber);
  }

  if (event.moved)
  {
    posX += event.x;
    posY += event.y;
    Serial.printf("mouse: address=%u iface=%u pos: x=%ld y=%ld delta: dx=%d dy=%d wheel=%d\n",
                  event.address,
                  event.interfaceNumber,
                  (long)posX,
                  (long)posY,
                  (int)event.x,
                  (int)event.y,
                  (int)event.wheel);
  }
}

static void onConsumerControl(const EspUsbHostConsumerControlEvent &event)
{
  Serial.printf("consumer: address=%u iface=%u %s usage=0x%04x %s\n",
                event.address,
                event.interfaceNumber,
                event.pressed ? "press" : "release",
                event.usage,
                espUsbHostConsumerControlUsageName(event.usage));
}

static void onSystemControl(const EspUsbHostSystemControlEvent &event)
{
  Serial.printf("system: address=%u iface=%u %s usage=0x%02x %s\n",
                event.address,
                event.interfaceNumber,
                event.pressed ? "press" : "release",
                event.usage,
                espUsbHostSystemControlUsageName(event.usage));
}

void setup()
{
  Serial.begin(115200);
  delay(2500);

  Serial.println("EspUsbHost multiple HID example start");

  usb.setKeyboardLayout(ESP_USB_HOST_KEYBOARD_LAYOUT_JA_JP);

  usb.onDeviceConnected([](const EspUsbHostDeviceInfo &device)
                        {
                          Serial.print("connected: ");
                          espUsbHostPrint(device); });

  usb.onDeviceDisconnected([](const EspUsbHostDeviceInfo &device)
                           {
                             Serial.print("disconnected: ");
                             espUsbHostPrint(device); });

  usb.onKeyboard(onKeyboard);
  usb.onMouse(onMouse);
  usb.onConsumerControl(onConsumerControl);
  usb.onSystemControl(onSystemControl);

  if (!usb.begin())
  {
    Serial.printf("usb.begin() failed: %s\n", usb.lastErrorName());
  }
}

void loop()
{
  delay(1);
}

serial monitor output connecting a USB Hub, keyboard and two mice

EspUsbHost multiple HID example start
connected: device: address=1 portId=0x01 vid=05e3 pid=0608 class=0x09(Hub) speed=full-speed product="USB2.0 Hub"
connected: device: address=2 portId=0x14 vid=413c pid=2113 class=0x00(per-interface) speed=low-speed product="Dell KB216 Wired Keyboard"
keyboard: address=2 iface=0 keycode=0x04 ascii='a'
keyboard: address=2 iface=0 keycode=0x05 ascii='b'
keyboard: address=2 iface=0 keycode=0x06 ascii='c'
keyboard: address=2 iface=0 keycode=0x07 ascii='d'
keyboard: address=2 iface=0 keycode=0x08 ascii='e'
keyboard: address=2 iface=0 keycode=0x09 ascii='f'
connected: device: address=3 portId=0x12 vid=045e pid=00a4 class=0x00(per-interface) speed=low-speed product="Microsoft(R) Compact Optical Mouse"
mouse: address=3 iface=0 pos: x=8 y=-11 delta: dx=8 dy=-11 wheel=0
mouse: address=3 iface=0 pos: x=9 y=-13 delta: dx=1 dy=-2 wheel=0
mouse: address=3 iface=0 pos: x=11 y=-15 delta: dx=2 dy=-2 wheel=0
mouse: address=3 iface=0 pos: x=12 y=-16 delta: dx=1 dy=-1 wheel=0
mouse: address=3 iface=0 pos: x=14 y=-17 delta: dx=2 dy=-1 wheel=0
mouse: address=3 iface=0 pos: x=15 y=-18 delta: dx=1 dy=-1 wheel=0
mouse: address=3 iface=0 pos: x=16 y=-18 delta: dx=1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=16 y=-17 delta: dx=0 dy=1 wheel=0
mouse: address=3 iface=0 right press
mouse: address=3 iface=0 pos: x=-118 y=-56 delta: dx=1 dy=1 wheel=0
mouse: address=3 iface=0 right release
mouse: address=3 iface=0 pos: x=-117 y=-56 delta: dx=1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=-116 y=-56 delta: dx=1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=-115 y=-56 delta: dx=1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=-114 y=-56 delta: dx=1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=-113 y=-56 delta: dx=1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=-108 y=-53 delta: dx=1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=-107 y=-53 delta: dx=1 dy=0 wheel=0
mouse: address=3 iface=0 left press
mouse: address=3 iface=0 pos: x=-106 y=-52 delta: dx=1 dy=1 wheel=0
mouse: address=3 iface=0 pos: x=-105 y=-52 delta: dx=1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=-104 y=-51 delta: dx=1 dy=1 wheel=0
mouse: address=3 iface=0 left release
mouse: address=3 iface=0 pos: x=-105 y=-52 delta: dx=-1 dy=-1 wheel=0
mouse: address=3 iface=0 pos: x=-106 y=-52 delta: dx=-1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=-136 y=-49 delta: dx=-1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=-136 y=-49 delta: dx=0 dy=0 wheel=1
mouse: address=3 iface=0 pos: x=-137 y=-50 delta: dx=-1 dy=-1 wheel=0
mouse: address=3 iface=0 pos: x=-137 y=-50 delta: dx=0 dy=0 wheel=1
mouse: address=3 iface=0 pos: x=-136 y=-49 delta: dx=1 dy=1 wheel=0
mouse: address=3 iface=0 pos: x=-135 y=-49 delta: dx=1 dy=0 wheel=0
connected: device: address=6 portId=0x11 vid=192f pid=0416 class=0x00(per-interface) speed=low-speed product="USB Optical Mouse"
mouse: address=3 iface=0 pos: x=554 y=-63 delta: dx=-2 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=550 y=-65 delta: dx=0 dy=1 wheel=0
mouse: address=3 iface=0 pos: x=550 y=-64 delta: dx=0 dy=1 wheel=0
mouse: address=3 iface=0 pos: x=549 y=-64 delta: dx=-1 dy=0 wheel=0
mouse: address=3 iface=0 pos: x=550 y=-64 delta: dx=1 dy=0 wheel=0
disconnected: device: address=6 portId=0x11 vid=192f pid=0416 class=0x00(per-interface) speed=low-speed product="USB Optical Mouse"
mouse: address=7 iface=0 pos: x=637 y=110 delta: dx=0 dy=1 wheel=0
mouse: address=7 iface=0 pos: x=638 y=110 delta: dx=1 dy=0 wheel=0
disconnected: device: address=7 portId=0x12 vid=045e pid=00a4 class=0x00(per-interface) speed=low-speed product="Microsoft(R) Compact Optical Mouse"

why wouldn't they?

it's all in how one parses the data coming in the USB

that's why I linked the document that shows the whos and whats of the USB world

I should have said
I doubt if a UNO or Mega would support two USB OTG Host devices concurrently
the answer is to experiment
try connecting two USB HID devices to a UNO/Mega and USB Host shield and see if it works

You may be able to do it on the UNO, consider an external serial module connected to a TTL to USB adapter. This is a desperation suggestion, not a solution. Your data throughput will also be limited.

A Mega would be a better choice as it has a total of 4 hardware serial ports. Using one of those along with a USB to TTL converter should work.

Since you did not specify the application or data rates this may not work, Regardless you will be data rate limited. Your software will also have a big impact on your throughput.

A LONG time ago, I played some with some of the USB Host shields. Some of them were more reliable than others. One of them I tried was either this one or a previous generation one by Sparkfun
SparkFun USB-C Host Shield - SparkFun Electronics

There are also several of them available from Amazon.com, like:

Amazon.com: HiLetgo USB Host Shield for Arduino UNO Support Google Android ADK USB HUB : Electronics

I had a couple of cheap ones that I had difficulty making them work, but some of them are probably fine.

I was using the library:
felis/USB_Host_Shield_2.0: Revision 2.0 of USB Host Library for Arduino.

The main times I played with this was to compare things between this and the USB Host library and support for the Teensy 3.6 and 4.x.
If you look at the Teensy 4.1 product page, you will see where there are pins setup for USB Host:
Teensy® 4.1
using the host cable:
PJRC Store
Note: PJRC stuff (Teensy) is now sold by Sparkfun.

Back to the USB Shield and library. The library support multiple USB devices, which will require a USB Hub. In the library I mentioned, there is an example for a mouse and keyboard:
USB_Host_Shield_2.0/examples/HID/USBHIDBootKbdAndMouse/USBHIDBootKbdAndMouse.ino at master · felis/USB_Host_Shield_2.0

Note: I don't know the current state of this library. At one point I believe there were some that were working on a new version of the USB Host Shield library code, but I have not followed it, so no idea if it now exists or the state of it.

Hope that helps.
Kurt

So far I just want to see it working with just one joystick. I'm using the Mega and a Host Usb Adk Shield. I noticed that if the shield is installed, I can't upload any sketch, an error message appears:
Error: timeout
Error: timeout
Error: timeout
Error: timeout
Error: timeout
Error: timeout
Error: timeout communicating with programmer
Error: stk500v2_getsync() failed; try -x delay=n with some n in [-80, 100]
Error: unable to open port COM10 for programmer wiring
Error: unable to open port COM10 for programmer wiring

so I upload a sketch that I asked to GPT to monitor the output, and then install the shield, but no output. Is there any valid sketch that works? I need to see the output while moving the axis.

I guess I have to power the USB device because I connected the rudder pedals that have a LED and it doesn't turn on.

USB devices (keyboard, mouse, etc) need 5V power
e.g. the ESP32-S3-DevKitC-1 has a link on the back of the PCB which enables the USB power to a device
The USB Host shield must have some means of providing 5V to a USB device
do you have a manual or a schematic for the USB Host shield?