MAKER: Kutluhan Aktar/Translated by: Qiu
For a long time, I needed a simple device that allows me to send various mouse and keyboard commands to test some of my web applications and games in the browser. Additionally, I could use such a device to test the keyboard and mouse functions of new single-board computers (like Raspberry Pi) without worrying about the lack of a suitable keyboard and mouse. Therefore, I decided to create this project.
First, to be able to send keyboard and mouse commands via USB, I used an Arduino Pro Micro based on the ATmega 32U4 (an 8-bit AVR very similar to the Atmega328), which is equipped with a full-speed USB transceiver that can emulate any USB device.
Then, I designed a sending keyboard that supports up to 32 keys and two dynamic keyboard options using a 4×4 matrix keyboard.
Finally, I used two joysticks as a multifunction mouse, which can also control dynamic keyboard options and modifier keys.
Component List
Custom PCB × 1, SparkFun Pro Micro 5V/16MHz × 1, Analog Output Joystick × 2, 6×6 Keypad × 1, 5mm Green LED × 1, 5mm Blue LED × 1, 220Ω Resistors × 2, Soldering Iron × 1, Lead-free Solder Wire × 1
Circuit Board Soldering
The PCB production files can be downloaded from the project repository: https://make.quwj.com/project/355
After receiving the sample PCB, first, solder the sockets with the soldering iron. The joystick, LED, and resistors are also soldered.
Components on PCB: A1 (Connector for Arduino Pro Micro), J1, J2 (COM-09032 Analog Joystick), K1-K16 (6×6 Keypad), D1 (5mm Green LED), D2 (5mm Blue LED), R1, R2 (220Ω Resistors), C1 (External Keyboard Connector)
Setting Up Arduino IDE
Before coding, we need to verify the settings for the Pro Micro board in Arduino IDE. In the latest version of Arduino IDE, third-party development boards can be easily added through the Boards Manager.
1. Open Arduino IDE, then go to Preferences (File > Preferences). Next, paste the following URL into the Additional Board Manager URLs box at the bottom of the window.
You can add multiple URLs by clicking the window icon and pasting one URL per line.
2. After clicking OK, open the Tools > Board selection tab > Boards Manager to open the Boards Manager.
3. In the Boards Manager, search for sparkfun. When SparkFun AVR Boards appears, click install, and wait a moment until the IDE confirms that all .brd files have been installed.
4. Finally, select the Sparkfun Pro Micro Board under Sparkfun Boards, and upload the code to the Pro Micro.
Programming
Add the required libraries. Keypad https://www.arduino.cc/reference/en/libraries/keypad/
Keyboard https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
Mouse https://www.arduino.cc/reference/en/language/functions/usb/mouse/
Define symbols on the dynamic keyboard option buttons – letters and numbers. Initialize the dynamic keyboard.
char letterKeys[ROWS][COLS] = {char letterKeys[ROWS][COLS] = { {'e','a','r','i'}, {'o','t','n','s'}, {'p','m','h','w'}, {'l','c','u','d'}};char numberKeys[ROWS][COLS] = { {'1','2','3','+'}, {'4','5','6','-'}, {'#','0','*','%'}, {'7','8','9','/'}};byte rowPins[ROWS] = {6, 7, 8, 9}; // Connect to the row pinouts of the keypad.byte colPins[COLS] = {2, 3, 4, 5}; // Connect to the column pinouts of the keypad.// Initialize an instance of class NewKeypad for each keypad setting - letter and number.Keypad letterKeypad = Keypad( makeKeymap(letterKeys), rowPins, colPins, ROWS, COLS);Keypad numberKeypad = Keypad( makeKeymap(numberKeys), rowPins, colPins, ROWS, COLS);
In the read_joysticks() function, the data generated by the joystick handles J1 and J2.
void read_joysticks(){ joystick_left_x = analogRead(VRX_L); joystick_left_y = analogRead(VRY_L); joystick_left_button = digitalRead(SW_L); joystick_right_x = analogRead(VRX_R); joystick_right_y = analogRead(VRY_R); joystick_right_button = digitalRead(SW_R);}
In the mouse_controls() function, the cursor moves according to the left joystick (J1) movements, and left (J1) and right (J2) joystick buttons are used for clicking.
void mouse_controls(){ // Move mouse according to the left joystick movements. if(joystick_left_y > 700) Mouse.move(0, -15); // UP if(joystick_left_y < 300) Mouse.move(0, 15); // DOWN if(joystick_left_x > 700) Mouse.move(-15, 0); // LEFT if(joystick_left_x < 300) Mouse.move(15, 0); // RIGHT if(joystick_left_button == 0) Mouse.click(MOUSE_LEFT); // LEFT CLICK if(joystick_right_button == 0) Mouse.click(MOUSE_RIGHT); // RIGHT CLICK delay(100);}
In the keyboard_controls() function, the dynamic keyboard settings (letters or numbers) are changed based on the movements of the right joystick (J2), and modifier keys (Enter and Backspace) are pressed.
Use the selected dynamic keyboard option (letters and numbers) to get custom keys and write the custom keys as modifier keys.
void keyboard_controls(){ // Change keypad settings (letter or number) and press modifier keys according to the right joystick movements. if(joystick_right_y > 700){ Keyboard.press(KEY_RETURN); delay(100); Keyboard.releaseAll(); } // RETURN if(joystick_right_y < 300){ Keyboard.press(KEY_BACKSPACE); delay(100); Keyboard.releaseAll(); } // BACKSPACE if(joystick_right_x > 700){ letter = true; number = false; digitalWrite(let_LED, HIGH); digitalWrite(num_LED, LOW); } // Letter Keypad if(joystick_right_x < 300){ letter = false; number = true; digitalWrite(let_LED, LOW); digitalWrite(num_LED, HIGH); } // Number Keypad // Get the custom key depending on the selected keypad type - letter or number. char customKey; if(letter == true) customKey = letterKeypad.getKey(); if(number == true) customKey = numberKeypad.getKey(); // Write the custom key. if (customKey){ Keyboard.write(customKey); }}
Pin Connection Table
// Arduino Pro Micro : // JoyStick (Left)// A0 --------------------------- VRX// A1 --------------------------- VRY// D14 --------------------------- SW// 5V --------------------------- 5V// GND --------------------------- GND// JoyStick (Right)// A2 --------------------------- VRX// A3 --------------------------- VRY// D16 --------------------------- SW// 5V --------------------------- 5V// GND --------------------------- GND// KeyPad// D2 --------------------------- C1// D3 --------------------------- C2// D4 --------------------------- C3// D5 --------------------------- C4// D6 --------------------------- R1// D7 --------------------------- R2// D8 --------------------------- R3// D9 --------------------------- R4// Letter Keypad LED// D15 --------------------------- +// Number Keypad LED// D10 --------------------------- +
After completing and uploading the code to the Arduino Pro Micro, insert it into the PCB socket.
Modes and Functions
The controller allows users to move the cursor using the left joystick (J1) and click by pressing the left (J1) or right (J2) joystick buttons.
– J1 > Left > Cursor moves left
– J1> Right > Cursor moves right
– J1 > Up > Cursor moves up
– J1 > Down > Cursor moves down
– J1 > Button > Left mouse click
– J2> Button > Right mouse click
Users can choose between dynamic keyboard options (letters and numbers) and send modifier keys by moving the right joystick (J2).
– J2 > Left > Letter keyboard
– J2 > Right > Number keyboard
– J2 > Up > Enter
– J2 > Down > Backspace
The controller includes an integrated 4×4 matrix keypad. With dynamic keyboard options, the controller can support up to 32 keyboard keys.
Default keyboard keys on keypad buttons:
– K1 > e,1
– K2 > a,2
– K3 > r,3
– K4 > i,+
– K5 > o,4
– K6 > t,5
– K7 > n,6
– K8 > s,-
– K9 > w,%
– K10 > h,+
– K11 > m,o
– K12 > p,#
– K13 > d,/
– K14 > u,9
– K15 > c,8
– K16 > I,7
The code used in the project can be downloaded from the project repository.
Finally, I wish everyone a Happy New Year! Looking forward to next year~
Project repository link:
http://make.quwj.com/project/355
via hackster.io/arduino-based-atmega32u4-mouse-and-keyboard-controller-0f75c5
Links in the article can be clicked at the end to read the original text
More exciting content
Make a motorcycle dashboard with Raspberry Pi
Make an intelligent pet feeder with Raspberry Pi
Build a smart planetary observer based on Raspberry Pi
Handmade metal wireframe craft X-wing clock
Arduino + 280 LEDs DIY music spectrum light
DIY Stanford Pupper 12 Degrees of Freedom Quadruped Robot Dog
Barrier: Keyboard and mouse sharing solution between PC and Raspberry Pi