Line Following Robot Using Pixy2

Good day everyone our professor gave us a task to use a pixy2 in making a line following robot so i bought all the materials that i need and assembled everything. Basically, the hardware part is done, the only problem is the code that i need in the project. I used the sample code of pixy2 for line following but after i upload the codes to the arduino mega, the robot is not working. I checked all the connections and pins of the motor driver but it is connected properly. Here is the sample code that i have used.

// 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 (
// Those wishing to use Pixy source code, software and/or
// technologies under different licensing terms should contact us at
// Such licensing terms are available for
// all portions of the Pixy codebase presented here.
// end license header

#include <Pixy2.h>
#include <PIDLoop.h>
#include <ZumoMotors.h>
#include <ZumoBuzzer.h>

// Zumo speeds, maximum allowed is 400
#define ZUMO_FAST        200
#define ZUMO_SLOW        150
#define X_CENTER         (pixy.frameWidth/2)

Pixy2 pixy;
ZumoMotors motors;
ZumoBuzzer buzzer;

PIDLoop headingLoop(5000, 0, 0, false);

void setup() 

  // Turn on both lamps, upper and lower for maximum exposure
  pixy.setLamp(1, 1);
  // change to the line_tracking program.  Note, changeProg can use partial strings, so for example,
  // you can change to the line_tracking program by calling changeProg("line") instead of the whole
  // string changeProg("line_tracking")

  // look straight and down
  pixy.setServos(500, 1000);

void loop()
  int8_t res;
  int32_t error; 
  int left, right;
  char buf[96];

  // Get latest data from Pixy, including main vector, new intersections and new barcodes.
  res = pixy.line.getMainFeatures();

  // If error or nothing detected, stop motors
  if (res<=0) 
    buzzer.playFrequency(500, 50, 15);
    Serial.print("stop ");

  // We found the vector...
  if (res&LINE_VECTOR)
    // Calculate heading error with respect to m_x1, which is the far-end of the vector,
    // the part of the vector we're heading toward.
    error = (int32_t)pixy.line.vectors->m_x1 - (int32_t)X_CENTER;


    // Perform PID calcs on heading error.

    // separate heading into left and right wheel velocities.
    left = headingLoop.m_command;
    right = -headingLoop.m_command;

    // If vector is heading away from us (arrow pointing up), things are normal.
    if (pixy.line.vectors->m_y0 > pixy.line.vectors->m_y1)
      // ... but slow down a little if intersection is present, so we don't miss it.
      if (pixy.line.vectors->m_flags&LINE_FLAG_INTERSECTION_PRESENT)
        left += ZUMO_SLOW;
        right += ZUMO_SLOW;
      else // otherwise, pedal to the metal!
        left += ZUMO_FAST;
        right += ZUMO_FAST;
    else  // If the vector is pointing down, or down-ish, we need to go backwards to follow.
      left -= ZUMO_SLOW;
      right -= ZUMO_SLOW;  

  // If intersection, do nothing (we've already set the turn), but acknowledge with a beep.
    buzzer.playFrequency(1000, 100, 15);

  // If barcode, acknowledge with beep, and set left or right turn accordingly. 
  // When calling setNextTurn(), Pixy will "execute" the turn upon the next intersection, 
  // making the left or right branch in the intersection the new main vector, depending on 
  // the angle passed to setNextTurn(). The robot will then follow the branch.  
  // If the turn is not set, Pixy will choose the straight(est) path by default, but 
  // the default turn can be changed too by calling setDefaultTurn(). The default turn
  // is normally 0 (straight).   
  if (res&LINE_BARCODE)
    buzzer.playFrequency(2000, 100, 15);
    // code==0 is our left-turn sign
    if (pixy.line.barcodes->m_code==0)
      pixy.line.setNextTurn(90); // 90 degrees is a left turn 
    // code==5 is our right-turn sign
    else if (pixy.line.barcodes->m_code==5)
      pixy.line.setNextTurn(-90); // -90 is a right turn 

I used a L298N motor driver btw. Thanks for helping me.

Please provide a detailed explanation of what you mean by "the robot is not working".

We are having a project about a Line Tracking Robot using Pixy 2 using its own logic provided by the codes. In which it can determine its own route, composed of intersections, towards the colored blocks to be transferred to its drop site.

We are using 2 dc motors connected to a L298N motor driver. Through our own research, we only see a sketch for Pixy 2 using ZUMO motors. We are having a hard time making a sketch that can make PIXY 2 decide its own track since we have limited knowledge in programming. We also used arduino mega.

May we know what can be your recommendations toward our project?

Thanks in advance with your reply.

Please edit your post to use code tags instead of quotes. See the "How to use this forum" post.

You still didn't answer my question.

Define "not working".

Why do you think the "ZumoMotors" library will work for your robot? I certainly would not expect it to work.

Make a small sketch that just exercises the motors. When that works perfectly, replace whatever you have in the original code with the working version. You may have to learn how the L298 motor driver works.

Hi all,
I am having a similar issue with the posted Arduino Sketch, but I am using the Original 32U4 Zumo robot and the Pixy2 camera.
All is working, Pixy2 is seeing the line, the Arduino sketch is calculation the error and giving the value to the left and right motor to move the Zumo in a way that the line is in the middle.
The issue is that the right motor is not able to move forward, only backwards. Same with left motor, it is always running forwards.

As a result the Zumo is turning to the right side until the line is out of sight and Zumo is stopping.

When the Pixy2 Init line is deactivated all is fine, but Pixy2 is not working, which is not the plan.

I guess I need to contact the Pixy Team, because it is too specific, but perhaps somebody has experiences same issue and found a fix. Or I have overlooked something.

Here the test Sketch:

#include <Zumo32U4.h>
#include <Pixy2.h>

Pixy2 pixy;
Zumo32U4Motors motors;

void setup() {

 pixy.init();  // deactivating this line will run the motors is correct directions

 motors.setSpeeds(0, 0);

 motors.setSpeeds(200, 200); // Left motor is running for forwards and right motor is running backwards, which is wrong
 motors.setSpeeds(0, 0);
 motors.setSpeeds(-200, -200); // Left motor is still running for forwards which is wrong and right motor is running backwards, which is wrong as well
 motors.setSpeeds(0, 0);

void loop() {


When the Pixy2 Init line is deactivated all is fine, but Pixy2 is not working, which is not the plan.

Could be a conflict with hardware timer usage.

You have the code for all the bits, so check which timers are used for the motors and the Pixy.

Post on the Pololu forum, too and update this thread if a solution is found.