[SOLVED] and some hints Using Seeeduino XIAO as a keyboard-emulator

Hi everybody,

I have successfully installed Seeeduino XIAO support for the Arduino-IDE 1.18.13
and the basic blink-program
works

void PrintFileNameDateTime() {
  Serial.println("Code running comes from file ");
  Serial.println(__FILE__);
  Serial.print("  compiled ");
  Serial.print(__DATE__);
  Serial.print(" ");
  Serial.println(__TIME__);  
}

boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long
const byte    OnBoard_LED = LED_BUILTIN;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();

}

void loop() {
  BlinkHeartBeatLED(OnBoard_LED,100);

  if ( TimePeriodIsOver(MyTestTimer,1000) ) {
    Serial.println("Hello World");
  }  
}

Onboard LED blinks serial output works.

Now next step I tried the USB-Keyboard-emulation with this simple code

#include "keyboard.h"


boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();
  Keyboard.begin();
}


void loop() {
  if ( TimePeriodIsOver(MyTestTimer,1000) ) {
   Keyboard.print( F"Hi I'm the keyboard-emulator") );
  }  

}

but I get the error-message

keyboard.h: No such file or directory

I'm using the Arduino-IDE 1.8.13 in a portable version.
Here is the complete compiler-output

Arduino: 1.8.13 (Windows 10), TD: 1.53, Board: "Seeeduino XIAO, Arduino, Off"

F:\MyPortable-PRgs\arduino1.8.13\arduino-builder -dump-prefs -logger=machine -hardware F:\MyPortable-PRgs\arduino1.8.13\hardware -hardware F:\MyPortable-PRgs\arduino1.8.13\portable\packages -hardware F:\MyPortable-PRgs\arduino1.8.13\portable\sketchbook\hardware -tools F:\MyPortable-PRgs\arduino1.8.13\tools-builder -tools F:\MyPortable-PRgs\arduino1.8.13\hardware\tools\avr -tools F:\MyPortable-PRgs\arduino1.8.13\portable\packages -libraries F:\MyPortable-PRgs\arduino1.8.13\portable\sketchbook\libraries -fqbn=Seeeduino:samd:seeed_XIAO_m0:usbstack=arduino,debug=off -vid-pid=2886_802F -ide-version=10813 -build-path C:\Users\dipl-\AppData\Local\Temp\arduino_build_886788 -warnings=all -build-cache C:\Users\dipl-\AppData\Local\Temp\arduino_cache_659107 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arm-none-eabi-gcc.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\arm-none-eabi-gcc\7-2017q4 -prefs=runtime.tools.arm-none-eabi-gcc-7-2017q4.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\arm-none-eabi-gcc\7-2017q4 -prefs=runtime.tools.bossac.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\bossac\1.7.0-arduino3 -prefs=runtime.tools.bossac-1.8.0-48-gb176eee.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\bossac\1.8.0-48-gb176eee -prefs=runtime.tools.CMSIS.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\CMSIS\5.4.0 -prefs=runtime.tools.CMSIS-5.4.0.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\CMSIS\5.4.0 -prefs=runtime.tools.openocd.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\arduino\tools\openocd\0.10.0-arduino7 -prefs=runtime.tools.openocd-0.10.0-arduino7.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\arduino\tools\openocd\0.10.0-arduino7 -prefs=runtime.tools.arduinoOTA.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\arduinoOTA\1.2.1 -prefs=runtime.tools.arduinoOTA-1.2.1.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\arduinoOTA\1.2.1 -prefs=runtime.tools.bossac-1.7.0-arduino3.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\bossac\1.7.0-arduino3 -prefs=runtime.tools.CMSIS-Atmel.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\CMSIS-Atmel\1.2.1 -prefs=runtime.tools.CMSIS-Atmel-1.2.1.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\CMSIS-Atmel\1.2.1 -verbose F:\MyPortable-PRgs\arduino1.8.13\portable\sketchbook\Seeeduino-XIAO-Keyboard-Emulator-000-reduced\Seeeduino-XIAO-Keyboard-Emulator-000-reduced.ino

