Skip to main content

Arduino IR Contactless Mouse

I've created a lot of projects using Arduino as a mouse or keyboard. Including “Arduino Nano 33 BLE SENSE Gesture Mouse”, “DesignSpark Mechanical Virtual Controller”, “Playing GarageBand with Arduino”, and the “Raspberry Pi Pico Piano” series.

So, today, this will be the last article about making a mouse with Arduino. With this series being about making a mouse or keyboard with an Arduino, I wanted to bring out the many possibilities that the Arduino has. I think when you talk about a mouse, your first impression is that there is a scroll wheel and two buttons. But sometimes, when we are doing some innovative projects or embedded products, we have to jump out of the box. We can design it according to our purpose, or just take the components off your desk and design it. Having reviewed the concepts from my previous articles, let's talk about today's project.

Infrared Contactless Mouse

After doing the last article "Arduino Nano 33 BLE SENSE Gesture Mouse", I found contactless very interesting, but you will find that controlling the mouse cursor in that gesture sensor does not make the cursor move smoothly, and if you want to use that control all the time it will be tiring. So, I came up with the idea of making my screen a touchscreen. How to do it? I'm thinking that if I put IR sensors around the screen and use a matrix method to find out where I'm pointing, then my monitor could become a touchscreen. But if I put the IR sensor around the screen, it should need a lot of IR sensors, so I first came up with a prototype idea to make an IR non-contact mouse first.

The idea is very simple, make a 3x3 matrix to determine where the cursor moves.

Through this method, we can use some conditions to determine where the mouse cursor moves. For example, if we want to move the cursor up, we can program that if the first row of the second column is at a high level, the mouse cursor will move up. Otherwise, if we don't use the matrix method, we can also use the analogue readings from the IR sensor to determine where the cursor has moved. This means we can directly use the analogue reading of column 2 to determine whether the cursor is moving forward or backwards. But I would say that the accuracy of this method is very low and I don't recommend it.


For each column and row, we need a set of IR sensors and LED. The infrared sensor (IR sensor) is a photoelectric element sensitive to radiation. We can determine whether the reading has changed by emitting electromagnetic radiation to the infrared sensor through the IR LED, and see if there is something blocking the signal.

The Program

I think if you read my previous article on making a mouse with Arduino, you know pretty well how to make a mouse with Arduino. Therefore, I won't go through all the code in this article. If you haven't read my previous article on how to make a mouse with Arduino, read it first.

But in this article, I would like to share what is ternary operator (conditional operator / short-hand if else) is, which can be used to replace if...else.

variable = (condition) ? expressionTrue : expressionFalse;

Sometimes, when we want to replace multiple lines of code with a single line, then we will program in the ternary operator.

Below is an example I used the ternary operator in the program:

//ternary operator
result =
        (valc1 >= limit && valr1 >= limit1) ? "A"
      : (valc2 >= limit1 && valr1 >= limit1) ? "B"
      : (valc3 >= limit1 && valr1 >= limit1) ? "C"
      : (valc1 >= limit && valr2 >= limit1) ? "D"
      : (valc2 >= limit1 && valr2 >= limit1) ? "E"
      : (valc3 >= limit1 && valr2 >= limit1) ? "F"
      : (valc1 >= limit && valr3 >= limit1) ? "G"
      : (valc2 >= limit1 && valr3 >= limit1) ? "H"
      : (valc3 >= limit1 && valr3 >= limit1) ? "I"
      : "off";

Of course, we can program with if...else... , but in the example above, if we have similar conditions and expressions, it is clear to use the ternary operator. But if you are not familiar with the ternary operator, you can still use if...else to explain it, like the example below that I also use in my program.

  if (result == "A"){
    // move mouse up left
    Mouse.move(-3, -3);
    Serial.println("Up left");
  } else if(result == "C"){
    // move mouse up right
    Mouse.move(3, -3);
    Serial.println("Up right");
  }else if(result == "B"){
    // move mouse up
    Mouse.move(0, -5);
  }else if(result == "D"){
    // move mouse left
    Mouse.move(-5, 0);
  }else if(result == "F"){
    // move mouse right
    Mouse.move(5, 0);
  }else if(result == "H"){
    // move mouse down
    Mouse.move(0, 5);
  }else if(result == "G"){
    // move mouse down left
    Mouse.move(-3, 3);
    Serial.println("down left");
  }else if(result == "I"){
    // move mouse down right
    Mouse.move(3, 3);
    Serial.println("down right");
  else if(result == "off"){

I hope you enjoy this project. If you have any comments, please leave them below.


JulianWong has not written a bio yet…