Arduino due + usb keyboard

Hello everyone,

I am working on a project that uses a normal USB keyboard as an input. I have Arduino due, and I installed the latest Arduino IDE. I downloaded the needed settings, and libraries. my plan initially is to write a simple code that will:

  • take an input from the keyboard, and show the pressed button in the serial monitor:
    example: if I press an " K" in the keyboard the serial monitor shows "K".

My hardware setup:

  • keyboard connected to the native USB port of Arduino due
  • Arduino due is connected to the PC using the programming port

This is the code that I am trying, it compile error free, and it upload in the Due error free. however the code does not show anything in the serial monitor except for "Program started".

---------------CODE-----------------------

// Require keyboard control library
#include <KeyboardController.h>

// Initialize USB Controller

USBHost usb;

// Attach keyboard controller to USB

KeyboardController keyboard(usb);

// This function intercepts key press
void keyPressed() {

  Serial.print("Pressed:  ");

  printKey();
}

// This function intercepts key release
void keyReleased() {

  Serial.print("Released: ");

  printKey();
}

void printKey() {

  // getOemKey() returns the OEM-code associated with the key

  Serial.print(" key:");

  Serial.print(keyboard.getOemKey());

  // getModifiers() returns a bits field with the modifiers-keys

  int mod = keyboard.getModifiers();

  Serial.print(" mod:");

  Serial.print(mod);

  Serial.print(" => ");

  if (mod & LeftCtrl)

    Serial.print("L-Ctrl ");

  if (mod & LeftShift)

    Serial.print("L-Shift ");

  if (mod & Alt)

    Serial.print("Alt ");

  if (mod & LeftCmd)

    Serial.print("L-Cmd ");

  if (mod & RightCtrl)

    Serial.print("R-Ctrl ");

  if (mod & RightShift)

    Serial.print("R-Shift ");

  if (mod & AltGr)

    Serial.print("AltGr ");

  if (mod & RightCmd)

    Serial.print("R-Cmd ");

  // getKey() returns the ASCII translation of OEM key

  // combined with modifiers.

  Serial.write(keyboard.getKey());

  Serial.println();
}

void setup()
{

  Serial.begin(115200);

  Serial.println("Program started");

  delay(200);
}

void loop()
{

  // Process USB tasks

  usb.Task();
}

Are you using a USB micro OTG to USB host cable? I am not sure what else can go wrong.

is USB micro OTG to USB host cable, different from a genaric Micro USB adapter? I am useing a small adapter peace that let you connect a USB male to a Micro USB

Welcome to the forum.

Your sketch is working fine. I used a OTG to USB Host cable and a Logitech wireless dongle and keyboard.

Program started
Pressed:   key:4 mod:2 => L-Shift A
Released:  key:4 mod:2 => L-Shift A
Pressed:   key:21 mod:1 => L-Ctrl r
Released:  key:21 mod:1 => L-Ctrl r
Pressed:   key:7 mod:4 => Alt d
Released:  key:7 mod:4 => Alt d
Pressed:   key:24 mod:64 => AltGr u
Released:  key:24 mod:64 => AltGr u
Pressed:   key:12 mod:16 => R-Ctrl i
Released:  key:12 mod:16 => R-Ctrl i
Pressed:   key:17 mod:32 => R-Shift n
Released:  key:17 mod:32 => R-Shift n
Pressed:   key:18 mod:0 => o
Released:  key:18 mod:0 => o

Thank you Klaus_K,

could you please explain to me why are using the
Logitech wireless dongle and keyboard, I am using a wired keyboard ( genaric one from amazon) connected to the Arduino due through a (genaric USB to micro USB adapter), Do you think the problem is with my hardware? I appreciate your help

Sure, I use many Logitech devices and they are all wireless (proprietary Logitech protocol). They come with a USB dongle.

Do you have a link to the adapter you are using?

Sounds like it.
You could test the adapter and keyboard with an Android phone or tablet.

Just in case:

  • my laptop is connected to the Arduino Due programming port (next to the power plug)
  • the dongle is connected via the OTG cable on the USB port next to the reset button

I just order one of the adapter you told me about This is the link to the adapter that I am using now.

-https://www.amazon.com/dp/B00KILQLKE?ref=ppx_pop_mob_ap_share

there is some progress after I used the adapter you recommended, now it give me some outputs but they seem random. none of the other keys work after the first 3 sec of running the sketch, no matter how many keys I press in the keyboard, I do not get any outputs any more.

Also here are some pictures that might give you guys an Idea of my set up. please let me know if you see something off.

this is the code that I am running:

// Require keyboard control library
#include <KeyboardController.h>

// Initialize USB Controller

USBHost usb;

// Attach keyboard controller to USB

KeyboardController keyboard(usb);

// This function intercepts key press
void keyPressed() {

Serial.print("Pressed: ");

printKey();
}

// This function intercepts key release
void keyReleased() {

Serial.print("Released: ");

printKey();
}

void printKey() {

// getOemKey() returns the OEM-code associated with the key

Serial.print(" key:");

Serial.print(keyboard.getOemKey());

// getModifiers() returns a bits field with the modifiers-keys

int mod = keyboard.getModifiers();

Serial.print(" mod:");

Serial.print(mod);

Serial.print(" => ");

if (mod & LeftCtrl)

Serial.print("L-Ctrl ");

if (mod & LeftShift)

Serial.print("L-Shift ");

if (mod & Alt)

Serial.print("Alt ");

if (mod & LeftCmd)

Serial.print("L-Cmd ");

if (mod & RightCtrl)

Serial.print("R-Ctrl ");

if (mod & RightShift)

Serial.print("R-Shift ");

if (mod & AltGr)

Serial.print("AltGr ");

if (mod & RightCmd)

Serial.print("R-Cmd ");

// getKey() returns the ASCII translation of OEM key

// combined with modifiers.

Serial.write(keyboard.getKey());

Serial.println();
}

void setup()
{

Serial.begin(115200);

Serial.println("Program started");

delay(200);
}

void loop()
{

// Process USB tasks

usb.Task();
}


What do you guys think ?

I tested this again and your sketch runs without issues. I can run with my finger over the keyboard and the Serial Monitor keeps printing like crazy.

I suspect it is your keyboard. I do not believe there is something wrong with your keyboard, but this could be a power issue. I measured the USB voltage where the keyboard is connected. It is as expected lower than 5V (~ 4.7 - 4.8 V). It’s the 5V from your PC minus the drop across the MOSFET that switches the power.
When I unplug the USB dongle the voltage increases by 70mV. This shows the voltage is susceptible to loads. Maybe your keyboard requires more current than my USB dongle. My keyboard is powered by a battery (it is wireless). Yours needs to get power from the USB. Maybe when you press a couple of keys the voltage drops below the value where the keyboard controller works properly.