If you want to add a keyboard to your microcontroller project, you will know it’s not a simple task. The easiest technical solution was to use an old-style PS/2 keyboard (if you can still find one) which has a serial interface, but you still have to decode all the keyboard scan codes into ascii on your microcontroller.
This is a free software download for our USB Host Board and USB Host IC’s (SOIC, DIP, SSOP).
Please note: This software is free to download but only works on one of our USB boards or chips which must be bought separately.
To order, simply select which of our boards or IC products you wish to have the software loaded onto. See the USB Host Board and IC product pages for details on pricing and installing this software.
Well, now you can simply and easily add a modern USB keyboard to your project by using our USB Host controller board with the Keyboard application software loaded. We take care of the complex USB protocol and keyboard scancode decoding to give you an ascii serial output that you can read straight into your microcontroller.
Note: Apple Macintosh keyboards have a built-in USB Hub and will not work with our boards of firmware.
Note: This software also works with USB Barcode Readers
It converts key strokes into standard ASCII characters that are transmitted via serial TTL at selectable baud rates between 2400 and 115200.
As of version 1.15 there is now basic functionality to read the key presses via I2C as well as serial
Works with Both wired and 2.4GHz wireless USB keyboards
All the standard keys are translated into ASCII characters including
- a-z and A-Z
- 0-9
- Backspace, Tab, Enter, Esc
- ! ” £ $ % ^ & * ( ) – _ + = { } [ ] @ ‘ ~ # , . < > ? / \ | `
- Control-A to Control-Z
- Alt-A to Alt-Z
- Alt-F1 to Alt-F12
- Shift and Caps Lock work as they should generating the correct character
- Home, End, Ins, Del, Page Up, Page Down
- Arrow Keys
- Print Screen and the “Windows” key
- Function Keys F1 to F12
Function keys on the keyboard are translated into a two character combination with the first character being the ESC character (27 or 0x1B). The ASCII two character key combinations are shown in the table below.
Special 2-character key combinations First Character is Escape (27 [0x1B]) followed by… | ||||
Key Pressed | ASCII Character | Key Pressed | ASCII Character | |
F1 | 0x3B | INS | 0x52 | |
F2 | 0x3C | DEL | 0x53 | |
F3 | 0x3D | HOME | 0x47 | |
F4 | 0x3E | END | 0x4F | |
F5 | 0x3F | PAGE UP | 0x49 | |
F6 | 0x40 | PAGE DOWN | 0x51 | |
F7 | 0x41 | UP Arrow | 0x48 | |
F8 | 0x42 | DOWN Arrow | 0x50 | |
F9 | 0x43 | LEFT Arrow | 0x4B | |
F10 | 0x44 | RIGHT Arrow | 0x4D | |
F11 | 0x57 | WINDOWS | 0x5B | |
F12 | 0x58 | Print Screen | 0x54 |
Other Keyboard Codes
The CONTROL and ALT codes are detailed here
Connections required for Keyboard Software
- 5V power in
- 0V
- TX out
- RX in (for setting baud rate only)
- SS pin goes high when Key pressed, low when released
Configuration
The USB Host board will generate ASCII characters at the following baud rates
- 2400
- 4800
- 9600 (default)
- 14400
- 19200
- 31250
- 38400
- 57600
- 115200
The default baud rate is 9600
Configuration can be done by sending commands via the serial port. Either by microcontroller or via a terminal program and a suitable serial TTL connection. Commands take effect immediately and are stored in Flash ROM on the board.
Help can be displayed at any time by typing ? or HELP
The following Commands are available.
BAUD <value> | Set Serial Port Baud Rate (default 9600) [2400|4800|9600|14400|19200|38400|57600|115200] |
I2C <value> | Set I2C Address 1 to 127 (default 41) |
KEYBOARD <value> | Set Keyboard Country Code [0-USA|1-UK] |
ESC <value> | Set Escape Key Code 0 or 27 (default 27 – 0x1B) |
MODE <value> | Set Modification Mode 0 – default behaviour 1 – Customer mod 2 – Customer mod 3 – Customer mod 4 – ESC key produces two ESC chars 5 – Customer mod 6 – Outputs raw HID data – useful for debugging |
RS232 <value> | Set Serial Port Configuration 0 – default TTL (positive logic) 1 – RS232 (negative logic) |
HELP or ? | Display Help Screen |
RS232 Port Configuration
You can now set the serial port to RS232 negative logic. This will enable you to connect the board directly to serial ports that support the RS232 negative logic format. WARNING: If you activate RS232 mode you can no longer revert the Host Board back to TTL – reinstalling serial software does not work.
There are a few important points to bear in mind
- This does not effect the bootloader, so program updates will still need a positive logic TTL connection
- Once you have activated RS232 mode, you will need to communicate with the Host board/chip using RS232
- RS232 is negative logic, but the host chip will only provide 0V and 3.3V output. It will not produce negative voltages
- Do not connect to RS232 ports that have high voltages – e.g. old style PC serial ports which operate at +/- 12V
Program Updates
To upload the Software to the USB Host Board you will need to use the ds30Loader program. See the USB Host Board product page for more information on this.
Example use of the Keyboard Application
The following Arduino sketch prints out any characters received using the serial connection and decodes the function key codes into the name of the function key pressed.
/*
USB HOST BOARD - Keyboard Program
HOBBYTRONICS Ltd 2012
Simple Arduino sketch to show use of USB HOST board Keyboard software
*/
char flag_esc=0;
void setup() {
Serial.begin(9600);
Serial.println("Type some characters and i will send them back");
}
void loop() {
if (Serial.available() > 0) {
int inByte = Serial.read();
if(inByte == 27)
{
flag_esc = 1;
}
else
{
if(flag_esc==1)
{
// Previous char was ESC - Decode all the escaped keys
switch(inByte)
{
case 0x49:
Serial.print("[PgUp]");
break;
case 0x51:
Serial.print("[PgDn]");
break;
case 0x47:
Serial.print("[Home]");
break;
case 0x4F:
Serial.print("[End]");
break;
case 0x52:
Serial.print("[Ins]");
break;
case 0x53:
Serial.print("[Del]");
break;
case 0x3B:
Serial.print("[F1]");
break;
case 0x3C:
Serial.print("[F2]");
break;
case 0x3D:
Serial.print("[F3]");
break;
case 0x3E:
Serial.print("[F4]");
break;
case 0x3F:
Serial.print("[F5]");
break;
case 0x40:
Serial.print("[F6]");
break;
case 0x41:
Serial.print("[F7]");
break;
case 0x42:
Serial.print("[F8]");
break;
case 0x43:
Serial.print("[F9]");
break;
case 0x44:
Serial.print("[F10]");
break;
case 0x57:
Serial.print("[F11]");
break;
case 0x58:
Serial.print("[F12]");
break;
case 0x48:
Serial.print("[Up]");
break;
case 0x50:
Serial.print("[Down]");
break;
case 0x4B:
Serial.print("[Left]");
break;
case 0x4D:
Serial.print("[Right]");
break;
case 0x54:
Serial.print("[Print]");
break;
case 0x5B:
Serial.print("[Windows]");
break;
default:
Serial.print("[?]");
break;
}
flag_esc=0;
}
else
{
if(inByte==1)
{
Serial.print("Control-A");
}
else if(inByte==2)
{
Serial.print("Control-B");
}
else if(inByte==3)
{
Serial.print("Control-C");
}
else if(inByte==4)
{
Serial.print("Control-D");
}
else if(inByte==5)
{
Serial.print("Control-E");
}
else if(inByte==6)
{
Serial.print("Control-F");
}
else if(inByte==7)
{
Serial.print("Control-G");
}
// Dont decode 8 - backspace
else if(inByte==9)
{
Serial.print("Tab");
}
// Dont decode 10 - Line Feed
else if(inByte==11)
{
Serial.print("Control-K");
}
else if(inByte==12)
{
Serial.print("Control-L");
}
// Dont decode 13 - Carriage Return
else if(inByte==14)
{
Serial.print("Control-N");
}
else if(inByte==15)
{
Serial.print("Control-O");
}
else if(inByte==16)
{
Serial.print("Control-P");
}
else if(inByte==17)
{
Serial.print("Control-Q");
}
else if(inByte==18)
{
Serial.print("Control-R");
}
else if(inByte==19)
{
Serial.print("Control-S");
}
else if(inByte==20)
{
Serial.print("Control-T");
}
else if(inByte==21)
{
Serial.print("Control-U");
}
else if(inByte==22)
{
Serial.print("Control-V");
}
else if(inByte==23)
{
Serial.print("Control-W");
}
else if(inByte==24)
{
Serial.print("Control-X");
}
else if(inByte==25)
{
Serial.print("Control-Y");
}
else if(inByte==26)
{
Serial.print("Control-Z");
}
else
{
// Its a normal key
Serial.write(inByte);
}
}
}
}
}
I2C Functionality
As of Version 1.20 we have added the ability to read keyboard characters using I2C instead of a serial connection.
The I2C functionality uses a 256 byte keyboard buffer on the USB Host board to avoid losing any characters between reads.
There are basically two read functions you can perform
1. Check how many bytes are in the buffer
2. Read the data
Check if there are key presses in the buffer
To check how many characters are waiting in the buffer, read a single byte from the register address 0x01
Read the key presses in the buffer
To read the characters that are waiting in the buffer, read one or more bytes from the register address 0x00. It is recommended that you first check how many characters are in the buffer.
Example Arduino program to Read Using I2C (please note the following code doesn’t work with the latest UNO R4’s)
/*
** Wire Master Reader of Hobbytronics USB Host Keyboard
** Created 24 Oct 2013
**
** This example code is in the public domain.
** www.hobbytronics.co.uk
*/
#include <Wire.h>
const int adc_address=41; // I2C Address
char keyboard_data[80]; // Array to store keyboard values
void setup()
{
//Send settings to ADC_I2C device (optional)
Wire.begin(); // join i2c bus (address optional for master)
//Start Serial port
Serial.begin(9600); // start serial for output
}
void loop()
{
unsigned char i;
char keyboard_chars;
Wire.beginTransmission(adc_address); // transmit to device
Wire.write(1); // ask how many characters are in buffer
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(adc_address, 1); // request 1 bytes from slave device
while(Wire.available())
{
keyboard_chars = Wire.read();
}
Serial.println(keyboard_chars, DEC); // print number of buffer characters
Wire.beginTransmission(adc_address); // transmit to device
Wire.write(0); // get keyboard characters
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(adc_address, keyboard_chars); // request characters from slave device
i=0;
while(Wire.available())
{
keyboard_data[i++] = Wire.read();
keyboard_data[i] = '\0';
}
Serial.println(keyboard_data); // print the character
keyboard_data[0]='\0';
delay(5000); // Wait 5 seconds
}