F:\MyPortable-PRgs\arduino1.8.13\arduino-builder -compile -logger=machine -hardware F:\MyPortable-PRgs\arduino1.8.13\hardware -hardware F:\MyPortable-PRgs\arduino1.8.13\portable\packages -hardware F:\MyPortable-PRgs\arduino1.8.13\portable\sketchbook\hardware -tools F:\MyPortable-PRgs\arduino1.8.13\tools-builder -tools F:\MyPortable-PRgs\arduino1.8.13\hardware\tools\avr -tools F:\MyPortable-PRgs\arduino1.8.13\portable\packages -libraries F:\MyPortable-PRgs\arduino1.8.13\portable\sketchbook\libraries -fqbn=Seeeduino:samd:seeed_XIAO_m0:usbstack=arduino,debug=off -vid-pid=2886_802F -ide-version=10813 -build-path C:\Users\dipl-\AppData\Local\Temp\arduino_build_886788 -warnings=all -build-cache C:\Users\dipl-\AppData\Local\Temp\arduino_cache_659107 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arm-none-eabi-gcc.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\arm-none-eabi-gcc\7-2017q4 -prefs=runtime.tools.arm-none-eabi-gcc-7-2017q4.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\arm-none-eabi-gcc\7-2017q4 -prefs=runtime.tools.bossac.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\bossac\1.7.0-arduino3 -prefs=runtime.tools.bossac-1.8.0-48-gb176eee.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\bossac\1.8.0-48-gb176eee -prefs=runtime.tools.CMSIS.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\CMSIS\5.4.0 -prefs=runtime.tools.CMSIS-5.4.0.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\CMSIS\5.4.0 -prefs=runtime.tools.openocd.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\arduino\tools\openocd\0.10.0-arduino7 -prefs=runtime.tools.openocd-0.10.0-arduino7.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\arduino\tools\openocd\0.10.0-arduino7 -prefs=runtime.tools.arduinoOTA.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\arduinoOTA\1.2.1 -prefs=runtime.tools.arduinoOTA-1.2.1.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\arduinoOTA\1.2.1 -prefs=runtime.tools.bossac-1.7.0-arduino3.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\bossac\1.7.0-arduino3 -prefs=runtime.tools.CMSIS-Atmel.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\CMSIS-Atmel\1.2.1 -prefs=runtime.tools.CMSIS-Atmel-1.2.1.path=F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\tools\CMSIS-Atmel\1.2.1 -verbose F:\MyPortable-PRgs\arduino1.8.13\portable\sketchbook\Seeeduino-XIAO-Keyboard-Emulator-000-reduced\Seeeduino-XIAO-Keyboard-Emulator-000-reduced.ino

Using board 'seeed_XIAO_m0' from platform in folder: F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\hardware\samd\1.8.1

Using core 'arduino' from platform in folder: F:\MyPortable-PRgs\arduino1.8.13\portable\packages\Seeeduino\hardware\samd\1.8.1

Detecting libraries used...

"F:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\tools\\arm-none-eabi-gcc\\7-2017q4/bin/arm-none-eabi-g++" -mcpu=cortex-m0plus -mthumb -c -g -Os -w -std=gnu++14 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions "-D__SKETCH_NAME__=\"\"\"Seeeduino-XIAO-Keyboard-Emulator-000-reduced.ino\"\"\"" -w -x c++ -E -CC -DF_CPU=48000000L -DARDUINO=10813 -DARDUINO_SEEED_XIAO_M0 -DARDUINO_ARCH_SAMD -DARDUINO_SAMD_ZERO -D__SAMD21__ -D__SAMD21G18A__ -DARM_MATH_CM0PLUS -DSEEED_XIAO_M0 -DUSB_VID=0x2886 -DUSB_PID=0x802F -DUSBCON -DUSB_CONFIG_POWER=100 "-DUSB_MANUFACTURER=\"Seeed\"" "-DUSB_PRODUCT=\"Seeed XIAO M0\"" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\hardware\\samd\\1.8.1\\cores\\arduino/TinyUSB" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\hardware\\samd\\1.8.1\\cores\\arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\hardware\\samd\\1.8.1\\cores\\arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore/tinyusb/src" -DARDUINO_SAMD_ZERO -D__SAMD21__ -D__SAMD21G18A__ -DARM_MATH_CM0PLUS -DSEEED_XIAO_M0 -DUSB_VID=0x2886 -DUSB_PID=0x802F -DUSBCON -DUSB_CONFIG_POWER=100 "-DUSB_MANUFACTURER=\"Seeed\"" "-DUSB_PRODUCT=\"Seeed XIAO M0\"" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\hardware\\samd\\1.8.1\\cores\\arduino/TinyUSB" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\hardware\\samd\\1.8.1\\cores\\arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\hardware\\samd\\1.8.1\\cores\\arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore/tinyusb/src" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\tools\\CMSIS\\5.4.0/CMSIS/Core/Include/" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\tools\\CMSIS\\5.4.0/CMSIS/DSP/Include/" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\tools\\CMSIS-Atmel\\1.2.1/CMSIS-Atmel/CMSIS/Device/ATMEL/" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\hardware\\samd\\1.8.1\\cores\\arduino" "-IF:\\MyPortable-PRgs\\arduino1.8.13\\portable\\packages\\Seeeduino\\hardware\\samd\\1.8.1\\variants\\XIAO_m0" "C:\\Users\\dipl-\\AppData\\Local\\Temp\\arduino_build_886788\\sketch\\Seeeduino-XIAO-Keyboard-Emulator-000-reduced.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE

Alternatives for keyboard.h: []

Seeeduino-XIAO-Keyboard-Emulator-000-reduced:1:10: fatal error: keyboard.h: No such file or directory

ResolveLibrary(keyboard.h) #include "keyboard.h"



  -> candidates: []~~~

compilation terminated.
exit status 1
keyboard.h: No such file or directory

So I thought simple thing do a google-search with the error-message
No results for Seeeduino XIAO
Then searched for Seeeduino XIAO USB-keyboard-emulation.
Just found a microPython-example. No C++-example

Now I'm stumped. I guess I need some basic understanding how the SAMD-Arm-M0-cortex-microcontroller work.

Or maybe I just have too much XIAOs peeking into my eyes that I can't see what has to be done to make it work.
I want the XIAO to act as an USB-keyboard which is connected to a PC

XIAO------>-----------PC-USB-Port

any hints are greatly appreciated
best regards Stefan

Dunno if this is helpful

a7

On the Arduino Leonardo/Micro it is spelled "Keyboard.h" with a capital 'K'.

Hi Alto777,

I tried the link and a demo-code from the adafruit tiny USB-library.
After installing with the library-manager including all dependencies
I just got a error-message

cannot declare variable 'usb_hid' to be of abstract type 'Adafruit_USBD_HID'

If somebody can shine a light on what that means in general I will learn something new.

Anyway using Seeeduino XIAO seems to be a hard way to create an USB-Keyboard-emulator.

@Seeeduino.cc:
I'm giving it a chance. But if I encounter more trouble after solving this issue
I will say "Ciao" to XIAO

best regards Stefan

Hi John,

good catch. That made it work.
Though print a whole sentence like

    Keyboard.print("Hi I'm the keyboard-emulator");

once per second is bad idea as it did not only write into a texteditor but started some extrawindows I guess through misinterpreted keypresses

thank you very much John.

best regards Stefan

So as the XIAO acts as a USB-keyboard using anykind of repeated
Keyboard.print is a bad idea too

because as soon as you plugin the microcontroller it starts typing
(doh ! Yes of course)
So it is much better to have a button and the Keyboard.print prints only if button is pressed. And in case of connecting the button wrong ad a counter that limits the number of Keyboard.prints

Or use a real big delay of 30 seconds to have enough time to change from
Arduino-IDE to a textwindow.

Hm but with a button to switch on off Keyboard.print one application is
to let the Seeeduino type into the send-function of the serial monitor of a second arduino-IDE observing a different board as a quick and dirty "scripting" inside the serial monitor.

This code did work and as it does print the word "test" only once every 30 seconds and only two times you have a chance to upload again without messing up the sourcecode.

//Test
#include "Keyboard.h"

boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long

int count = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  Keyboard.begin();
}

void loop() {

  if (count < 3) {
    if ( TimePeriodIsOver(MyTestTimer,30000) ) {
      Keyboard.print("Test");
      count++;
    }  
  }  
}

The better way is to have a button connected
another hint is to setup the USB-stack with the right type
When using the Keyboard.h-library the Arduino-IDE must be adjusted to use the
Arduino-USB-stack

best regards Stefan