2
\$\begingroup\$

I am doing a project that requires me to control both a mouse/keyboard combination and a serial port to activate it. (I know, it can prevent me from using my computer, etc. but I have precautions already in place.) However, can the Arduino Leonardo be two devices (or technically three) at once without any additional hardware? I know Arduino has example on how to use both the keyboard and mouse at once, but is it possible for the Leonardo to compute the "triathlon of serial communications?"

I know there is one output that goes to the USB and one that can go to the TX/RX, so I could technically buy a serial to USB converter and hook it up for the serial side, but that can get pricey and hog two USB ports instead of one (very valuable on a laptop and a hub is not economical when you add shipping and the price for the UART module, and it is more fun figuring this out.

\$\endgroup\$

3 Answers 3

4
\$\begingroup\$

It looks like the keyboard and mouse example on the arduino site already has a method for reading the serial port.



/*
  KeyboardAndMouseControl

 Controls the mouse from five pushbuttons on an Arduino Leonardo or Micro.

 Hardware:
 * 5 pushbuttons attached to D2, D3, D4, D5, D6


 The mouse movement is always relative. This sketch reads 
 four pushbuttons, and uses them to set the movement of the mouse.

 WARNING:  When you use the Mouse.move() command, the Arduino takes
 over your mouse!  Make sure you have control before you use the mouse commands.

 created 15 Mar 2012
 modified 27 Mar 2012
 by Tom Igoe

 this code is in the public domain

 */

// set pin numbers for the five buttons:
const int upButton = 2;     
const int downButton = 3;        
const int leftButton = 4;
const int rightButton = 5;
const int mouseButton = 6;

void setup() { // initialize the buttons' inputs:
  pinMode(upButton, INPUT);       
  pinMode(downButton, INPUT);       
  pinMode(leftButton, INPUT);       
  pinMode(rightButton, INPUT);       
  pinMode(mouseButton, INPUT);

  Serial.begin(9600);
  // initialize mouse control:
  Mouse.begin();
  Keyboard.begin();
}

void loop() {

right here:


  // use serial input to control the mouse:
  if (Serial.available() > 0) {
    char inChar = Serial.read();

    switch (inChar) {   
    case 'u':
      // move mouse up
      Mouse.move(0, -40);
      break; 
    case 'd':
      // move mouse down
      Mouse.move(0, 40);
      break;
    case 'l':
      // move mouse left
      Mouse.move(-40, 0);
      break;
    case 'r':
      // move mouse right
      Mouse.move(40, 0);
      break;
    case 'm':
      // perform mouse left click
      Mouse.click(MOUSE_LEFT);
      break;
    }
  }

  // use the pushbuttons to control the keyboard:
  if (digitalRead(upButton) == HIGH) {
    Keyboard.write('u'); 
  }
  if (digitalRead(downButton) == HIGH) {
    Keyboard.write('d'); 
  }
  if (digitalRead(leftButton) == HIGH) {
    Keyboard.write('l'); 
  }
  if (digitalRead(rightButton) == HIGH) {
    Keyboard.write('r'); 
  }
  if (digitalRead(mouseButton) == HIGH) {
    Keyboard.write('m'); 
  }

}


If you can read the serial port, it seems like you can write to it too. I don't have a leonardo board to test this out myself, but inside of the loop() function you should be able to add something like


Serial.print("hello world");

and verify this while the Mouse and Keyboard are running too.

\$\endgroup\$
3
\$\begingroup\$

Using an Arduino Leonardo and a modified sketch based on the code of the KeyboardSerial reference example I was able to verify that it is possible to use the serial connection over the same USB cable as the HID keyboard simulation.

I used the following code:

#include "Keyboard.h"

void setup() {
  // open the serial port:
  Serial.begin(9600);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  // check for incoming serial data:
  if (Serial.available() > 0) {
    // read incoming serial data:
    char inChar = Serial.read();
    Serial.write(inChar);
    // Type the next ASCII value from what you received:
    // Keyboard.write(inChar + 1);
    Keyboard.press(KEY_CAPS_LOCK);
    delay(100);
    Keyboard.releaseAll();
  }
}

When connecting to the COM port of the Leonardo (eg. using PuTTY) I got:

THe cApSlOcK lEd Is tOgGlInG WiTh eVeRy kEy sTrOkE.

Note: The output of the terminal is an echo of the input sent by the Arduino

Serial.write(inChar);

statement.

\$\endgroup\$
4
  • \$\begingroup\$ But it may not be reliable. It may lock up completely, but only if a program is connected to the serial (like PuTTY) here (or at much higher probability). I got lock ups of the Arduino application itself for outputting about 35 characters of (debug) information per key press (or key release) to the serial port. This was also on an Arduino Leonardo. \$\endgroup\$ Commented Sep 14, 2022 at 17:19
  • \$\begingroup\$ cont' - The Arduino application locked up (either became completely unresponsive or exhibited erratic behaviour), the serial port connection failed (forcibly closed and the terminal programs I used quit execution), and a power cycle was necessary to bring the Arduino application back. It was mostly intermittent, but I found a scenario where I could reliably reproduce the problem. It locked up for both the built-in "Serial Monitor" and using "Screen" as a serial terminal program. \$\endgroup\$ Commented Sep 14, 2022 at 17:19
  • \$\begingroup\$ A lot may depend on the exact details: Rate of key presses, how it is interleaved with the serial output, the exact content of the serial output and the timing of it, the boot loader version (e.g., bugs in it), etc. For instance, I only noticed this problem with starting to use the second serial port ('Serial1') for another purpose (receiving data from a PS/2 port). The two ports should be completely independent of each other. It could also be due to using the debug output more, not related to the second serial port. \$\endgroup\$ Commented Sep 14, 2022 at 17:29
  • \$\begingroup\$ For reliability on the host end, the key typing rate was limited to about 50 per second (20 ms) - 10 ms delay for key press and 10 ms key release. \$\endgroup\$ Commented Sep 14, 2022 at 17:38
1
\$\begingroup\$

It cannot do serial at the same time it is acting as an HID device. I got around this by using a usb-5v serial cable and using the second serial port on the Leonardo (i.e. Serial1.begin(9600) )

\$\endgroup\$
2
  • \$\begingroup\$ I wonder why the Arduino example code has that... \$\endgroup\$ Commented May 9, 2013 at 23:39
  • 1
    \$\begingroup\$ It can. By being a composite usb device with multiple profiles. \$\endgroup\$
    – Passerby
    Commented Feb 12, 2017 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.