Hooking up Pixy to a Microcontroller like an Arduino

Version 47 (Jesse French, 03/04/2016 06:34 pm)

1 47 Jesse French
h1. NOTE: this page is deprecated, please find the current version [[Hooking up Pixy to a Microcontroller (like an Arduino)|here]]!
2 47 Jesse French
3 47 Jesse French
4 47 Jesse French
h2. Hooking up Pixy to a Microcontroller like an Arduino
5 1 Rich LeGrand
6 20 Rich LeGrand
Pixy is meant to talk to a microcontroller, and Pixy comes out of the box ready to talk to an Arduino. It sends block information to Arduino at 1 Mbits/second which means Pixy can send more than 6000 detected objects per second or 135 detected objects per frame (Pixy can process 50 frames per second.)  
7 1 Rich LeGrand
8 20 Rich LeGrand
OK, to get Pixy and Arduino talking to each other, use the supplied Arduino cable to connect Pixy to your Arduino.  
9 20 Rich LeGrand
10 15 Rich LeGrand
!{width: 35%}http://i74.photobucket.com/albums/i241/cmucam/IMG_4521_zpsb5aa9a88.jpg!
11 1 Rich LeGrand
!{width: 35%}http://i74.photobucket.com/albums/i241/cmucam/IMG_4519_zpse7e5ea91.jpg!
12 1 Rich LeGrand
13 46 Jesse French
Next, download the Arduino library "arduino_pixy-x.y.z.zip" "here":/projects/cmucam5/wiki/Latest_release. Bring up the Arduino IDE and import the Pixy library by selecting *Sketch➜Import Library* in the Arduino IDE, and then browsing to the Pixy.zip file that you just downloaded.  
14 1 Rich LeGrand
15 19 Rich LeGrand
Next, load the "hello_world" example by selecting it in *File➜Examples➜Pixy*.  Upload it and bring up the *Serial Monitor*.  You should see messages printed that look similar to this:
16 1 Rich LeGrand
17 2 Rich LeGrand
<pre>
18 1 Rich LeGrand
Detected 1:
19 1 Rich LeGrand
  block 0: sig: 1 x: 159 y: 109 width: 61 height: 61
20 1 Rich LeGrand
Detected 1:
21 1 Rich LeGrand
  block 0: sig: 1 x: 173 y: 114 width: 60 height: 61
22 1 Rich LeGrand
Detected 1:
23 1 Rich LeGrand
  block 0: sig: 1 x: 146 y: 111 width: 70 height: 65
