Thursday, September 4, 2008

Working with the PixArt camera directly

This has been a pretty whirlwind past few months. Lots of things have happened, almost none of which procrastineering related which is why I haven't posted anything here. But, one of the things that I have poked at in the past few weeks was creating a PixArt to USB-HID device which allows the camera from the Wiimote to appear as a relatively easy to access USB device. This addresses several problems with using the Wiimote such as running off batteries for extended periods and flakey platform specific Bluetooth drivers. It's also possible to read from the Pixart cam at over 100Hz if you read directly via I2C as well as track visible dots once you remove the IR filter. Of course, none of this was discovered by me. All credit belongs to the numerous individuals who have contributed thier knowledge to the various Wiimote hacking websites. Normally, this project wouldn't be worth a post, but all the information on how to do this is pretty scattered and difficult to follow. So, I figured I would contribute by trying to making this all a bit clearer.

This project is fairly advanced. You must be comfortable with working with microcontrollers. Several simpler devices such as the Arduino or the Basic Stamp may work, but I used the 18F4550 PIC Microcontroller which provides built-in full-speed USB capabilites. But first, let talk about the PixArt camera:

Here's the pinout thanks to kako and a PCB picture. The Reset pin is active low, so use a pullup resistor to Vcc. The Wiimote runs the camera with a 25Mhz clock, but it also works with a 20Mhz clock so you might get away with fudging this a bit. The I2C communication is fast 400Khz and the slave device address is 0xB0. Most microcontroller development platforms should include I2C communication capabilities. If yours doesn't, get a better dev kit =o). Desoldering the camera can be hard with so many pins. But, careful use of a hot air gun will do the trick. The first part is to initialize the camera over I2C. Here's the pseudo code for initializing to maximum sensitivity (actual CCS C code in comments):

  1. write(hex): B0 30 01
  2. wait 100ms
  3. write(hex): B0 00 00 00 00 00 00 00 90 //sensitivity part 1
  4. wait 100ms
  5. write (hex): B0 07 00 41 //sensitivity part 2
  6. wait 100ms
  7. write(hex): B0 1A 40 00 //sensitivity part 3
  8. wait 100ms
  9. write(hex): B0 33 03 //sets the mode
  10. wait 100ms
  11. write(hex): B0 30 08
  12. wait 100ms

It's still somewhat mysterious to me what all these mean, but in this mess is the sensitivity and mode settings described at Wiibrew. The above code uses the sensitivity setting suggested by inio "00 00 00 00 00 00 90 00 41, 40 00" experssed in the 2nd, 3rd, and 4th message. The wait times are conservatively long. After you initialize, you can now read samples from it:

  1. write(hex): B0 37 //prepare for reading
  2. wait 25us
  3. write(hex): B1 //read request
  4. read 8 bytes
  5. wait 380us
  6. write(hex): B1 //read request
  7. read 4 bytes

This yeilds one sample from the camera containing 12 bytes, 3 for each of the 4 potential points. The format of the data will be the Extended Mode (X,Y, Y 2-msb, X 2-msb, Size 4-bits). The wait timings approximate what the Wiimote does. I've called this routine 1000 times per second without ill effect. Though, I doubt this is actually scanning the sensor and instead is just reporting the contents of an interal buffer. But, people claim 200Hz updates are possible. So, you can use that as a suggestion.

