0

I've tried to use Keypad.h and Keyboard.h libraries to get my Arduino to use my Keypad for Keyboard input.

I'll start with working keypad code (which is pretty easy but it's my first post so I want to make it as neat as possible).

Working keypad

#include <Keypad.h>

const byte Rows = 4;
const byte Cols = 4;

char hexaKeys[Rows][Cols] = {
  {'1', '2', '3', 'A' },
  {'4', '5', '6', 'B' },
  {'7', '8', '9', 'C' },
  {'*', '0', '#', 'D' },
};

byte rowPins[Rows] = {9,8,7,6};
byte colPins[Cols] = {5,4,3,2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins
, Rows, Cols);


void setup() {
  Serial.begin(9600);
}

void loop() {
char customKey = customKeypad.getKey();
  if (customKey){
    Serial.println(customKey);
  }
}

At this point, using Serial Monitor, I can see that all keys are being read and printed out no problem. Then, I tried adding the Keyboard.h library to get it to act as an keyboard input Not Working Keyboard Input

#include <Keypad.h>
//#include <Keyboard.h>
const byte Rows = 4;
const byte Cols = 4;

char hexaKeys[Rows][Cols] = {
  {'1', '2', '3', 'A' },
  {'4', '5', '6', 'B' },
  {'7', '8', '9', 'C' },
  {'*', '0', '#', 'D' },
};

byte rowPins[Rows] = {9,8,7,6};
byte colPins[Cols] = {5,4,3,2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins
, Rows, Cols);


void setup() {
  Serial.begin(9600);
 // Keyboard.begin();
}

void loop() {
char customKey = customKeypad.getKey();
  if (customKey){
    Serial.write(customKey);
    //Keyboard.write(customKey);
  }
}

The commented out sections are where I've added changes. The problems I've encountered: - Can't include Keyboard.h on regular Arduino Uno. I've read that changing board type to Arduino Leonardo helps, and it does, but then the Serial Monitor is greyed out and doesn't read keypad input. Using following code I get following error message ( I'm still 'using' Leonardo board since Keyboard.h doesn't want to work with Uno)

#include <Keypad.h>
#include <Keyboard.h>
const byte Rows = 4;
const byte Cols = 4;

char hexaKeys[Rows][Cols] = {
  {'1', '2', '3', 'A' },
  {'4', '5', '6', 'B' },
  {'7', '8', '9', 'C' },
  {'*', '0', '#', 'D' },
};

byte rowPins[Rows] = {9,8,7,6};
byte colPins[Cols] = {5,4,3,2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins
, Rows, Cols);


void setup() {
  Serial.begin(9600);
 Keyboard.begin();
}

void loop() {
char customKey = customKeypad.getKey();
  if (customKey){
    Keyboard.write(customKey);
  }
}

processing.app.debug.RunnerException at cc.arduino.packages.uploaders.SerialUploader.uploadUsingPreferences(SerialUploader.java:152) at cc.arduino.UploaderUtils.upload(UploaderUtils.java:77) at processing.app.SketchController.upload(SketchController.java:732) at processing.app.SketchController.exportApplet(SketchController.java:703) at processing.app.Editor$UploadHandler.run(Editor.java:2047) at java.lang.Thread.run(Thread.java:748) Caused by: processing.app.SerialException: Error touching serial port 'COM7'. at processing.app.Serial.touchForCDCReset(Serial.java:107) at cc.arduino.packages.uploaders.SerialUploader.uploadUsingPreferences(SerialUploader.java:136) ... 5 more Caused by: jssc.SerialPortException: Port name - COM7; Method name - openPort(); Exception type - Port busy. at jssc.SerialPort.openPort(SerialPort.java:164) at processing.app.Serial.touchForCDCReset(Serial.java:101) ... 6 more An error occurred while uploading the sketch Exception in thread "Thread-28" java.util.ConcurrentModificationException at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:966) at java.util.LinkedList$ListItr.next(LinkedList.java:888) at processing.app.packages.LibraryList.getByName(LibraryList.java:61) at cc.arduino.contributions.libraries.LibrariesIndexer.addToInstalledLibraries(LibrariesIndexer.java:130) at cc.arduino.contributions.libraries.LibrariesIndexer.scanLibrary(LibrariesIndexer.java:229) at cc.arduino.contributions.libraries.LibrariesIndexer.scanInstalledLibraries(LibrariesIndexer.java:201) at cc.arduino.contributions.libraries.LibrariesIndexer.rescanLibraries(LibrariesIndexer.java:161) at cc.arduino.contributions.libraries.LibrariesIndexer.setLibrariesFolders(LibrariesIndexer.java:120) at processing.app.BaseNoGui.onBoardOrPortChange(BaseNoGui.java:677) at processing.app.Base.onBoardOrPortChange(Base.java:1336) at processing.app.Editor$UploadHandler.run(Editor.java:2083) at java.lang.Thread.run(Thread.java:748)

1

1 Answer 1

0

The UNO can't be a Keyboard. It doesn't have a real USB connection. It has a serial connection that passes through a UART to USB converter.

You cannot use the UNO as a keyboard.

Instead you need one of the boards that has a real USB connection, such as the Arduino Leonardo or any of the other ATMega32U4-based boards.

3
  • Thanks for that clarification! Would Arduino Mega 2560 work?
    – Atlantic
    Commented Mar 28, 2020 at 15:33
  • No. That doesn't use the ATMega32U4 either. Arduino Micro or Leonardo
    – Majenko
    Commented Mar 28, 2020 at 15:36
  • if the UNO uses a ATMega16U4 as a USB to serial converter, then it is doable .... you would have to reprogram the ATMega16U4
    – jsotola
    Commented Mar 28, 2020 at 17:50

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.