USB Host - Keyboard to ASCII Converter

HobbyTronics USB Host - Keyboard to ASCII Converter
Click to enlarge
Model: USBHOST-KEYBOARD
Manufacturer: HobbyTronics
Average Rating: 5 out of 5 Stars!

HobbyTronics

USB Host Board - Keyboard Software

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).
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.

Download Software
Click Here to Download (Version 1.36)
USB Host BoardUSB Host Mini
USB Host Boards
USB Host IC SOIC  USB Host IC DIP  USB Host IC SSOP
USB Host IC's

Well, now you can simply and easilty 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
HELP or ? Display Help Screen

 

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

/*
** 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
}

Version History

  1.36   14 Nov 2016   Restricted Interface to keyboard only 3,1,1
  1.33   04 Nov 2016   Added MODE 6 - debug keyboard codes
  1.32   11 Apr 2016   Customer mod 5
  1.30   09 Dec 2015   Bug fix in I2C functionality
  1.28   23 Feb 2015   Added BAUD Rate 31250
  1.27   11 Feb 2015   Improved key-is-pressed output on SS pin
  1.26   26 Jan 2015   Added MODE 4 - ESC key sends two ESC characters to make 
                       it easier to detect ESC key press
  1.25   15 Sep 2014   Fixed Pound sign
  1.23   10 Feb 2014   Fixed Baud Rate 38400
  1.22   06 Jan 2014   Added Bootloader check
  1.21   06 Nov 2013   Added Alt key combinations
  1.20   05 Nov 2013   Fixed Function Keys on Numeric Keypad
  1.15   02 Nov 2013   Initial release of I2C functionality to read keyboard                     
  1.10   23 May 2013   Fixed Caps/Num Lock Toggling problem
  1.07   28 Jan 2013   Added ability to change ESC key from 27 to 0
  1.06   16 Jan 2013   Added key press indicator
                       SS pin goes high when key is pressed, low when released
  1.05   07 Jan 2013   Added UK Country Keyboard option
                       Prints Pound sign and swaps " and @ keys over
  1.04   24 Oct 2012   Fix for Eeprom Storage (V2 boards only)
  1.03   23 Sep 2012   All Control codes (A-Z) now suppor

 

Write Review
Your Name:


Your Review: Note: HTML is not translated!

Rating: Bad            Good

Enter the code in the box below:

There are no additional images for this product.
USB Host - Keyboard to ASCII Converter
$ 0.00 New In Stock
Shopping Cart
0 items
 
DELIVERY/PAYMENT

Free Delivery Orders over £50

UK - £2.40 orders under £50
Airmail from £2.95 (+VAT)
More Details...

 
Brands
 
 Check out our videos