Hooking this up to your microcontroller is pretty straight forward. Give the camera 3.3v power using a voltage regulator, ground, a 20-25Mhz clock, and connect the SDA and SCL lines (don't forget your pull up resistors), and pull up the reset pin.

The CCS C Compiler for the PIC18F4550 includes USB-HID sample code. It's simply a matter of stuffing the data you got from the PixArt camera into the input report buffers for the USB. With this, you could actually create a USB mouse profile and make it control the cursor without any software or drivers at all. If set it up as a full speed device, it's possible to get 1ms reports providing extremely low latency updates. CCS provides relatively affordable PIC programmers as well. Explaining how to set all this up is not within the scope of this post, but it should be plenty to get you started. If you want to make a PCB, you can try ExpressPCB which can get you boards in-hand for as low as $60.

Update 9/6/08: Just a note about the clock. Since my PIC was using a 20Mhz resonator, I just piggy backed the Pixart clock pin off the OSC2/CLKO pin of the PIC which seemed to work fine. Also, Kako has more details (in Japanese) on doing this with an Arduino

50 comments:

  1. void initPixArtCam3(void)//max from inio
    {

    i2c_start();
    i2c_write(0xB0); i2c_write(0x30); i2c_write(0x01);
    i2c_stop();
    delay_ms(100);

    i2c_start();
    i2c_write(0xB0);
    i2c_write(0x00); i2c_write(0x00);
    i2c_write(0x00); i2c_write(0x00);
    i2c_write(0x00); i2c_write(0x00);
    i2c_write(0x00); i2c_write(0x90);
    i2c_stop();
    delay_ms(100);

    i2c_start();
    i2c_write(0xB0);
    i2c_write(0x07); i2c_write(0x00);i2c_write(0x41);
    i2c_stop();
    delay_ms(100);


    i2c_start();
    i2c_write(0xB0);
    i2c_write(0x1A); i2c_write(0x40);i2c_write(0x00);
    i2c_stop();
    delay_ms(100);


    i2c_start();
    i2c_write(0xB0);
    i2c_write(0x33); i2c_write(0x03);
    i2c_stop();
    delay_ms(100);

    i2c_start();
    i2c_write(0xB0); i2c_write(0x30); i2c_write(0x08);
    i2c_stop();
    delay_ms(100);

    }


    void readPixArtCam(void){
    i2c_start();
    i2c_write(0xB0); // Device address
    i2c_write(0x37); // Data to device
    i2c_stop();
    delay_us(25);

    i2c_start(); // Restart
    i2c_write(0xB1); // to change data direction
    out_data[0]=i2c_read(); // Now read from slave
    out_data[1]=i2c_read(); // Now read from slave
    out_data[2]=i2c_read(); // Now read from slave
    out_data[3]=i2c_read(); // Now read from slave
    out_data[4]=i2c_read(); // Now read from slave
    out_data[5]=i2c_read(); // Now read from slave
    out_data[6]=i2c_read(); // Now read from slave
    out_data[7]=i2c_read(0); // Now read from slave
    i2c_stop();

    delay_us(380);

    i2c_start(); // Restart
    i2c_write(0xB1); // to change data direction
    out_data[8]=i2c_read(); // Now read from slave
    out_data[9]=i2c_read(); // Now read from slave
    out_data[10]=i2c_read(); // Now read from slave
    out_data[11]=i2c_read(0); // Now read from slave
    i2c_stop();


    }

    ReplyDelete
  2. awesome! now what we need is a cylindrical lens to widen the field of view and we could mount it on top of a short throw projector.

    ReplyDelete
  3. Hey Johnny,

    Is it possible for you to post a schematic of how the crystal should connect to the IR camera?

    Thanks,

    NP

    ReplyDelete
  4. Does that camera have hardware tracking as part of the component by it's self? If so, how fast can you get it to track? Are you still limited to the original 100hz over Bluetooth?

    Even so, that's a very cool camera. If I can get my hands on a Wii remote at GoodWill (not likely) or something, I will definitely give this a try.

    ReplyDelete
  5. The info at WiiBrew is ancient. The Wiimote Project Wiki has much more detailed sensitivity configuration info

    http://wiki.wiimoteproject.com/IR_Sensor

    ReplyDelete
  6. Hi,
    I tried your protocol with the Renesas 2377 using I2c.
    However, it keeps returning me with 0xFF.

    Is there a initialization sequence before i start writing sensitivity, mode or reading?

    Does anyone faces the same problem?

    Ken

    ReplyDelete
  7. Hi Jhonny.
    Im Túlio Cunha from Vitória (Brazil). I Would like to invite you to do a participation with me on the II Biennali of Sea here in Vitória. The proposal of the work are to urban artalong the Avenue Mal Mascarenha de Moraes (http://maps.google.com/maps?f=q&hl=pt-BR&geocode=&q=vitoriA&ie=UTF8&ll=-20.322273,-40.333235&spn=0.00994,0.013819&t=h&z=16&iwloc=addr)
    Please contact me for more details: tulio.cunha@hotmail.com or 55 27 92453913.
    Please send me or contact too.
    Im posgraduating student of urbanism in UFES.
    Thank you.
    You are well come.

    ReplyDelete
  8. What could you do with the Fitbit Tracker?
    http://fitbit.com/faq

    ReplyDelete
  9. hi~~~

    we can use your idea to build new featrue like the computer technologies in the film "iron man"

    ReplyDelete
  10. HI there like Jianliang i use your code, but i use in a self made board and code in asseembly, and i can only get 255(0xFF) in all responses.

    Does some one have any idea of the motive? ps i use a 12Mhz cristal but im almost sure its not the problem.

    ReplyDelete
  11. Hi Johnny!

    Have tried to use the Pixart cam and the accelerometer together?
    I mean, its possible to connect the accelerometer directly to a PIC?

    Many thanks from Buenos Aires.
    Federico

    ReplyDelete
  12. hi BioBaguear i have done this alot of time white pic's

    The sensor is only 3 analog output's
    bing 2,5v-0G 5v-3G and 0v-(-)3G

    heres an exemple http://www.pyroelectro.com/projects/car_gmeter/

    ReplyDelete
  13. Ricardo thanks for the information.
    I have another question maybe someone knows. Its possible to connect the wiimote directly to the pc via serial? I need to keep a constant sampling rate, so I think that the bluetooth would be a big problem.
    Maybe the best its to take out the cam and the accelerometer and make my own device.
    What do you think?

    ReplyDelete
  14. yes thats the best option to the fastest update rate.

    Just find yourself a pic whit i2c and 3 analog inputs plus usb. Then ask some samples from the website.

    ReplyDelete
  15. Hello !

    Amazing work !
    Does anybody has schematic of the circuit? (Pixart Cam to USB)

    Thank you in advance, contact me at milou645@hotmail.com and let's share some experiences ;-)

    Emilien

    ReplyDelete
  16. Ken, Ricardo, we ran into the same problems. It appears to be solved by asking for location 0x36 instead of 0x37 (and throwing away the first byte) inside the polling loop.

    There is no explanation on why 0x37 doesn't work (while it does work in the polling loop for the Wiimote), yet there it is.

    ReplyDelete
  17. Todo un conjunto de sistemas muy utiles...... y muy interesantes que serian muy buenos en paìses en desarollo.....

    Visiten estas paginas.....

    http://www.kokoa.espol.edu.ec/

    http://www.espol.edu.ec/

    ReplyDelete
  18. The schematic would be a great help to everyone looking at doing this :) any chance?

    Thx

    SC

    ReplyDelete
  19. Hi all,
    Has anyone tried to take apart the accelometers from the wiimate and connect them to a microprocessor. It seems like a waist to open the wiimote and just take apart the cam

    Thanks
    Bobster23

    ReplyDelete
  20. Hi everyone,
    I´m still working with the camera.
    I was able to get data from the camara with the pic, but I don´t know why I get a constant 00h in the X0 data register.
    Do you think that it´s a configurational problem?
    I´m using C18 instead of CCS like Johnny did.
    Interestingly, X0 its the first data after sending the comand for changing the data direction.

    Many thanks in advance.
    Federico

    ReplyDelete
  21. I don't have much patience working with Assembly Language-like Instruction Set Architectures. You can buy ready to use devices from wholesale general merchandise shops without having to tweak these stuff.

    ReplyDelete
  22. I love your articles, they are really wonderful. And welcome to my blog. We have the same opinions on something.
    How do you think of polo cotton shirt? Do you want to be a fashion chaser? In our store online you can chase the most popular ralph lauren women shirts. it can help you increase your attraction.
    My friends and I also want to buy excellent tennis prince, and I know there is a perfect store recently, which sells perfect tennis rackets.In order to play the game and search the place where can buy the
    tennis racket, we find many places, and finally, we find tennis set shop.
    would you want to be more attractive? If your answer is "Yes", you can wear spyder
    jackets
    , you really need a pair of north face jacket. They can make you become the focus.
    women's polo hoodies
    men's polo shirts
    spyder jacket
    head racquets
    babolat racquets
    wilson racquet
    ralph lauren t shirt
    polo women shirts
    black polo
    prince tennis accessories
    discount tennis racquets
    wilson tennis racquets

    ReplyDelete
  23. If so, how fast can you get it to track? Are you still limited to the original 100hz over Bluetooth?
    Buy Dissertation | Buy Essay | Buy Research Paper

    ReplyDelete
  24. I Would like to invite you to do a participation with me on the II Biennali of Sea here in Vitória.
    Buy Term Paper | Buy Thesis

    ReplyDelete
  25. I was able to get it working on my Microchip PIC24FJ64GA002 using the I2C peripheral libraries.

    I sent the request command 0x37 as suggested by Johnny Chung Lee and it seemed to work.

    I have posted the source code on my website along with a few pictures of problems I encountered:

    http://web.uvic.ca/~gleung86/courses/499/tracking/

    Hope it helps!

    ReplyDelete
  26. NYC limousine service Pickups is a full NYC limos service Company Serving Tri State NYC- CT and NJ with luxuries sedans, Stretch SUV, and New York city limos. NYCLimopickups is the preferred for individuals who demand personal service and a professional transportation experience at reasonable rates. Limo service nyc offers 24 - hour service with advance reservations. NYCLimopickups is rated #1 as New york city limousine and New York Limousine services

    ReplyDelete
  27. A lot of very smart people have written in questions and suggestions and wmv to iphone
    avi to iphone converter I think a lot of them are very clever and worth sharing... and I'd

    ReplyDelete
  28. Breitling Bentley have been the de facto standard for aviators across the entire planet for over 100 years now, and with good reason. Not only do they look the business but Navitimer Watches have a whole range of features built in that just make a pilots life easier. The Navitimer World emergency watch, as well as being a very attractive Montbrillant Datora , uniquely has a built-in transmitter that operates on the international aviation distress frequency of 121.5 Mhz.


    The moment you put on Christian Louboutin Sale they go on to become a wardrobe enhancer and put you several notches above on the ladder of success. The wearing of the Christian Louboutin Pumps enables one to anchor and team Christian Louboutin Sandals with the very best of people and things available. Christian Louboutin Boots can be found in different color tones and can be matched just so very right with the entire outfit and the look that you want to portray.


    The quality of Coach Wallets is uncompared and your purse will last a life time and look good all through the years. Coach Hamptons are fashionable and can match just about any outfit. The Coach Legacy leather is exquisite, the Coach Luggage locks with a logo letter C and the style is just what every woman wants to have.

    ReplyDelete
  29. Hello everybody, This is such a great resource that you are providing and you give it away for free. i Would like to thanks to you

    custom writing service
    custom writing
    custom writings
    professional writing service
    professional writing

    ReplyDelete
  30. Very nice article I would like to visit this article. keep posting such a nice post.




    .....Alex
    generic viagra

    ReplyDelete
  31. sinema filmleri izle film izle
    sinema filmleri izle dizi izle
    lig tv maç izlettir lig tv izle
    maç izle lig tv izle lig tv izle

    canlı maç dinle izle canlı maç izle
    Canlı maç izle canlı maç izle

    sikiş izle sikiş izle

    sikiş izle sikiş izle
    sikiş sikiş
    sikiş izleme sikiş
    sex izle sex izle
    sikiş yeni sikiş filmleri
    redtube sikiş redtube sikiş

    sikiş sikiş
    sikiş yeni sikiş filmleri
    sikiş amatör sikiş
    sikiş gizli sikiş
    sikiş kızlık bozma

    sikiş sikiş
    porno porno izle
    porno porno izle
    sikişsikiş
    sikiş izle sikiş izle
    sikiş izle sikiş izle
    sikiş amcık

    ReplyDelete
  32. great article. keep posting.

    hardian

    ReplyDelete
  33. Looking for luxury watches?replica Movado Watches on sale,fashion TAG Heuer Watches sale online,new Panerai Watches for sale,cheap TAG watches sale with high quality.Burberry discount at Burberry bag shop burberry4handbags.com. There are lots of Burberry bags and Burberry handbags.

    ReplyDelete
  34. These kind of post are always inspiring and I prefer to read quality content so I happy to find many good point here
    in the post..
    Thanks for sharing me...reptile answers

    ReplyDelete