Skip to main content

Controlling an Arduino MCU using a keyboard

I find it sometimes useful to be able to control digital output pins on an Arduino using my computer keyboard when doing projects. It allows for quick and easy testing (i.e. toggling relays...) before writing any more control coding. Making use of the Processing and Arduino languages, it is possible to send bytes of data over the Arduino's serial port. 
First, why use Processing instead of any other language? Since Processing is a very user friendly language and has some backwards compatibility with Java, it is the perfect environment for writing small pieces of code without having to worry about the headaches of compiling and running codes. Additionally, it uses the same light weight IDE as Arduino making it simple to use. 
Second, why even use Processing? Arduino deals with the programming of the on board microcontroller, rather than listening for keyboard inputs from your computer. Solving this problem is simple by writing a program that listens at the software level of your computer for keyboard inputs. In this case I am using Processing. 

Below is the Arduino code I am using in order to control the digital pins 2 and 3 (set as outputs). In this program, Serial.read() is reading values and storing them into a char. The char is then used to determine which output pins to set as HIGH as determined by the value of "val". Additionally, when a key is not being pressed, the character 'a' is being sent by default to trigger a loop break (This helps resolve the noise caused by the keyboard refresh rate). Finally, the default state of the pins is set to LOW.
Below is the Arduino Code:
===
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Arduino Code

int pinA = 2; //Define an int for digital pin 2
int pinB = 3; //Define an int for digital pin 3

void setup() {
  pinMode(pinA, OUTPUT); //Set pinA as output
  pinMode(pinB, OUTPUT); //Set pinB as output
  Serial.begin(9600); // Serial at 9600 baud
}

void loop() {
  char val = Serial.read(); //Read the char sent from the Processing code

  while (val == 'z') {
    digitalWrite(pinA, HIGH);

    if (Serial.read() == 'a')
      break;
  }
  while (val == 'x') {
    digitalWrite(pinB, HIGH);

    if (Serial.read() == 'a')
      break;
  }
  digitalWrite(pinA, LOW);
  digitalWrite(pinB, LOW);
}
===
Next, to send the keyboard strokes to the Arduino code, Processing code is used. In this code, it can be seen that a serial communication is established with the Arduino and the functions keyPressed and keyReleased are being used. Function keyPressed sends the key being hit's value over to the Arduino (i.e. 'z' or 'x'). Function keyReleased sends char 'a' when nothing is being pressed.
Below is the Processing Code:
===
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//Processing Code

import processing.serial.*;
/* Click the box that appears when running the program before striking the z or x keys */


String comPort = "COM4"; //Select the serial port used by the Arduino
Serial CNC = new Serial(this, comPort, 9600);

void draw() {
  //Empty for this program. Keep the draw() function so that Processing runs.
}


void keyPressed() {
  println(key); //shows which key is being pressed.
  
  while (key == 'z' || key == 'Z') {
    CNC.write('z'); //send 'z'
    break;
  }
  while (key == 'x' || key == 'X') {
    CNC.write('x'); //send 'x'
    break;
  }
}

void keyReleased(){ 
 CNC.write('a'); //send 'a' to indicate that no key is being pressed.
}
===

In conclusion, this is a quick and easy way to control an Arduino using Processing and a keyboard, and is great for testing purposes.