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 http://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.

Leave a Reply

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