As an Amazon Associate we earn from qualifying purchases.
This project turns hand movements into mouse movements on a computer, with no cable between you and the PC. An accelerometer senses how you tilt your hand, a PIC microcontroller turns that tilt into single-letter commands, and a Zigbee link carries them to the computer. On the PC, MATLAB reads the letters and moves the cursor.
It is a good learning project because it combines analog sensing, ADC readings, serial communication, wireless links and PC-side control in one build. Below you will find the video, the transmitter circuit, the full MATLAB and Mikro C code, and notes on getting it to behave.
How the wireless 3D mouse works
The system has two sides: a transmitter you hold and a receiver connected to the computer.
- Sensing: The accelerometer outputs an analog voltage for each axis (X, Y and Z). Tilting the board changes those voltages.
- Conversion: The PIC reads each axis with its ADC and compares the value to fixed thresholds.
- Encoding: When a threshold is crossed, the PIC sends one character over its UART: R, L, F, B, S or T.
- Wireless link: A Zigbee module on the transmitter sends those characters to a second Zigbee module on the PC side. The pair acts as a wireless replacement for a serial cable.
- Cursor control: MATLAB reads the characters from the COM port and uses Java’s Robot class to move the pointer or click.
What each character does
- R (ASCII 82): move the cursor right (tilt on the X axis)
- L (ASCII 76): move the cursor left
- F (ASCII 70): move the cursor up (forward tilt on the Y axis)
- B (ASCII 66): move the cursor down
- S (ASCII 83): right click (Z axis one way)
- T (ASCII 84): left click (Z axis the other way)
What you need
- An analog-output accelerometer module
- A PIC microcontroller with an ADC and a hardware UART
- Two Zigbee modules, one on each side, set to the same baud rate
- A USB-to-serial adapter or a Zigbee USB dongle for the PC end
- Mikro C (MikroC PRO for PIC) to compile the firmware
- MATLAB on the computer, with its built-in Java support
- A power source for the transmitter, such as a small battery with a regulator
If you have not used Zigbee modules before, our guide on Zigbee with Arduino explains the pairing and serial settings, which work the same way with a PIC.
PRACTICAL VIDEO
CIRCUIT OF TRANSMITTER
The transmitter connects the three accelerometer outputs to three ADC input pins of the PIC (channels 0, 1 and 2 in the code). The PIC’s UART transmit pin goes to the receive pin of the Zigbee module. Make sure the accelerometer and the Zigbee module run from voltages they support, since many Zigbee modules are 3.3 V devices.
On the PC side, the receiving Zigbee module appears as a COM port. Both modules and both programs must use the same settings: 9600 baud, 8 data bits, no parity and 1 stop bit.
SIMULATION
The code below is split into the PC-side MATLAB script and the PIC-side Mikro C firmware.
Matlab code
The script opens the serial port, starts the cursor at a fixed position, and then loops forever. Each received character moves the cursor by 3 pixels or triggers a click. Change COM6 to the port your receiver uses, and set screensizex and screensizey to your own screen resolution.
import java.awt.Robot;
import java.awt.event.*;
mouse = Robot;
s = serial('COM6');
set(s,'BaudRate',9600,'DataBits',8,'Parity','none','StopBits',1,'FlowControl','none','InputBufferSize', 1);
s.Terminator = 'CR';
s.BytesAvailableFcnMode = 'byte';
set(s, 'Timeout',1);
s.ReadAsyncMode = 'continuous';
fclose(s);
fopen(s);
s.BytesAvailable
display('Opened..........')
fprintf(s,'Press the required keys from the micro controller')
x=650;
y=350;
screensizex=1600;
screensizey=900;
while(1)
n=fread(s)
s.BytesAvailable
if (n==82) % R: move right
if (x<screensizex)&&(y<screensizey)
x=x+3
if (x>screensizex-1)
x=screensizex-1
end
mouse.mouseMove(x, y);
end
elseif(n==76) % L: move left
if (x<screensizex)&&(y<screensizey)
x=x-3
if (x<0)
x=0
end
mouse.mouseMove(x, y);
end
elseif(n==70) % F: move up
if (x<screensizex)&&(y<screensizey)
y=y-3
if (y<0)
y=0
end
mouse.mouseMove(x, y);
end
elseif(n==66) % B: move down
if (x<screensizex)&&(y<screensizey)
y=y+3
if (y>screensizey-1)
y=screensizey-1
end
mouse.mouseMove(x, y);
end
elseif(n==83) % S: right click
mouse.mousePress(InputEvent.BUTTON3_MASK);
mouse.mouseRelease(InputEvent.BUTTON3_MASK);
elseif(n==84) % T: left click
mouse.mousePress(InputEvent.BUTTON1_MASK);
mouse.mouseRelease(InputEvent.BUTTON1_MASK);
end
end
fclose(s);
delete(s);
clear s;
Two small fixes were made to the original script. The right and down branches used to check the wrong limit (a lower bound of zero), so the cursor could get stuck at the screen edge. They now stop one pixel before the edge of the screen.
MIKRO C CODE
The firmware reads each axis, compares it with a high and a low threshold, and sends one letter when a threshold is crossed. Movement letters use a short 20 ms delay, so holding a tilt sends a steady stream of commands. Click letters use a 300 ms delay so a single tilt does not trigger many clicks.
int a,b;
void delay1(void);
void main()
{
ADC_Init();
uart1_init(9600);
Delay_ms(100); // Wait for UART module to stabilize
UART1_Write_Text("Start");
while(1)
{
a = ADC_Read(0); // FOR X - AXIS
Delay_ms(1);
if(a>400) {UART1_write('R'); delay1();}
if(a<310) {UART1_write('L'); delay1();}
b = ADC_Read(2); // FOR Z - AXIS
Delay_ms(10);
if(b>500) {UART1_write('S'); Delay_ms(300);}
if(b<250) {UART1_write('T'); Delay_ms(300);}
b = ADC_Read(1); // FOR Y - AXIS
Delay_ms(10);
if(b>380) {UART1_write('F'); delay1();}
if(b<300) {UART1_write('B'); delay1();}
b = 0;
b = ADC_Read(2); // FOR Z - AXIS
Delay_ms(10);
if(b>500) {UART1_write('S'); Delay_ms(300);}
if(b<260) {UART1_write('T'); Delay_ms(300);}
}
}
void delay1(void)
{
Delay_ms(20);
}
The Z axis is read twice in each loop, once before the Y axis and once after, with slightly different lower limits (250 and 260). That works, but you can remove the second block if you want simpler code.
Tuning the thresholds
The numbers in the code (400, 310, 380, 300, 500, 250) are ADC readings tied to this particular accelerometer, supply voltage and mounting position. Yours will probably differ. To tune them:
- Print or send the raw ADC value for each axis while the board lies flat. These are your resting values.
- Tilt the board to the angle where you want the cursor to start moving and note the reading.
- Set the high and low thresholds a little either side of the resting value so small hand tremor does not move the cursor.
- Adjust delay1 to change cursor speed. A shorter delay sends commands faster.
If you want to see more accelerometer projects built on the same idea, the accelerometer based wheel chair and the earthquake alert system both use tilt and motion readings in a similar way. For more Mikro C practice on a PIC, try the scrolling display in Mikro C project.
Limitations and ideas for improvement
- The cursor moves at a fixed speed of 3 pixels per command. Mapping tilt angle to speed would feel more natural.
- The screen size is typed into the script. Reading it from MATLAB or Java instead would remove that step.
- The script starts the cursor at a fixed position rather than where it currently is.
- MATLAB’s serial function still works in many versions, but newer releases recommend serialport, so you may need to adapt the connection lines.
- The loop never exits cleanly, so the last three lines only run if you add a way to break out of it.
FAQ
Do I need MATLAB for this project?
Here it is used to read the serial port and control the mouse through Java’s Robot class. Other tools that can read a serial port and move the pointer could do the same job, but the code above is written for MATLAB.
Why use Zigbee instead of a wired connection?
Zigbee modules work as a wireless serial link, so the PIC code stays almost the same as for a wired UART. You only add the modules and keep the same baud rate on both sides.
Why does the cursor not move when I tilt the board?
Check that the COM port number in MATLAB matches your receiver, that both modules use 9600 baud, and that your ADC thresholds suit your accelerometer’s resting readings.
Can I use a different PIC or accelerometer?
Yes, as long as the PIC has an ADC and a UART and the accelerometer gives analog outputs. You will need to recheck the ADC channels and thresholds for your parts.