How to turn Arduino Mega 2560 into simple keyboard?

This tutorial will show you how to turn Arduino into HID controller (in this case simple keyboard).

Writing keyboard software

You need to design your software before flashing new firmware because one cannot put new sketches to a keyboard 🙂 If you would like to edit your sketch and reflash it you’ll need to restore an original board firmware.

So, let’s begin… This is my rather stupid keyboard operating system based on examples of others:

It is a simple implementation, only four buttons 1, 2, 3 and 4 connected to four pins. Pin connections are very simple, you should simply connect them all to GND pin of Arduino.

If you want to use other key codes check attachment on the bottom of this post.

Arduino Mega R3 with keys attached (scrapped from old scanner).


File: keyboard.ino
------------------

/** 
* Arduino USB HID Keyboard Demo
* Keys 1, 2, 3 and 4 on pins 4, 5, 6 and 7
* 
* @info check details on my website
* @link https://blog.realhe.ro
*/
uint8_t buf[8] = {
0 }; /* Keyboard report buffer */

#define KEY_ONE     0x1E
#define KEY_TWO     0x1F
#define KEY_THREE   0x20
#define KEY_FOUR    0x21

#define PIN_ONE     4
#define PIN_TWO     5
#define PIN_THREE   6
#define PIN_FOUR    7

int state = 1;

void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
  delay(200);

  pinMode(PIN_ONE, INPUT);
  pinMode(PIN_TWO, INPUT);
  pinMode(PIN_THREE, INPUT);
  pinMode(PIN_FOUR, INPUT);

  digitalWrite(PIN_ONE, 1);
  digitalWrite(PIN_TWO, 1);
  digitalWrite(PIN_THREE, 1);
  digitalWrite(PIN_FOUR, 1);
}

void loop()
{

  state = digitalRead(PIN_ONE);
  if (state != 1) {
    buf[0] = 0;
    buf[2] = KEY_ONE;
    Serial.write(buf, 8);
    releaseKey();
    delay(200);
  }

  state = digitalRead(PIN_TWO);
  if (state != 1) {
    buf[0] = 0;
    buf[2] = KEY_TWO;
    Serial.write(buf, 8);
    releaseKey();
    delay(200);
  }


  state = digitalRead(PIN_THREE);
  if (state != 1) {
    buf[0] = 0;
    buf[2] = KEY_THREE;
    Serial.write(buf, 8);
    releaseKey();
    delay(200);
  }

  state = digitalRead(PIN_FOUR);
  if (state != 1) {
    buf[0] = 0;
    buf[2] = KEY_FOUR;
    Serial.write(buf, 8);
    releaseKey();
    delay(200);
  }

}

void releaseKey()
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key
}

Flashing Arduino firmware

At first check my previous note how to flash Arduino because you will need this knowledge to put new software on your board.

This is for Arduino Mega 2560 R3!

If you know what your doing, go to this repository and clone it or download to you computer. Go to firmwares/arduino-keyboard and open makefile and change MCU like that:

# MCU name(s)
#       Since the ATMEGA8U2 part is not directly supported by the current
#       versions of either avrdude or dfu-programmer, we specify a dummy
#       part; AT90USB82 which is close enough in memory size and organization
#MCU = atmega8u2
#MCU_AVRDUDE = at90usb82
#MCU_DFU = at90usb82
MCU = atmega16u2
MCU_AVRDUDE = atmega16u2
MCU_DFU = atmega16u2

Now you will need to compile your firmware using make command. File with your software will be named Arduino-keyboard.hex. Burn it to your Arduino device using previously mentioned tutorial.

In case of trouble write a comment.

Published by

Konrad Fedorczyk

Konrad Fedorczyk

I'm interested in programming and gamedev. I especially luv HTML5 and everything connected to web technologies.

