USB Host on MKR1010

Hello,

I want to use a keyboard (with USB connector) with my Arduino MKR1010.

According to the datasheet, this board has a 'Full-Speed USB Device and embedded Host' so, this should be realisable.

I found two libraries :

The first one is compatible only with Arduino DUE and the second is for the USB Host Shield.

However, I've found nothing about how to use the USB Host port on the MKR1010...

Do you know if a library for USB Host on MKR1010 exists?
Do you know if it's possible to modify one of those two libraries to adapt it to the mkr1010?

Thanks for your help!

There is a USBHost library that comes with the Arduino SAMD (32-bits ARM Cortex-M0+) Boards platform you installed to add support to the Arduino IDE for your MKR WiFi 1010. No modifications are needed to use it with your MKR WiFi 1010.

You will find an example that demonstrates its use as a USB keyboard host under File > Examples > USBHost > KeyboardController (ignore the comment about "Native USB port on an Arduino Due Board".

This example was written with the Arduino Zero in mind. The difference between the Arduino Zero and the MKR 1010 is the Arduino Zero has a "programming port" and a "native USB port". This means you can connect the Zero to your computer via the "programming port", and the keyboard to the "native USB port" (via an OTG adapter), and be able to view the example's serial output on your Serial Monitor.

On the MKR WiFi 1010, the situation is a bit different because you only have the one USB socket on the board, which is the equivalent of the Zero's "native USB port". So you need to connect the keyboard to the USB socket. If you want to see the serial output of the example sketch, you'll need to connect a USB to serial adapter to pin 14 (Serial1 TX) on the MKR WiFi 1010 and add this line to the example sketch:

#define SERIAL_PORT_MONITOR Serial1

Of course, the USBHost library itself has no need for this Serial1 connection. That's just to get some output from the example sketch to tell you the communication with the keyboard is working. You could write your own sketch that uses the keyboard for other amazing purposes that have no need for serial output. That said, it is nice to have that serial connection even if it's just for some debug output during development.

Another thing to note is that you need to power the MKR WiFi 1010 via the VIN pin with a 5 V power supply. This is needed to provide power to the keyboard. If you power the MKR WiFi 1010 via the VCC pin or battery connector, then the keyboard won't get power, and so won't work.

Thank you for all those interesting details!

I haven’t think to use the serial pin to debug output, I plan to use a simple screen OLED in I2C and manipulate connection each time… Your solution is smarter!

The next step of my project is to connect a barcode reader to my mkr1010.
Can I install drivers on my board to connect and use a barcode reader?

hello guys.

I’m working with MKRWIFI1010 and Usbhost connected to a barcode reader.
And I’m using the USBHost librairy provided by Arduino.

My issue is :

  • With old barcode reader (Zebra DS9808 and other), my code works fine.
  • With new barcode reader (Zebra DS9908), my code doesn’t works. The reader is OFF, no wake up
  • With a real keyboard, the code works fine

See the code, very simple :

#define SERIAL_PORT_MONITOR Serial1
#include <KeyboardController.h> 
USBHost usb;
KeyboardController keyboard(usb);

void setup(){
  Serial1.begin(9600);
  while (!SERIAL_PORT_MONITOR); 
  Serial1.println("Program started...");
}

void loop(){    usb.Task();   }

void keyPressed() { // This function intercepts key press   
  Serial1.println(keyboard.getKey());
}

Hardware :

  • Power 5.1V/1A
  • tension on USB cable : 5 V / then no problem with power

Functionnality :

  • All barcode readers work on my PC like a keyboard, no issue with windows.
  • With old one : The program start, wait few second, the barcode reader beep and after I can scan a bar code : well
  • With new one : The program start, wait X minutes, the barcode reader don’t power ON, no beep
  • The configuration is the same : HID keyboard
  • I checked the Class=3 and subport = 1 / same on each barcode reader

Now my assumption is the USBHost librairy and the HID protocol.

Please, could you bring me some links or ideas to resolve my issue ?

  • what’s the command to wake up the reader ?
  • How to debug this part of librairy ?
  • Is the HID norm changed ?
  • I read on the forum that some HID device don’t provide a proper “profil”. What’s means ?
  • USB description is good on each bar code reader.
  • What kind of question can I send to the supplier to help me ?

Thanks’ to help me, I’m working many days on this topic.
My only idea is to change USB to serial. But it’s not a modern communication and I have to add an other Power module.

Hi guys,

I want to use a barcode scanner (with USB connector) with my Arduino MKR1000.
I have connected the barcode scanner using usb OTG cable and a power supply.

I'm running the Keyboard example ( File > Examples > USBHost > KeyboardController )
and I get the following(below) Error:

Can someone advise?

Arduino: 1.8.13 (Windows 10), Board: "Arduino MKR1000"

WARNING: library USBHost claims to run on sam architecture(s) and may be incompatible with your current board which runs on samd architecture(s).

That warning does indicate a potential problem.

Please do this:

  • Click on the black console pane at the bottom of the Arduino IDE window.
  • Press Ctrl+A to select all the text.
  • Press Ctrl+C to copy the selected text to the clipboard.
  • In a forum reply here, click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum’s code tags markup to your reply.
  • Press “Ctrl + V”. This will paste the copied output between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.

If the text exceeds the forum’s 9000 character limit, save it to a .txt file and post it as an attachment. If you click the “Reply” button here, you will see an “Attachments and other settings” link that will allow you to make the attachment.

That output might provide some information that we can use to help you.

WARNING: library USBHost claims to run on sam architecture(s) and may be incompatible with your current board which runs on samd architecture(s).
In file included from C:\Users\User\Documents\libraries\USBHost\src/Usb.h:27:0,
                 from C:\Users\User\Documents\libraries\USBHost\src/hidboot.h:23,
                 from C:\Users\User\Documents\libraries\USBHost\src/KeyboardController.h:22,
                 from C:\Users\User\Documents\firmware\arduino_testers\BarcodeScannerController\BarcodeScannerController.ino:3:
C:\Users\User\Documents\firmware\libraries\USBHost\src/address.h:102:8: error: using typedef-name 'UsbDevice' after 'struct'
 struct UsbDevice
        ^~~~~~~~~
In file included from C:\Users\User\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.2.0/CMSIS/Device/ATMEL/samd21/include/samd21g18a.h:270:0,
                 from C:\Users\User\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.2.0/CMSIS/Device/ATMEL/samd21/include/samd21.h:69,
                 from C:\Users\User\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.2.0/CMSIS/Device/ATMEL/samd.h:105,
                 from C:\Users\User\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.2.0/CMSIS/Device/ATMEL/sam.h:540,
                 from C:\Users\User\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.9\cores\arduino/Arduino.h:48,
                 from sketch\BarcodeScannerController.ino.cpp:1:
C:\Users\User\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.2.0/CMSIS/Device/ATMEL/samd21/include/component/usb.h:1745:3: note: 'UsbDevice' has a previous declaration here
 } UsbDevice;
   ^~~~~~~~~
