NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » Read serial mouse data

February 17, 2012
by Jalex
Jalex's Avatar

I would like to know where I can find code for reading the serial data from a PC wheel type mouse. Eventually I would like to read wireless data from the mouse. I want to sense rotation direction in degrees and because mice are cheap I thought it was a good place to start.

February 17, 2012
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Jalex,

I don't know where you will find code that already does this for you, but reading the data of a PS/2 mouse sounds like a pretty cool project that is certainly possible for you to take on. The first thing to do would be to take a look at our keyboard tutorial. The electrical protocol for PS/2 mice and keyboard are the same. Next read this writeup about reading a PS/2 mice and keyboard, it should have all the information you need to get you started.

Humberto

February 17, 2012
by Jalex
Jalex's Avatar

Hi Thanks for the info I haven't built the Keyboard kit as yet and I think I would learn a lot about how to do this when I master that.

February 17, 2012
by pcbolt
pcbolt's Avatar

Jalex -

I was able to interface with a PS/2 mouse fairly easily. The links Humberto posted were invaluable. My project photos are posted here - Forum link

The major difference between the PS/2 keyboard and the PS/2 mouse is the mouse needs to both send and receive messages from the MCU. The listening part Humberto and Mike did in the Keyboard tutorial. The send part is outlined in the link from the previous post. I think to get wireless to work, you would need to use USB which is more complicated and code heavy (but still possible). Another think to keep in mind if you go the PS/2 route, some but not all USB mice are PS/2 compatible and wiring is straigtforward.

I agree completely about the mouse being a handy interface to the MCU. You basically have 3 buttons (total of 8 combinations) and three movement sensors. I'm in the process of building a code library for the mouse and if you are interested I'll try to post it (maybe in the library section). It will take a little time though, I'm in nearing the end of making an SD card reader for the Atmega168 and I can't seem to break away when I'm so close to finishing.

February 17, 2012
by Ralphxyz
Ralphxyz's Avatar

pcbolt, both the PS/2 mouse and the SD card reader would be nice additions to the Nerdkit Community Library.

Ralph

February 17, 2012
by pcbolt
pcbolt's Avatar

Ralph -

As long as you don't laugh at some of my spaghetti code, I don't think it would be a problem :-) It may take a couple of weeks since I have a business trip to attend to (and I can't take my Nerdkit with me).

February 17, 2012
by Ralphxyz
Ralphxyz's Avatar

Believe me, no laughing or scoffing allowed!! Everything is appreciated you just never know what will get someone's attention.

Also the Library is setup as a WiKi meaning that someone else can edit your spaghetti code if they want to contribute.

You might publish it and they would take it and fix it and then publish their improved version over your original.

I like the idea of using a mouse with the mcu and of course a SD reader would be neat.

Ralph

February 19, 2012
by pcbolt
pcbolt's Avatar

Ralph -

Took your suggestion about the mouse project and posted to the library Here.

I "finished" the SD Card reader/writer but am a long way from fully testing it. I'd hate to make a post about it until I'm sure it will "do no harm". All in all it is not to hard IF you just want store data and retrieve it with a MCU. The real bear is trying to use it so a computer can read/write as well as the MCU. This takes you into FAT32 territory and is tricky when dealing with limited MCU resources.

I'll start a new thread on it next week.

February 19, 2012
by Ralphxyz
Ralphxyz's Avatar

Ah that's great thank you, looks good.

Re: the SD card couldn't you avoid the FAT32 hassel by just writing raw data to the SD card from the mcu and then have a Python (or other language) translate the data to windows format? Such as is done when populating a Excel datasheet from the mcu using a serial feed.

As long the Python program "knows" what to expect it should be able to handle the conversion.

I am just speaking of numbers here possible limited text but primarily numbers. The main thing is to have a "intelligent" program running on the pc to do the translation.

Ralph

February 19, 2012
by pcbolt
pcbolt's Avatar

Ralph -

That can be done very easily. You would not even need a Python script, just read the data from the card and output through the UART to any type of program that needs it. The reason I wanted to use a predefined file format is to operate the MCU remotely, without any connection to the computer. Then I'd be able to save data to the card, take the card out when I'm done and read it from any computer. Also, if you want to save data to the card from a PC then take it to the MCU "in the field", you could do that as well.

Now if Linux is able to access the SD Card at the "raw" level, that would be the way to go. I know Windows won't let you do that without writing your own driver. I just don't know enough abut Linux (or writing Windows drivers) to go that route. It would save MCU program space though...I think I'm around 4K with the FAT32 code.

Post a Reply

Please log in to post a reply.

Did you know that interrupts can be used to trigger pieces of code when events happen? Learn more...