19 thoughts on “How to turn Arduino Mega 2560 into simple keyboard?”

        1. Have you installed avr-gcc?
          sudo apt-get install avr-gcc avr-libc
          What OS are you working on?
          If you have gcc installed simply type make in firmware directory.

  1. Hi Konrad, can you help me?
    I have edited my sketch, downloaded zip file from github (and modified the makefile as you described), but now can you describe step by step how to compile?

    Which file has to be named Arduino-keyboard.hex?
    Following your next link i think i have to run:
    sudo apt-get update && sudo apt-get install dfu-programmer
    i don’t have to do any wget becouse i suppose i have to flash my Arduino-keyboard.hex
    I have to enable DFU mode and erase current firmware (no backup needed?), then upload my Arduino-keyboard.hex, and finally reset it. Is it right?

    I don’t have understand what do you mean with “You need to design your software before flashing new firmware because one cannot put new sketches to a keyboard If you would like to edit your sketch and reflash it you’ll need to restore an original board firmware.”
    have i to flash again firmware every time i have to edit and upload again my schatck? where i can find an orgnal firmware? why we dont’ have done a backup at the beginning of your procedure?

    Sorry but it’s my first time with arduino.

    1. Ciao Ozzy,
      I’ll try to explain everything to you step by step.

      Compilation
      1. To compile repository with keyboard firmware you will need to install compilers:
      sudo apt-get install -y build-essential avr-gcc avr-libc
      2. Than in a directory cloned from a repo (where you’ve changed this specific file [arduino-usb/firmwares/arduino-keyboard]) simply type:
      make in a terminal.
      3. After succesful compilation hex file will be created in the same directory.
      4. Use dfu instruction to put it on your board.

      Backup
      You don’t need to backup because Arduino’s firmware can be found here [Arduino-usbserial-mega.hex].

      “You need to design your software before flashing…”
      By that I mean, that after flashing keyboard firmware to Arduino you won’t be able to put new sketches to it. That’s because Arduino IDE will see it as a keyboard not Arduino… So if you’ll ever want to update code on your board you’ll need to reflash it back to the original firmware. Also please keep in mind that firmware and sketch aren’t the same thing. Firmware is like a BIOS and sketch is the operating system.

      This instruction will guide you through reflashing process.

      I hope that I’ve helped 🙂

      1. Hi Konrad, tnx for your reply and for all steps, but
        there’s something more i don’t understand, sorry for this.

        If i’m not wrong, as you wrote after step 5 “Arduino IDE will see it as a keyboard not Arduino”, so, how can i upload on it my sketch file?

        1. Ok, I suppose i have to upload sketch before step1, then proceed with stap1 to 4. If sketch is wrong, flash back to Arduino-usbserial-mega.hex so i can upload new sketch, and then proceed again with step1 to 4. Right? I’ll try immediately

          1. Sorry for the late reply! Yest this is exactly what I’ve meant. Upload sketch before flashing keyboard firmware, reflash on error 🙂

  2. Hi konrad, your sample sketch works fine! I have tried to edit adding more keys (i need 24 inputs for my mamecab) but it doesnt work. I have seen mega pinout, but it is not clear, which pins can i use?

  3. Hi Konrad, i have a challenge for you 😀

    Can you define a shift funcion in your code? (i mean: defined the shift key, pressing it and another one it gieve us a new key).
    (I need more than 24 input for my mame cab but i can use only 24 physical buttons).

    Can you help me? I can offer you a beer!

    ..and happy new year!

  4. I have a sample from another sketch:

    if ((digitalRead(g_start) == LOW) && (digitalRead(downButton) == LOW)) {
    Keyboard.press(‘p’); Keyboard.release(‘p’); }

    but you have to import keyboard.h (if i’m not wrong this header works on arduino due or in general with atmega32u4 not on arduino mega).

    how can we adapt these functions to your script?

      1. With arduino nano or arduino due, i have a sketch in which i use the keyboard() funcition. With this mega board i have solved recalling loop function. I know it is a dangerous recursisve solution, but hotkeys are not called so often..
        Here my code: https://pastebin.com/a7HsaSfF

Leave a Reply

Your email address will not be published. Required fields are marked *