In file included from C:\Users\User\Documents\firmware\libraries\USBHost\src/KeyboardController.h:22:0,
                 from C:\Users\User\Documents\firmware\arduino_testers\BarcodeScannerController\BarcodeScannerController.ino:3:
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h: In member function 'virtual uint32_t HIDBoot<BOOT_PROTOCOL>::Init(uint32_t, uint32_t, uint32_t)':
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:280:10: error: 'struct UsbDevice' has no member named 'epinfo'
  if (!p->epinfo)
          ^~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:287:17: error: 'struct UsbDevice' has no member named 'epinfo'
  oldep_ptr = p->epinfo;
                 ^~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:290:5: error: 'struct UsbDevice' has no member named 'epinfo'
  p->epinfo = epInfo;
     ^~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:292:5: error: 'struct UsbDevice' has no member named 'lowspeed'
  p->lowspeed = lowspeed;
     ^~~~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:303:6: error: 'struct UsbDevice' has no member named 'epinfo'
   p->epinfo = oldep_ptr;
      ^~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:309:5: error: 'struct UsbDevice' has no member named 'epinfo'
  p->epinfo = oldep_ptr;
     ^~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:325:6: error: 'struct UsbDevice' has no member named 'lowspeed'
   p->lowspeed = false;
      ^~~~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:334:5: error: 'struct UsbDevice' has no member named 'lowspeed'
  p->lowspeed = false;
     ^~~~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:341:5: error: 'struct UsbDevice' has no member named 'lowspeed'
  p->lowspeed = lowspeed;
     ^~~~~~~~
