Object Tracking Device Using Color Recognition

Hello everyone! For this project, we are going to show you how to create an Object Tracking device using VIA Pixetto, two servomotors, and an Arduino board. The device will move itself around to follow the tracked and detected object.

Alright, let’s get started!

This guide will involve the following steps:

Step 1: Assembling all components for the Object Tracking Device
Step 2: Connecting the two servomotors to an Arduino board
Step 3: Connecting the VIA Pixetto to an Arduino board
Step 4: Configuring the VIA Pixetto
Step 5: Programming the VIA Pixetto with Blocks
Step 6: Uploading the code to the Arduino board on the Object Tracking
device and saving the project

Step 1

You need to put together all the components to build the Object Tracking device. These include the VIA Pixetto, camera bracket, two servomotors, servomotor lower and upper brackets, and two screws.

First, you need to install the first servomotor with the servomotor arm into the servomotor lower bracket (base).

Secondly, install the servomotor upper bracket on the top of the first servomotor (via servomotor arm), then secure it with the screw.

Third, install the second servomotor into the servomotor upper bracket.

Fourth, install the VIA Pixetto onto the camera bracket then install the camera bracket onto the first servomotor, and secure it with the screw.

Lastly, attach the expansion board to the Arduino board via the pin header connectors.

Step 2

You need to connect the two servomotors to an Arduino board.

First, connect the first servomotor (installed in the lower bracket) that will be responsible for x-axis (horizontal) movements on the Grove connector on the expansion board labeled D7.

Then, connect the second servomotor (installed in the upper bracket) that will be responsible for y-axis (vertical) movements on the Grove connector labeled D8.

Step 3

You need to connect the VIA Pixetto to an Arduino board via an expansion board as shown in the diagram.

First, connect the Grove cable to the VIA Pixetto vision sensor. Then, attach the other end of the Grove cable to the Grove connector labeled ‘UART’ on the expansion board, which is in turn attached to the Arduino board.

Object Tracking Device

Step 4

You need to configure the VIA Pixetto.

First, connect the VIA Pixetto to your PC using a Micro USB 2.0 cable.

When the green, blue, and red LEDs are lit, this means the VIA Pixetto is successfully connected.

Object Tracking Device

Now, configure the VIA Pixetto to recognize the color red.

Open the VIA Pixetto Utility. In the ‘Configuration’ panel on the right, set the ‘Function’ option to ‘Color Detection’. Check off the colors you would like your VIA Pixetto to recognize and follow.

Once you have finished configuring the VIA Pixetto vision sensor, test the vision sensor to make sure it detects your desired color.

In this case, choose the color red, so VIA Pixetto will recognize and label red colored objects.

Step 5

You need to program the VIA Pixetto with Blocks.

First, open the VIA Pixetto Junior application. You are going to program blocks so that the Object Tracking device will follow the object.

A. First Layer of Blocks

Select the first layer from the Control category, which is the ‘setup’ block.

Add the ‘initialize vision sensor RX# () TX# ()’ block from the Vision Sensor category.

Object Tracking Device

Place it within the setup block. This block will relate information to the VIA Pixetto vision sensor.

Select pin ‘0’ and ‘1’. This makes sure there is a connection between the Arduino board and VIA Pixetto vision sensor. RX means Receive, and TX means Transmit. We recommend using pins ‘0’ and ‘1,’ because it is more reliable.

Next, go to the Variable category and select the ‘declare i as integer value’ block.

You need to create four integers to control the movement of the servomotors when it detects the object and recognizes the color of the object. The ‘x’ integer is used to determine the center and position of the object on the horizontal axis while the ‘y’ integer is used to determine the center and position of the object on the vertical axis.

The ‘dx’ integer is used to determine the left and right location of the object when it is in the horizontal axis position, while the ‘dy’ is used to determine the up and down location of the object when it is in the vertical axis position.

To create these integers for the Object Tracking Device, click on the ‘i’ dropdown menu arrow, and then click on the ‘make a variable’ to create the ‘x’ integer.

Object Tracking Device

Enter ‘x’ and click on the ‘OK’ button.

