Friday, September 20, 2019

Make a Keyboard with Arduino

This project will show how can you use an USB enabled Arduino as a USB Keyboard.
I will not explain how it works from the silicon. That will be a book one it self.

So What is "USB enabled Arduino" ?

Not all Arduino can act as a USB Keyboard. Usually those board has Atmel-Mega32U4 MicroControlar can act as one. Those chip have HID support.
Like Arduino micro, Arduino Leonardo etc.
You can make an entire keyboard if you want.

What you need?

1. Arduino Micro/ Leonardo.
2. 4 Push Buttons
3. Some wires

Schematics

Wire up your parts according to the diagram And connect the Arduino to your PC/Mac.





Code

This is a simple code that will send some English character to a computer.
1. Setup the circuit as shown in the diagram above with the breadboard wires.
2. Now Plug the Arduino to your PC/Mac with USB cable.
3. Select the right board and Port from the Tools menu in Arduino IDE.

3.  Click the Upload button to send this code to your Arduino.
4. Wait for to upload to finished then reconnect the Arduino by unplugging the USB cable and re-plugging.
5. Now open "Notepad" app in your PC/MAC.
6. Push one of those button, you will see the character in the app.


Here you can see when i press tthe buttons it sends the character to the PC.


This one will send 'A', 'B', 'C', 'D' character to PC/Mac.



#include <Keyboard.h>    // This is a "built-in" library so no need to install from outside


void setup() {
pinMode(2,INPUT_PULLUP);  // sets pin 3 to input

pinMode(3,INPUT_PULLUP);  // sets pin 4 to input

pinMode(5,INPUT_PULLUP);  // sets pin 3 to input

pinMode(6,INPUT_PULLUP);  // sets pin 4 to input
}


void loop() {
 Keyboard.begin();         //First begin keyboard

 if (digitalRead(2) == 0 )  // if buton 2 is pushed
  {
    Keyboard.write('A');  // send single character "A"
    delay(200);           // delay to don't get 5 A's
  }

  if (digitalRead(3) == 0 )  // if buton 3 is pushed
  {
    Keyboard.write('B');  // send single character "B"
    delay(200);           // delay to don't get 5 B's
  }

  if (digitalRead(5) == 0 )  // if buton 5 is pushed
  {
    Keyboard.write('C');  // send single character "C"
    delay(200);           // delay to don't get 5 C's
  }

  if (digitalRead(6) == 0 )  // if buton 6 is pushed
  {
    Keyboard.write('D');  // send single character "D"
    delay(200);           // delay to don't get 5 D's
  }

  Keyboard.end();         // stops keybord
}


This one will send Up, Down, Left, Right command to PC/Mac. You can try this in a game or website.

#include <Keyboard.h>    // This is a "built-in" library so no need to install from outside


void setup() {
pinMode(2,INPUT_PULLUP);  // sets pin 3 to input

pinMode(3,INPUT_PULLUP);  // sets pin 4 to input

pinMode(5,INPUT_PULLUP);  // sets pin 3 to input

pinMode(6,INPUT_PULLUP);  // sets pin 4 to input
}

void loop() {

 Keyboard.begin();         //First begin keyboard

 if (digitalRead(2) == 0 )  // if buton 2 is pushed
  {
    Keyboard.write(KEY_UP_ARROW);
    delay(200);         
  }

  if (digitalRead(3) == 0 )  // if buton 3 is pushed
  {
    Keyboard.write(KEY_DOWN_ARROW);
    delay(200);         
  }

  if (digitalRead(5) == 0 )  // if buton 5 is pushed
  {
    Keyboard.write(KEY_LEFT_ARROW);
    delay(200);        
  }

  if (digitalRead(6) == 0 )  // if buton 6 is pushed
  {
    Keyboard.write(KEY_RIGHT_ARROW);
    delay(200);        
  }

  Keyboard.end();         // stops keybord
}


Conclusion

1. You can send any character you want in this list from 1st to 128th. Other can't because the keyboard library dose not supports others.
2. You can also use other spacial keys from this list here.
3. You can make a custom Keyboard to suit your needs. Like Video editing or Photoshop shorcuts in a keyboard.

N.T.

1. Download the library first from "Tools">> "Manage Libraries..">> "Keyboard Buit-In by Arduino ">> Click the install.
2. Select the proper board from the "Tools">> "Boards" . Or you will get error when you tried to build the code.
3. It will register/handles one button at a time. Because the code dose not supports multi pal registering key. Means if you press all of those buttons at a time it will not send all of those one time.
4. Make sure there is no loose connection and Use a good USB cable.

 Thanks.
Ain't getting any visitors!
Please Share and Bookmark posts.

Tags

: (1) 18F2550 (1) 36KHz (3) and (1) arduino (1) Based (1) battery (1) Bipolar (1) Blinking (1) blinks (1) Bluetooth (1) bluetooth device interfacing (1) bluetooth module (1) button (1) circuit (1) clock (1) control (1) crystal oscillator (3) Db9 (1) DC Motor (2) digital (2) Digital Voting Machine (1) digital voting machine using pic (1) display (2) DS1307 (1) electronic (1) embedded c programming tutorial (11) embedded c tutorial (11) experiment kit (4) external interrupt (4) flash (1) flashing (1) Gas Leakage detector (1) HC-06 (1) home (1) how (1) How to (10) i2c tutorial (1) in (1) indicator (1) infrared Connection (3) interface (8) interfacing (3) Interrupt (3) Introduction (1) IR Connection (3) IR Receiver (4) IR Transmitter (4) key pad (1) keyboard (1) keypad (1) lavel (1) Lcd 16x2 (2) lcd 2x16 (2) led (1) lm35 (2) LPG (1) machine (1) make (1) Make bootloader (1) making (1) matrix (1) max232 (1) membrane keyboard (2) meter (2) Micocontroller (1) microchip (4) microchip pic (2) microchips (3) microcontroller (9) microcontroller based (3) microcontroller programming (3) Microcontroller Project (4) Microcontroller Projects (1) microcontroller_project (2) microcontrollers (4) Microprocessor (2) mikroC (5) mikroc code to start and stopstart and stop dc motor (1) mikroc pro for pic (2) Motion detector (1) MQ-9 Gas Sensor (1) musical (1) NEC Protocol (4) pcb (5) PIC (3) pic controller (11) pic microcontroller (11) pic microcontroller tutorial (11) pic programming (1) pic programming in c (12) pic proteus (1) Pic Tutorial (12) pic18 (2) pic18f2550 (11) picmicrocontroller (4) picRFモジュール (1) PIR Motion Sensor (1) printed circuit board (1) proteus (6) pulse width modulation (1) push (1) push button (1) PWM (1) real (1) rf transmitter (3) Rs 232 (1) Rs232 (1) scroll (1) scrolling (1) Serial communication (1) Serial Connection (1) Serial Port (1) serial port rs232 (1) Servo Motembedded c programming tutorial (1) simulation (2) Soil Moisture Meter (1) speed control (1) step by step (7) step bystep (1) Stepper Motor (2) text (2) Thief Detector (1) time (1) timer (4) timer0 (4) tone (1) TSOP38236 Receiver (4) tutorial (2) Unipolar (1) USART Connection (1) USB (1) usb 1.0 (1) USB bootloadere (1) USB HID (1) using (9) voltmeter (1) voting (1) water level indicator (3) with (2) work (1)

Traffic Feed


Live Traffic Feed
Visitor Tracking

Leave Your Message Here

Name

Email *

Message *

Like on Facebook