Pixy I2C

I am trying to figure out how to interface a Pixy Camera with our Roborio using the i2C port. Does anyone have experience with the Pixy or i2C communications?

Also, can we instead use an Arduino as a mediator, we already wrote the code for the Arduino and It would be easier to do it via the Arduino to Roborio if possible.

My team got the Arduino communicating with the roborio over I2C using the following code on the roborio

//this is outside a method
I2C Wire = new I2C(Port.kOnboard, 4);

//this is inside a method
if (Global.driver.Buttons.Back.changedDown) {
	String WriteString = "go"; //this is being sent to the Arduino
	char] CharArray = WriteString.toCharArray();
	byte] WriteData = new byte[CharArray.length];
	for (int i = 0; i < CharArray.length; i++) {
		WriteData* = (byte) CharArray*;
	Wire.transaction(WriteData, WriteData.length, null, 0);

And this code on the Arduino which was made to turn on an LED when it received the message “go”

#include <Wire.h>

void setup()
  pinMode (13, OUTPUT);
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event

void loop()

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
  String LED = "";
  while ( Wire.available() > 0 )
    char n=(char)Wire.read();
    if(((int)n)>((int)(' ')))
   LED += n; 
  if (LED == "go") 
    digitalWrite (13, HIGH);


We are using pixy to lock onto yellow totes in AUTO. My team found it easier to decode packets via the serial port (connected to roboRIOC) than the I2C. We can share code if you need it.

I’d be interested in the code.

That would be great, if serial is possible it would be a lot easier.

I would also like to see said code.

Hi there. Here is what out team is using to pry information out of the pixy. I hope this helps! I’m not sure why the code block is anti-tabbing…

(the code gpetilli mentioned)

import edu.wpi.first.wpilibj.SerialPort;
import edu.wpi.first.wpilibj.SerialPort.Port;
//Warning: if the pixy is plugged in through mini usb, this code WILL NOT WORK b/c the pixy is smart and detects where it should send data
public class Pixy {
SerialPort pixy;
Port port = Port.kMXP;
PixyPacket] packets;
PixyException pExc;
String print;
public Pixy() {
pixy = new SerialPort(19200, port);
packets = new PixyPacket[7];
pExc = new PixyException(print);
//This method parses raw data from the pixy into readable integers
public int cvt(byte upper, byte lower) {
return (((int)upper & 0xff) << 8) | ((int)lower & 0xff);
public void pixyReset(){
//This method gathers data, then parses that data, and assigns the ints to global variables
public PixyPacket readPacket(int Signature) throws PixyException {
int Checksum;
int Sig;
byte] rawData = new byte[32];
rawData = pixy.read(32);
} catch (RuntimeException e){
if(rawData.length < 32){
System.out.println("byte array length is broken");
return null;
for (int i = 0; i <= 16; i++) {
int syncWord = cvt(rawData*, rawData*); //Parse first 2 bytes
if (syncWord == 0xaa55) { //Check is first 2 bytes equal a "sync word", which indicates the start of a packet of valid data
syncWord = cvt(rawData*, rawData*); //Parse the next 2 bytes
if (syncWord != 0xaa55){ //Shifts everything in the case that one syncword is sent
i -= 2;
//This next block parses the rest of the data
Checksum = cvt(rawData*, rawData*);
Sig = cvt(rawData*, rawData*);
if(Sig <= 0 || Sig > packets.length){
packets[Sig - 1] = new PixyPacket();
packets[Sig - 1].X = cvt(rawData*, rawData*);
packets[Sig - 1].Y = cvt(rawData*, rawData*);
packets[Sig - 1].Width = cvt(rawData*, rawData*);
packets[Sig - 1].Height = cvt(rawData*, rawData*);
//Checks whether the data is valid using the checksum *This if block should never be entered*
if (Checksum != Sig + packets[Sig - 1].X + packets[Sig - 1].Y + packets[Sig - 1].Width + packets[Sig - 1].Height) {
packets[Sig - 1] = null;
throw pExc;
//Assigns our packet to a temp packet, then deletes data so that we dont return old data
PixyPacket pkt = packets[Signature - 1];
packets[Signature - 1] = null;
return pkt;
public class PixyException extends Exception {
public PixyException(String message){
public class PixyPacket {
public int X;
public int Y;
public int Width;
public int Height;

1 Like

2B || !2B ,
I am the lead programmer on Mike’s team, thank you so much for your code, We are cutting it a little close to the deadline and this definitely helps.
Also, awesome name.

How exactly do we wire the Pixy, the stock cable does not seem like the correct one to connect directly to the RoboRio

In case you are a newer team, you can remove the roboRIO from your robot before you bag it and work on code any time there is a competition anywhere (Typically next 6 Thur, Fri, Sat).

Look at the Pixy Docs - i dont remember if they call it rs232 or serial port. roboRIO also has a serial port. I will ask our electrical team to post more details as to what they did.

Is this the sort of thing were we can put together our own cable, or will we need to use the actual serial cable? I don’t think we have one, so we would have to make one, probably out of a few scrapped PWM cables. I would find it really helpful to see a picture of your connection or what cable you used.

yes, you can make your own cable. I seem to remember needing the hook the PIXY up via USB to a laptop to turn on the serial output. Serial outputs have multiple names so look for serial, uart, or rs232.

Does anyone have a picture or instructions on how to connect the pixy to the Roborio using the ribbon cables?

Hey, what communication protocol are you using in this code?

If anyone needs pixy 1 I2C code here it is: https://github.com/Team558/FRC-2017/blob/master/src/org/usfirst/frc/team558/robot/subsystems/PixyCam.java I am trying to port this to pixy 2 in java.

It would be awesome if you could share the code with us.