24 4 Rich LeGrand
... 
25 1 Rich LeGrand
</pre>
26 3 Rich LeGrand
27 12 Rich LeGrand
Note, this example will only print messages if Pixy is running the "default program" and an object that matches one of its color signatures is visible. 
28 1 Rich LeGrand
29 37 Rich LeGrand
h3. *Arduino API*
30 6 Rich LeGrand
31 6 Rich LeGrand
Using Pixy with Arduino is really simple. You simply include the SPI and Pixy headers:
32 6 Rich LeGrand
33 6 Rich LeGrand
<pre>
34 6 Rich LeGrand
#include <SPI.h>  
35 6 Rich LeGrand
#include <Pixy.h>
36 6 Rich LeGrand
</pre>
37 6 Rich LeGrand
38 23 Rich LeGrand
And make a global instance of Pixy by putting this little guy outside your setup() and loop() functions:
39 6 Rich LeGrand
40 6 Rich LeGrand
<pre>
41 6 Rich LeGrand
Pixy pixy;
42 6 Rich LeGrand
</pre>
43 6 Rich LeGrand
44 11 Rich LeGrand
The API consists of one call: @getBlocks()@, which returns the number of objects Pixy has detected.  You can then look in the @pixy.blocks[]@ array for information about each detected object (one array member for each detected object.)  Each array member (@i@) contains the following fields:
45 6 Rich LeGrand
46 6 Rich LeGrand
* @pixy.blocks[i].signature@  The signature number of the detected object (1-7) 
47 6 Rich LeGrand
* @pixy.blocks[i].x@  The x location of the center of the detected object (0 to 319)
48 6 Rich LeGrand
* @pixy.blocks[i].y@  The y location of the center of the detected object (0 to 199)
49 6 Rich LeGrand
* @pixy.blocks[i].width@  The width of the detected object (1 to 320)
50 1 Rich LeGrand
* @pixy.blocks[i].height@  The height of the detected object (1 to 200)
51 1 Rich LeGrand
* @pixy.blocks[i].print()@  A member function that prints the detected object information to the serial port 
52 9 Rich LeGrand
53 14 Rich LeGrand
So it's simple to talk to Pixy with your Arduino!  
54 6 Rich LeGrand
55 37 Rich LeGrand
h3. *Other Microcontrollers or Devices*
56 1 Rich LeGrand
57 29 Rich LeGrand
Pixy will output objects that it detects through one of several interfaces that you choose, and it will do this every 20 ms.  It supports SPI, I2C, UART, and analog/digital I/O through its 10-pin I/O connector.  Pixy also supports USB 2.0 through its mini-USB connector.  You can configure which interface Pixy uses through the configure dialog in PixyMon.  The "Data out port" parameter in the "Interface" tab determines the output port.  
58 3 Rich LeGrand
59 25 Rich LeGrand
!http://i74.photobucket.com/albums/i241/cmucam/Image201_zpsa127f29e.jpg! 
60 1 Rich LeGrand
61 1 Rich LeGrand
If you hover the mouse pointer over the "Data out port" text, a help string will be displayed that describes which value corresponds to which type of port.  
62 3 Rich LeGrand
63 38 Rich LeGrand
* 0: SPI - this is the default port that uses 3 wires (pins 1, 3, and 4 of the I/O connector) and is used to communicate with Arduino
64 38 Rich LeGrand
* 1: I2C - this is a multi-drop 2-wire port (pins 5 and 9 of the I/O connector) that allows a single master to communicate with up to 127 slaves (up to 127 Pixys).  
65 38 Rich LeGrand
* 2: UART - this is the common "serial port" (pins 1 and 4 of the I/O connector).  Pixy receives data via pin 1 (input) and transmits data via pin 4 (output).  
66 38 Rich LeGrand
* 3: analog/digital x - this will output the x value of the largest detected object as an analog value between 0 and 3.3V (pin 3). It also outputs whether an object is detected or not as a digital signal (pin 1 of the I/O connector).
67 38 Rich LeGrand
* 4: analog/digital y - this will output the y value of the largest detected object as an analog value between 0 and 3.3V (pin 3). It also outputs whether an object is detected or not as a digital signal (pin 1 of the I/O connector).
68 4 Rich LeGrand
69 35 Rich LeGrand
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/Image140_zps87c8d289.jpg!
70 30 Rich LeGrand
71 43 Rich LeGrand
h2. *Technical notes on the interfaces*
72 30 Rich LeGrand
73 36 Rich LeGrand
All serial interfaces use the same [[Pixy Serial Protocol|protocol]].  It may be helpful the refer to the "Pixy's schematic":http://cmucam.org/documents/26. 
74 32 Rich LeGrand
75 30 Rich LeGrand
h3. *SPI*
76 1 Rich LeGrand
77 36 Rich LeGrand
The SPI interface operates as an SPI slave.  It is designed around the Arduino's ICSP port, which doesn't have a slave select signal.  The default data rate is 1 mbits/sec, but this can be increased by modifying the Pixy.h file in the Pixy Arduino library.  The protocol has checksums to deal with bit errors, but bear in mind that the ribbon cable isn't shielded!
78 30 Rich LeGrand
79 1 Rich LeGrand
h3. *I2C*
80 1 Rich LeGrand
81 1 Rich LeGrand
* The I2C interface operates as an I2C slave and requires polling.  
82 36 Rich LeGrand
* There are somewhat weak 4.7K pullups to 5V on SDA and SCL signals, via R14 and R15.
83 36 Rich LeGrand
* I2C signals are 5V tolerant 
84 38 Rich LeGrand
* The I2C address can be configured in the "Interface" tab of the Configure Parameters dialog in PixyMon 
85 1 Rich LeGrand
86 35 Rich LeGrand
There is an Arduino example that uses I2C.  Run it by selecting *File&#10140;Examples&#10140;i2c* in the Arduino IDE.  You'll also need to make a special cable that connects:
87 35 Rich LeGrand
88 1 Rich LeGrand
# Arduino SDA to pin 9 of Pixy's I/O connector 
89 35 Rich LeGrand
# Arduino SCL to pin 5 of Pixy's I/O connector
90 44 Rich LeGrand
# Arduino GND to pin 6, 8 or 10 of Pixy's I/O connector
91 1 Rich LeGrand
92 38 Rich LeGrand
When talking to more than one Pixy, you will need to configure a different I2C address for each Pixy so they don't step on each other. You can make a "multi-crimp cable", meaning you can take a 10-conductor ribbon cable and crimp N 10-pin IDC connectors to it and plug into to your N Pixys.  That is, when selecting I2C as an interface, all signals on Pixy's I/O connector go into a high-impedance state and won't interfere with each other, waste power, etc. 
93 1 Rich LeGrand
   
