Slotted Optical Sensor to trigger mouse click

I am a total newbie, but every time I ask a smart person how I would use a slotted optical to send a command to a windows application that would be a left or right mouse click or a keyboard press. The response I keep getting is, "you should be able to do that with Arduino". So where do I start? Where can I get a little schooling on this? Thanks!

An Arduino Leonardo or Micro or ProMicro can pretend to be a USB keyboard but an Uno or Mega cannot.

There are keyboard functions to make it simple to send a keypress

If the only job the Arduino has to do is check for an input from the slotted optical detector then you can constantly check with digitalRead() on the I/O pin the detector is connected to.

If you don't know how to connect an optical detector to an Arduino then I recommend this Sparkfun circuit. I have used it with several different detectors that I have salvaged from printers. Sparkfun refer to it as an analog circuit but I use it with digital inputs.


Start by buying one of the Arduino boards Robin2 mentioned as well as the sensor and whatever you need to connect the sensor to the Arduino board.

Next, break the project into its components:

  • Read sensor
  • Click mouse button

Write a separate minimal sketch for each of those tasks. You can find a lot of information in the reference pages on this website. For the mouse button part, the Mouse library documentation: Mouse - Arduino Reference and the example sketches at File > Examples > 09.USB with their associated tutorials: After you have learned how to do each task individually and have verified your code is working correctly, it will be fairly easy to combine the two into a single program.

Thank you guys so much for the help. I shelved this for a moment when the opportunity seemed to go cold. It's now back alive and I have gathered the items. I have a Leonardo and the optical sensor. I am really really dumb about this stuff. I have no business trying, but I have no choice. Any hand holding I could get on this would be so appreciated. I am even interested in contracting someone to handle this for me. I need some step by step instructions.

I have a breadboard and a kit of jumpers, power supplies, all sorts of things. Just unclear on how to hook it all together.

There are three cables coming out of the sensor. They are as follows. Yellow (DOUT). Black (GND). Red (VCC).
I am thinking that DOUT is Digital Out, and Black is Ground, and VCC is voltage in.

I am assuming I should connect the ground to the ground port on the digital rail on the Leonardo. The maybe the Dout should go on any one of the 0-13 ports on the digital rail? Does it matter which one? Lastly, I am thinking I need to plug VCC into the 3.3v power port or the 5v port. Not sure on that or which one to use.

Next I think I need to power up the Leonard. Do I use the USB to connect it to a computer? Then how do I program the commands?

I am sorry I am an idiot. Thank you in advance for any help you can provide!

Call the pin holes pin holes or just pins. 8 of those makes a port on AVR chips and in time you may learn to read and write whole ports in a single command.

Pins 0 and 1 are used by Serial.

I wonder what kind of light sensor you got.

I use phototransistors that have 2 legs. Current only flows through by how much light hits, they are valves turned by light.
If you get the kind of IR light the bulb is black to filter visible light out, only IR penetrates but electrically it is the same as a PT.

There are cadmium sulphide light sensors that are cheap but they are slow compared to phototransistors and Arduino, 10 milliseconds to notice any change.

Thanks for the reply. The sensor I have is this one.

That source IR to reflect back onto the sensor from some distance maybe on close, I dunno, mess with it.
Try this, connect the data pin to Arduino pin 2. Wave a card in front of the sensor and see if the Arduino board led turns on and off. Or off and on.

// data-test

byte dataPin = 2;
byte ledPin=13;

void setup() 
  pinMode( dataPin, INPUT_PULLUP );
  pinMode( ledPin, OUTPUT );

void loop() 
  if ( digitalRead( dataPin ))
    digitalWrite( ledPin, HIGH );
    digitalWrite( ledPin, LOW );

That source IR to reflect back onto the sensor from some distance maybe on close

I think that is a regular slotted optical detector viewed from the bottom.

@UeckerFan, almost certainly it has a 5v connection, GND and Output. It should not be too hard with testing to figure out which is which.