In file included from C:\Users\User\Documents\firmware\libraries\USBHost\src/KeyboardController.h:22:0,
                 from C:\Users\User\Documents\firmware\FW\arduino_testers\BarcodeScannerController\BarcodeScannerController.ino:3:
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h: In member function 'virtual void HIDBoot<BOOT_PROTOCOL>::EndpointXtract(uint32_t, uint32_t, uint32_t, uint32_t, const USB_ENDPOINT_DESCRIPTOR*)':
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:474:62: error: 'UOTGHS_HSTPIPCFG_PTYPE_INTRPT' was not declared in this scope
   pipe = UHD_Pipe_Alloc(bAddress, epInfo[index].deviceEpNum, UOTGHS_HSTPIPCFG_PTYPE_INTRPT, UOTGHS_HSTPIPCFG_PTOKEN_IN, epInfo[index].maxPktSize, 10, UOTGHS_HSTPIPCFG_PBK_1_BANK);
                                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:474:62: note: suggested alternative: 'HSTPIPCFG_PTYPE_INTRPT'
   pipe = UHD_Pipe_Alloc(bAddress, epInfo[index].deviceEpNum, UOTGHS_HSTPIPCFG_PTYPE_INTRPT, UOTGHS_HSTPIPCFG_PTOKEN_IN, epInfo[index].maxPktSize, 10, UOTGHS_HSTPIPCFG_PBK_1_BANK);
                                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                              HSTPIPCFG_PTYPE_INTRPT
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:474:93: error: 'UOTGHS_HSTPIPCFG_PTOKEN_IN' was not declared in this scope
   pipe = UHD_Pipe_Alloc(bAddress, epInfo[index].deviceEpNum, UOTGHS_HSTPIPCFG_PTYPE_INTRPT, UOTGHS_HSTPIPCFG_PTOKEN_IN, epInfo[index].maxPktSize, 10, UOTGHS_HSTPIPCFG_PBK_1_BANK);
                                                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:474:93: note: suggested alternative: 'HSTPIPCFG_PTOKEN_IN'
   pipe = UHD_Pipe_Alloc(bAddress, epInfo[index].deviceEpNum, UOTGHS_HSTPIPCFG_PTYPE_INTRPT, UOTGHS_HSTPIPCFG_PTOKEN_IN, epInfo[index].maxPktSize, 10, UOTGHS_HSTPIPCFG_PBK_1_BANK);
                                                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~
                                                                                             HSTPIPCFG_PTOKEN_IN
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:474:151: error: 'UOTGHS_HSTPIPCFG_PBK_1_BANK' was not declared in this scope
   pipe = UHD_Pipe_Alloc(bAddress, epInfo[index].deviceEpNum, UOTGHS_HSTPIPCFG_PTYPE_INTRPT, UOTGHS_HSTPIPCFG_PTOKEN_IN, epInfo[index].maxPktSize, 10, UOTGHS_HSTPIPCFG_PBK_1_BANK);
                                                                                                                                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:474:151: note: suggested alternative: 'HSTPIPCFG_PBK_1_BANK'
   pipe = UHD_Pipe_Alloc(bAddress, epInfo[index].deviceEpNum, UOTGHS_HSTPIPCFG_PTYPE_INTRPT, UOTGHS_HSTPIPCFG_PTOKEN_IN, epInfo[index].maxPktSize, 10, UOTGHS_HSTPIPCFG_PBK_1_BANK);
                                                                                                                                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                                                                                                                       HSTPIPCFG_PBK_1_BANK
In file included from C:\Users\User\Documents\firmware\libraries\USBHost\src/Usb.h:27:0,
                 from C:\Users\User\Documents\firmware\libraries\USBHost\src/hidboot.h:23,
                 from C:\Users\User\Documents\firmware\libraries\USBHost\src/KeyboardController.h:22,
                 from C:\Users\User\Documents\arduino_testers\BarcodeScannerController\BarcodeScannerController.ino:3:
C:\Users\User\Documents\firmware\libraries\USBHost\src/address.h: In instantiation of 'void AddressPoolImpl<MAX_DEVICES_ALLOWED>::ForEachUsbDevice(UsbDeviceHandleFunc) [with long unsigned int MAX_DEVICES_ALLOWED = 16; UsbDeviceHandleFunc = void (*)(UsbDevice*)]':
C:\Users\User\Documents\firmware\libraries\USBHost\src/Usb.h:193:35:   required from here
C:\Users\User\Documents\firmware\libraries\USBHost\src/address.h:290:19: error: 'struct UsbDevice' has no member named 'address'
    if (thePool[i].address)
        ~~~~~~~~~~~^~~~~~~
exit status 1
Error compiling for board Arduino MKR1000.

This is very strange. There is a USBHost library written specifically for the SAMD architecture of the MKR 1000, which is located at:
C:\Users\User\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.9\libraries\USBHost

That library should be given preference over the SAM architecture one you have installed at:
C:\Users\User\Documents\firmware\libraries\USBHost
I can't think of why the SAM one would be given preference.

Please try moving the C:\Users\User\Documents\firmware\libraries\USBHost folder somewhere else (or delete it if you don't have any use for it) and then try compiling your sketch again.