How to use arrow Keys

Hey i am not so well experienced in programming...

I recently made a code in which i wish use my arrow keys as controls rather than just typing the command i created and pressing enter in the serial monitor.

I made a code where when you press r and enter, led light shifts right on the led matrix. same with l, means left, u, means up, d, means down.

After evry command i have to press enter to send it to the arduino board.

I wish to use arrow keys instead and without pressing enter i want the command to go to the board, like a video game you know.

Please Help

Not possible with the serial console that comes with the Arduino IDE, since it sends the text, typed into the textbox and you are not able to 'type' a 'left-key'.

You have to look for an alternative COM console that supports it or write your own controller.

Take a look at the Keyboard library, especially


He wants to send the arrow keys from the serial console to the arduino.

The link you attached is how to send keys from the arduino leonardo to a PC, right?

He wants to send the arrow keys from the serial console to the arduino.

Yes, but that's not going to work as you pointed out before my post. I was simply trying to show him a source that uses symbolic constants to read input from non-Serial sources. We don't know if he has the correct board to use the Keyboard library, but there are some ideas there on how to approach the problem.

i am using arduino Uno

If you use a terminal emulator rather than the serial monitor, you can configure it to send you the keycodes as one of the common terminal control escape sequences, e.g. ESC[A

write a little Processing program and just send l,u,r,d when you detect an arrow is pressed

here is a simple example

load this in your UNO - don't open the serial console

void setup() {
  Serial.begin(115200); // make sure it's the same on both sides

void up()
  Serial.write('R');  // RED

void down()
  Serial.write('G');  // GREEN

void left()
  Serial.write('Y');  // YELLOW

void right()
  Serial.write('O');  // ORANGE

void loop() {
  char incomingChar = 0;   // for incoming serial data

  if (Serial.available() > 0) {
    incomingChar =;
    switch (incomingChar) {
      case 'u':
      case 'd':
      case 'l':
      case 'r':
        Serial.write('B');  // BLACK
  } else incomingChar = 0; // not really needed but in case you want to do things in the loop upon receiving new chars. if (incomingChar == 0) then no new char was received, otherwise later in the loop you still have the char received

Then download Processing if you don't have it yet and load this sketch

import processing.serial.*;

// The serial port:
Serial mySerialPort;       

color fillVal = color(0);

void setup() {
  size(100, 100);
  printArray(Serial.list()); // do this to find your arduino
  // Open the port you are using at the rate you want:
  mySerialPort = new Serial(this, Serial.list()[1], 115200);

void draw() { 

//The keyPressed() function is called once every time a key is pressed.
// see

void keyPressed() {

  if (key == CODED) {
    switch(keyCode) {
    case UP: 
    case DOWN: 
    case LEFT: 
    case RIGHT: 
  } else fillVal = color(0);

void serialEvent(Serial p) {
  switch( {
  case 'R': 
    fillVal = #FF0000;
  case 'G': 
    fillVal = #00FF00;
  case 'Y': 
    fillVal = #FFFF00;
  case 'O': 
    fillVal = #FF7F00;
    fillVal = #000000;

Before this works you need to pick the right serial port

In your arduino IDE check which port your Arduino is connected to

You will notice these lines in the Processing program

  printArray(Serial.list()); // do this to find your arduino
  // Open the port you are using at the rate you want:
  mySerialPort = new Serial(this, Serial.list()[ [color=red]1[/color] ], 115200);

I picked 1 because when running for the first time the Processing program I could see that this was the entry matching the Serial Port used with my Arduino.

So basically run your processing program (click on the "play" arrow button at the top left), look in the console below the code which port is yours. stop the Processing program by clicking the "Stop" (rectangle) button on the top left of your Processing window, change the 1 in my code with whatever is your entry number (a good program would actually let you choose of course at run time)

Once this is done relaunch your processing program. it is now connected to your arduino and the code is pretty self explanatory.

On the processing side, I have 2 functions

** **void keyPressed() {** **

which is called when you press a key in your Program. it looks at what key was pressed and if it's an arrow key it sends the relevant char to the Serial line where the Arduino can pick it up

** **void serialEvent(Serial p)  {** **

which reads from the Serial line what comes from the arduino and if it recognizes a special letter, it uses that to set the fillVal variable as a color.

The draw() function in processing is similar to the loop() in Arduino world, it constantly runs and I've only one line of code there which is to paint the background of the small window I've open when launching the Processing program. (what I do in setup() with size(100, 100);) with the fillVal color.

On the arduino side it's pretty simple:

In the loop() I expect characters on the Serial

When I recognize r,l,u,d then I then call the right(), left(), up() or down() function (where you would do your stuff).

At the moment to prove this is working what I do is I send back on the serial line 1 character to the Processing Program. The code I send is 1 letter that defines a color like 'O' for Orange, 'R' for Red...

So basically you press the up arrow key on the keyboard, Your Processing Application recognizes this key and sends through the serial line the 'u' character. The arduino reads a 'u' and calls the up() functions which sends back a 'R' character to Processing. Processing gets that letter, changes the fillVal variable to be the red color (fillVal = #FF0000;) and as the draw() function is called repetitively, next time it gets there, it paints the window background in Red.

That way you control your arduino from your PC/Mac keyboard and you can even have your arduino send something back to your PC as demonstrated here.

pretty cool isn't it?

have fun

pretty cool isn't it?


thx :slight_smile:

I find playing with both pretty fun

The draw() function in processing is similar to the loop() in Arduino world, it constantly runs

There are differences, though. Explaining them is useful.

Of course not the same thing exactly - I just wanted the OP to understand the global architecture of my small Processing code if he is not familiar with Processing. then he can explore :slight_smile:

I'll try all the possible help you have given me. Thanks you so much for giving me many suggestions