<< return to Pixycam.com

User Tools

Site Tools


wiki:v2:hooking_up_pixy_to_a_microcontroller_-28like_an_arduino-29

Hooking up Pixy to a Microcontroller (like an Arduino)

Pixy is meant to talk to a microcontroller.

Out of the box, Pixy2 is 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 100 detected objects per frame (Pixy can process 60 frames per second.)

OK, to get Pixy and Arduino talking to each other, use the supplied Arduino cable to connect Pixy to your Arduino.

Note: if you're using Arduino Nano, the ribbon cable faces the interior of the Nano, it doesn't exit off the side like on the Uno (pictured).
If the cable is plugged in backwards, you won't be able to upload to the Nano, or have serial communication with it.

ce5166e65a245cce097741efef288fe09b08da8a.jpg

Next, download the latest Arduino library “arduino_pixy2-x.y.z.zip” here. Bring up the Arduino IDE and import the Pixy library by selecting Sketch➜Include Library➜Add .ZIP Library… (or if you're using an older version Sketch➜Import Library) in the Arduino IDE, and then browsing to the Arduino zip file that you just downloaded.

Next, load the “ccc_hello_world” example by selecting it in File➜Examples➜Pixy2. Upload it and bring up the Serial Monitor which can be found in Tools➜Serial Monitor. Also be sure to set the Serial Monitor baud rate to 115200 baud, which is selectable at the bottom of the Serial Monitor window.

You should see messages printed in the Serial Monitor window that look similar to this:

Detected 1
  block 0: sig: 1 x: 160 y: 111 width: 140 height: 135 index: 38 age: 42
Detected 1
  block 0: sig: 1 x: 160 y: 112 width: 140 height: 136 index: 38 age: 43
Detected 1
  block 0: sig: 1 x: 160 y: 111 width: 140 height: 136 index: 38 age: 44
Detected 1
  block 0: sig: 1 x: 160 y: 111 width: 140 height: 135 index: 38 age: 45
Detected 1
  block 0: sig: 1 x: 162 y: 109 width: 140 height: 133 index: 38 age: 46
Detected 1
  block 0: sig: 1 x: 162 y: 109 width: 140 height: 135 index: 38 age: 47
...

Note, this example will only print messages if Pixy is running the “color_connected_components” program and an object that matches one of its color signatures is visible. This is what PixyMon looks like when Pixy is running the color_connected_components program and it has detected objects:

Arduino API

Using Pixy with Arduino is really simple. You simply include the SPI and Pixy headers:

#include <Pixy2.h>

And make a global instance of Pixy by putting this little guy outside your setup() and loop() functions:

Pixy2 pixy;

The most important method in the Arduino library is getBlocks(), which returns the number of objects Pixy has detected. You can then look in the pixy.ccc.blocks[] array for information about each detected object (one array member for each detected object.) Each array member (i) contains the following fields:

  • pixy.ccc.blocks[i].m_signature The signature number of the detected object (1-7 for normal signatures)
  • pixy.ccc.blocks[i].m_x The x location of the center of the detected object (0 to 316)
  • pixy.ccc.blocks[i].m_y The y location of the center of the detected object (0 to 208)
  • pixy.ccc.blocks[i].m_width The width of the detected object (1 to 316)
  • pixy.ccc.blocks[i].m_height The height of the detected object (1 to 208)
  • pixy.ccc.blocks[i].m_angle The angle of the object detected object if the detected object is a color code (-180 to 180).
  • pixy.ccc.blocks[i].m_index The tracking index of the block
  • pixy.ccc.blocks[i].m_age The number of frames the block has been tracked.
  • pixy.ccc.blocks[i].print() A member function that prints the detected object information to the serial port

So it's simple to talk to Pixy2 with your Arduino! For more information on the Arduino Library and API, go here.

Updating Pixy Library for Arduino

Before installing a new version of the Arduino Library, it's recommended that you delete the existing library. To do this, you can go into your C:\Users\<yourname>\Documents\Arduino\libraries (or similar directory, <yourname>/Documents/Arduino in OSX and Linux) and remove the Pixy2 directory. Then re-run the Arduino IDE.

Troubleshooting

If you get the message “error: no response” from the Arduino serial monitor, first make sure your Pixy2 is running the ccc (color connected components) program from PixyMon, and that you have taught it an object as described here.

Using Arduino with other devices

If you're having problems with parts of your program not working, and you're using motors or something else connected to the Arduino pins, make sure that your other devices aren't connected to pins 10, 11, 12, or 13. Pixy2 uses those pins for communicating with Arduino.

Zumo Errors

If you're using Arduino Due, you may get an error like this when compiling:

libraries\Pixy2\ZumoBuzzer.cpp:90:5: error: expected constructor, destructor, or type conversion before '(' token

In that case, just go into your Pixy library and delete the following 4 files:

It should then compile!

wiki/v2/hooking_up_pixy_to_a_microcontroller_-28like_an_arduino-29.txt · Last modified: 2020/09/18 00:52 by jfrench