<< return to Pixycam.com

Pixy 2 with laser- arduino

im trying to do a project that tracks baloons with pixy by servos, and when the baloon is in the center, the laser will light on the baloon. I tried the example of pantilt in arduino and the servos are moving. but how can i do the thing with the laser? I saw there are x and y parameters, and i need to use them- and activate the laser while the baloon is centered.
If someone knows it can be great.


That sounds like an interesting project. It sounds like you want to do 2 things – track the balloon and point the laser.

If you line the laser up with Pixy’s center pixel it seems like you can do both simultaneously. That is, wherever Pixy looks the laser will be in the center of the image (or close to the center). Is that what you’re trying to do? If so, the ccc_pan_tilt demo should be a really good starting point because it will track and center the object, and the laser will point to the object also as long as it’s centered.

Hope this helps.


1 Like

Hi. The pan.tilt in arduino is working. But i dont understand how can i do the “if” of the x and y parameters. When the target is in the center I want to activate the laser and when its not centered so the laser should be closed.

So how can i do
If (the balloon is centered)
Active laser

Obviously i shoul be with x, y. But how🙂

If you print the values of panOffset and tiltOffset, they should be small when the balloon is centered.

Hope this helps.


for now, i need to turn on the led on the arduino (built in) but just whene the baloon is centered. but the led is on when the baloon is everywhere in the field view


// begin license header


// This file is part of Pixy CMUcam5 or "Pixy" for short


// All Pixy source code is provided under the terms of the

// GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).

// Those wishing to use Pixy source code, software and/or

// technologies under different licensing terms should contact us at

// [email protected] Such licensing terms are available for

// all portions of the Pixy codebase presented here.


// end license header


#include <Pixy2.h>

#include <PIDLoop.h>

Pixy2 pixy;

PIDLoop panLoop(400, 0, 400, true);

PIDLoop tiltLoop(500, 0, 500, true);

void setup()






  // We need to initialize the pixy object


  // Use color connected components program for the pan tilt to track



void loop()


  static int i = 0;

  int j;

  char buf[64];

  int32_t panOffset, tiltOffset;


  // get active blocks from Pixy



  if (pixy.ccc.numBlocks)




    if (i%60==0)



    // calculate pan and tilt "errors" with respect to first object (blocks[0]),

    // which is the biggest object (they are sorted by size).  

    panOffset = (int32_t)pixy.frameWidth/2 - (int32_t)pixy.ccc.blocks[0].m_x;

    tiltOffset = (int32_t)pixy.ccc.blocks[0].m_y - (int32_t)pixy.frameHeight/2;  


    // update loops




    // set pan and tilt servos  

    pixy.setServos(panLoop.m_command, tiltLoop.m_command);


#if 0 // for debugging

    sprintf(buf, "%ld %ld %ld %ld", rotateLoop.m_command, translateLoop.m_command, left, right);





  else // no object detected, go into reset state




    pixy.setServos(panLoop.m_command, tiltLoop.m_command);


  if((panOffset > 0) && (panOffset < 5) && (tiltOffset < 5 ) && (tiltOffset > 0))


    digitalWrite(LED_BUILTIN, HIGH);




    digitalWrite(LED_BUILTIN, LOW);




??? Any answers?

It seems to me that you need to understand what’s happening before writing some code.

I would recommend printing out the values for panOffset and tiltOffset and answer the question: what are the values of these variables when the balloon is “centered” to your liking? Do these values consistently correspond wth the ballon being centered?


I saw that when it centered, its 1-5