I am working on an Arduino project that replaces the computer mouse. I want to do this in combination with an ultrasonic sensor. I found some nice examples on youtube, but they didn't post the code. And an example on this website. But I don't get it completely. I don't have much Arduino skills, but do have some. How can you attach for example echoPinScroll to a board?

I hope someone can give me advice on how to use this ultrasonic to control my mouse!

#include <Mouse.h>

int echoPinCursor=2;
int trigPinCursor=3;

int echoPinBtn=4;
int trigPinBtn=5;
int echoPinScroll=6;
int trigPinScroll=7;
int x;
int y;
long duracionCursor, distanciaCursor;
void setup() {
 pinMode(trigPinCursor, OUTPUT);
 pinMode(echoPinCursor, INPUT);
 pinMode(trigPinBtn, OUTPUT);
 pinMode(echoPinBtn, INPUT);
 pinMode(trigPinScroll, OUTPUT);
 pinMode(echoPinScroll, INPUT);


void loop() {

//mover el cursor
int direccionx=moverx();
int direcciony=movery();

Mouse.move(direccionx, direcciony,0);


int moverx(){
if (distanciaCursor >= 1  || distanciaCursor <= 11){
if (distanciaCursor >= 12 || distanciaCursor <= 24){
 return x;

int movery(){
if (distanciaCursor >= 25 || distanciaCursor <= 37){
if (distanciaCursor >= 38 || distanciaCursor <= 50){
return y;

long obtener_distanciaCursor(void)
digitalWrite(trigPinCursor, LOW);

digitalWrite(trigPinCursor, HIGH);

digitalWrite(trigPinCursor, LOW);
duracionCursor = pulseIn(echoPinCursor, HIGH);

distanciaCursor = duracionCursor/58.2;


1 Answer 1


What is your hardware setup?

From your code, it looks like you're not using the pins trigPinBtn, echoPinBtn, trigPinScroll, echoPinScroll. Weird. I can't figure out:

  1. which ultrasound module you're using
  2. what do you expect, eg which movement(s) in front of which ultrasound sensor shall generate which mouse action(s)

I would recommand having an iterative approach:

  1. Play with your ultrasound sensor(s), eg understand properly how to wire them to the arduino, how to interface them (software/lib), what kind of values you can get from them, etc... This will depend on the HW module you have chosen (if any yet).
  2. Read the documentation that comes with the mouse lib of the Leonardo and play with it independently/with existing, working examples.

That is divide and conquer: understand each bit before trying to assemble them.

THEN, and only then, you will have a better understanding of the different part of your project. You will then be able to formulate an overview of your project goals, and how you can do it. That is, start documenting your project before it is done: doing so will force help you to have a clear idea of what it is that you're trying to achieve... and thus how to achieve it.

But yeah, the overall project might look something like this:

// Ultrasound distance sensor #1, hooked to the arduino with xxx and yyy; generates movement along the x axis
// Ultrasound distance sensor #2, hooked to the arduino with zzz and ttt ; generates movement along the y axis
// Something for the wheel? The buttons?

//global values & objects
//initialize hardware& libs

//get raw ultrasound distances
//translate these into x and y movements
//do something about buttons & wheel?
//update mouse values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.