94 1 Rich LeGrand
h3. *UART serial*
95 1 Rich LeGrand
96 36 Rich LeGrand
* The UART interface is 8 data bits, 1 stop bit, no parity, no handshaking
97 38 Rich LeGrand
* The baudrate can be configured in the "Interface" tab of the Configure Parameters dialog in PixyMon
98 36 Rich LeGrand
* RX signal (pin 1) is 5V tolerant input
99 1 Rich LeGrand
* TX signal (pin 4) is 0 to 3.3V signal output
100 1 Rich LeGrand
* Baudrates up to 230 kbaud supported.
101 1 Rich LeGrand
102 1 Rich LeGrand
There is an Arduino example that uses UART serial.  Run it by selecting *File&#10140;Examples&#10140;uart* in the Arduino IDE.  You'll also need to make a special cable that connects:
103 1 Rich LeGrand
104 1 Rich LeGrand
# Arduino TX to pin 1 of Pixy's I/O connector 
105 1 Rich LeGrand
# Arduino RX to pin 4 of Pixy's I/O connector
106 44 Rich LeGrand
# Arduino GND to pin 6, 8 or 10 of Pixy's I/O connector
107 38 Rich LeGrand
108 42 Rich LeGrand
   
109 38 Rich LeGrand
h3. *Analog/digital output*
110 38 Rich LeGrand
111 40 Rich LeGrand
Pixy has a single analog (DAC) output, so there are two modes for analog/digital output.  Mode 3 outputs the x value of the center of the biggest detected object to pin 3 of the I/O connector.  Mode 4 outputs the y value of the biggest detected object to pin 3 of the I/O connector.  Pin 1 goes high (3.3V) when an object is detected, and low (0V) when no object is detected.
112 38 Rich LeGrand
113 38 Rich LeGrand
* Pin 1 is 0 to 3.3V signal output, can source/sink 5 mA.
114 38 Rich LeGrand
* Pin 3 is 0 to 3.3V signal output, roughly 200 ohm impedance.
115 38 Rich LeGrand
* Pin 3 voltage is directly, linearly proportional to the object position in the image (depending on the mode) 
116 38 Rich LeGrand
* In mode 3 (x mode) pin 3 is 0V if object is on the far left of the image and 3.3V is object on the far right (PixyMon perspective).
117 38 Rich LeGrand
* In mode 4 (y mode) pin 3 is 0V if object is on the bottom of the image and 3.3V is object on the top (PixyMon perspective).