● x integer

You have successfully created the ‘x’ integer.

Next, follow the same steps to create the ‘y’, ‘dx’, and ‘dy’ integers.

● y integer

Object Tracking Device

● dx integer

● dy integer

You have now created the necessary integers.

Then arrange the integer blocks in sequence as shown in the picture below.

Object Tracking Device

Next, go to the Output Unit category. Set the blocks for the first servomotor to always start on a 90⁰ angle as its default position by selecting the ‘rotate the servo-motor on pin () an angle (0~180⁰) of ()’ block.

Set the pin to pin 7, because the first servomotor assigned for horizontal movements is connected to the Grove connector (labeled D7) and set it at a 90⁰ angle.

Object Tracking Device

Then insert it under the ‘declare dy as integer value’ block.

Lastly, set the second servomotor assigned for vertical movements to always start on a 90⁰ angle as its default position as well by selecting another ‘rotate the servo-motor on pin () an angle (0~180⁰) of ()’ block.

Set it to pin 8, because the second servomotor is connected to the Grove connector (labeled D8) and set it at a 90⁰ angle.

Then insert it under the ‘rotate the servo-motor on pin 7 an angle of 90⁰’ block.

Object Tracking Device

This section makes sure there is communication to both servomotors via the Arduino board.

B. Second layer of Blocks

Now, create the blocks governing both servomotors’ movements to follow the object.

From the Control category, select the ‘if () then’ block to create an event.

Next, go back to the Vision Sensor category and select the ‘object is detected’ block.

Object Tracking Device

Insert the ‘object is detected’ block inside the ‘if () then’ block. This makes the VIA Pixetto report that is detecting an object via a command from the Arduino board. If this condition is true, the block inside will activate the event.

Next, create the blocks controlling the movements of the servomotors. You want to make sure that when the VIA Pixetto detects an object, the Object Tracking device will move vertically and horizontally (up, down and sideways).

Create an ‘x’ and ‘dx’ integer block section and a ‘y’ and ‘dy’ section. The ‘x’ section will make the VIA Pixetto detect the center of the object when it is on the horizontal area. The ‘y’ section will make the VIA Pixetto detect the center of the object when it is on the vertical area.

x and dx variables

First, select the ‘set i to ()’ block and click on the ‘i’ icon. From the dropdown menu, select ‘x’. (This variable x is also the one that you created earlier on the blocks).

Object Tracking Device

Next, select two math blocks from the Math category then put them together.

Then, insert them into the ‘set x to ()’ block.

Within those two combined math blocks, insert an ‘object position x’ block and ‘object width’ block from the Vision Sensor category.

Set the first equation to ‘+’ and second equation to ‘÷’, and then set the value at ‘2’ to track the center of the object. Then, insert these combined blocks within the ‘if () then’ block.

Now, create the ‘dx’ integer. Go back to the Variable category then select again the ‘set i to ()’ block and click on the ‘i’ icon from the dropdown menu and change it to ‘dx’.

Color Recognition

Next, select a block from the Math category and insert it into the ‘set dx to ()’ block.

Then, from the Variable category, select the ‘i’ block. Click on the ‘i’ icon from the dropdown menu, and then change it to the variable ‘x’.

Next, insert the variable ‘x’ block to the math block then set the equation to ‘-’ and value to ‘50’.

This makes sure the detected object is within a position on the horizontal axis. So the VIA Pixetto can recognize and track the object detected. Make sure to insert this block underneath the ‘set x to object position x + object width ÷ 2’ block.

y and dy variables

First, select the ‘set i to ()’ block and click on the ‘i’ icon. From the dropdown menu, select ‘y’. (This variable y is also the one that you created earlier on the blocks).

Object Tracking Device Using Color Recognition

Next, select two math blocks from the Math category then put them together.

Then, insert them into the ‘set y to ()’ block.

Within those two combined math blocks, insert an ‘object position y’ block and ‘object height’ block from the Vision Sensor category.

Object Tracking Device Using Color Recognition

Set the first equation to ‘+’ and second equation to ‘÷’, and then set the value at ‘2’ to track the center of the object. Then, insert these combined blocks within the ‘if () then’ block.

Now, create the ‘dy’ integer. Go back to the Variable category then select again the ‘set i to ()’ block and click on the ‘i’ icon from the dropdown menu and change it to ‘dy’.

Next, select a block from the Math category and insert it into the ‘set dy to ()’ block.

Then, from the Variable category, select the ‘i’ block. Click on the ‘i’ icon from the dropdown menu, and then change it to the variable ‘y’.

Object Tracking Device Using Color Recognition

Next, insert the variable ‘y’ block to the math block, then set the equation to ‘-’ and value to ‘50’.

This makes sure the detected object is within a position on the vertical axis. So the VIA Pixetto can recognize and track the object detected. Make sure to insert this block underneath the ‘set y to object position y + object height ÷ 2’ block.

● dx section

Select the ‘if () then’ block for the movement event and insert it underneath the ‘set dy to y – 50’ block.

Object Tracking Device Using Color Recognition

Next, from the Logic category select the ‘logic’ block. Insert an ‘i’ block from the Variable category, then open its scrolling menu and select the ‘dx’ integer. Set the ‘logic’ block equation to a ‘<’ sign.

Object Tracking Device Using Color Recognition

Then, select a block from the Math category. Within that block insert ‘object width’ block from the Vision Sensor category. Set the ‘÷’ sign, and enter ‘-2’.

Insert the math block inside the logic block.

Object Tracking Device Using Color Recognition

Then, insert the logic block within the second ‘if () then’ block.

Then, from the Output Unit category, select the ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ block, set it to pin 7, and insert a math block from the Math category inside of it and set the equation to ‘+’ sign.

Again, from the Output Unit category, select the ‘the angle of the servo motor connected on the pin ()’ block, set it to pin 7, and insert it inside the math block in the ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ block.

Object Tracking Device Using Color Recognition

Get the ‘map value () from ~ to ~’ block from the Math category, insert ‘i’ block from the Variable category, and open its scrolling menu and select ‘dx’.

Next, set the value as follow: ‘from 0 ~ -50 to 1 ~ 4’ to make sure there is smooth movement of the servomotor’s angle. Then, insert this block into the math block.

Object Tracking Device

Make sure to insert this in the ‘if () then’ block.

Object Tracking Device

For the next section, click on the gear icon on the ‘if () then’ block to open its scrolling menu, then drag the ‘else if’ block underneath the ‘if’ block for the next movement event.

Next, from the Logic category again, select the ‘logic’ block. Insert an ‘i’ block from the Variable category then open its scrolling menu and select ‘dx’ integer. Set the ‘logic’ block equation to a ‘>’ sign.

Object Tracking Device

Then, select a block from the Math category. Within that block insert ‘object width’ block from the Vision Sensor category. Set the ‘÷’ sign, and enter ‘2’.

Insert the math block inside the logic block.

Object Tracking Device

Next, insert the logic block into the ‘if () then else if then’ block.

Note: The ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ block that you just constructed together. You will use the same instructions here, but just change the equations and values in the ‘map value dx from ~ to ~’ block. Please keep the same pin numbers.

The easiest way to do this is to duplicate these blocks as shown below. By right clicking on the previous block and selecting duplicate.

Object Tracking Device

A new block will be displayed. Make these two changes, first change the equation of the math block (in front of the word map value) to a ‘-‘ sign; then, in the ‘map value dx from ~ to ~’ block change ‘-50’ to ‘50’.

When you are done, make sure to insert this at the bottom of the previous block.

Object Tracking Device

● dy section

Select another ‘if () then’ block from the Control category for the movement event and insert it underneath the second ‘if () then’ block of dx section.

Next, from the Logic category, select the ‘logic’ block. Insert an ‘i’ block from the Variable category, then open its scrolling menu and select the ‘dy’ integer. Set the ‘logic’ block equation to a ‘<’ sign.

Object Tracking Device

Then, select a block from the Math category. Within that block insert the ‘object height’ block from the Vision Sensor category. Set the ‘÷’ sign, and enter ‘-2’.

Object Tracking Device

Insert the math block inside the logic block.

Then, insert the logic block within the third ‘if () then’ block.

Then, from the Output Unit category, select the ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ block, set it to pin 8, and insert a math block from the Math category inside of it and set the equation to ‘-’ sign.

Object Tracking Device

Again, from the Output Unit category, select the ‘the angle of the servo motor connected on the pin ()’ block, set it to pin 8, and insert it inside the math block in the ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ block.

Get the ‘map value () from ~ to ~’ block from the Math category, insert the ‘i’ block from the Variable category, and open its scrolling menu and select ‘dy’.

Object Tracking Device

Next, set the value as follow: ‘from 0 ~ -50 to 1 ~ 4’ to make sure there is smooth movement of the servomotor’s angle. Then insert this block into the math block.

Object Tracking Device

Make sure to insert this in the third ‘if () then’ block.

For the next section, click on the gear icon on the ‘if () then’ block to open its scrolling menu, then drag the ‘else if’ block underneath the ‘if’ block for the next movement event.

Object Tracking Device

Next, from the Logic category again, select the ‘logic’ block. Insert an ‘i’ block from the Variable category, then open its scrolling menu and select the ‘dy’ integer. Set the ‘logic’ block equation to a ‘>’ sign.

Then, select a block from the Math category. Within that block insert the ‘object height’ block from the Vision Sensor category. Set the ‘÷’ sign, and enter ‘2’.

Object Tracking Device

Insert the math block inside the logic block.

Next, insert the logic block into the ‘if () then else if then’ block.

Note: The ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ block that you just constructed together. You will use the same instructions here but just change the equations and values in the ‘map value dy from ~ to ~’ block. Please keep the same pin numbers.

The easiest way to do this is to duplicate these blocks as shown below. By right clicking on the previous block and selecting duplicate.

A new block will be displayed. Make these two changes, first change the equation of the math block (in front of the word map value) to a ‘+‘ sign. Then in the ‘map a value dy from ~ to ~’ block change ‘-50’ to ‘50’.

Object Tracking Device

When you are done, insert this at the bottom of the previous block.

The last layer of blocks is the ‘delay () seconds’ block from the Control category. Set it to 10 milliseconds. This creates a time for the servomotors to response to a movement.

Insert the delay block at the end of the combination blocks. You have now finished all the blocks for the Object Tracking Device.

Object Tracking Device

Step 6

After completing and reviewing the blocks, upload the code to the Arduino board on the Object Tracking device and save the project.

A. How to upload the code

First, connect the Arduino board to the PC using the Micro USB 2.0 cable. Next, click the ‘No Hardware’ dropdown arrow on the top left corner of the VIA Pixetto Junior application, and then select the ‘Arduino UNO’ to start establishing the connection.

Object Tracking Device

Click on the ‘Not Connected’ dropdown arrow. A pop-up screen will appear. Click the ‘Connect’ button to establish the connection.

You can use the ‘Refresh’ button if the USB connection is not found on the PC. The COM port is set to COM9 (Note: The COM port number may vary depending on your PC’s configuration).

Once connected, click on the ‘Go to Editor’ button to go back to the VIA Pixetto Junior application.

Click the ‘Upload’ button on the right side of the VIA Pixetto Junior application to upload the code to the Arduino board.

Object Tracking Device

Wait until the ‘Upload Success’ message appears before unplugging the cable.

B. How to save your project

Click on the icon next to the ‘Project’ to open its scrolling menu and select the ‘Save Project As’.

Object Tracking Device

When you click on the ‘Save Project As’ a window pop-up will appear on the screen and you can name the project ‘Object tracking device’. Feel free to choose whatever name you like.

Create or find a suitable folder to keep all your projects.

Object Tracking Device

Now that you have connected the VIA Pixetto vision sensor, attached the servomotors, and uploaded the code to the Arduino board, it is time to do some test with your Object Tracking device.

Have fun with this project and do not forget to share your own blocks creations with us on Instagram, Facebook, and Twitter at #VIAPixetto!

Share this blog post!

Share on linkedin
Share on twitter
Share on facebook

Leave a Reply