Learn How to Program a Color Following Driverless Car

For this project, we are going to show you how to create a driverless car guided by color recognition using VIA Pixetto and an Arduino board. The car will steer itself to follow an object with a specific color.

We will skip a few preliminary steps, such as connecting your Pixetto to a PC. For a detailed guide to connecting your VIA Pixetto to a PC, please refer to our previous tutorial on www.pixetto.ai.

We will need three steps: configuring the VIA Pixetto, programming the VIA Pixetto with blocks, and uploading the code to the Arduino board on the driverless car.

Step 1

We are going to configure the vision sensor to recognize the colors yellow and red.

configuring the color for the driverless car

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

Once we have finished configuring the Pixetto vision sensor, we can test the vision sensor to make sure it detects our desired colors. Here, we chose the colors red and yellow, so VIA Pixetto will recognize and label red and yellow colored objects.

Your Pixetto is now configured to recognize red and yellow (Step 1).
We can start programming VIA Pixetto using blocks (Step 2).

Step 2

First, open the VIA Pixetto Junior application.

Next, we are going to program blocks so that the driverless car will follow the color yellow and stop at the color red.

A. First Layer of Blocks

First, select the first layer from the control section, which is the ‘setup’ block.

Add the ‘initialize vision sensor RX# () TX# ()’ block from the vision sensor section and place it within the setup block. This block will relate information to the vision sensor.

Select pin ‘0’ and ‘1’. This makes sure there is a connection between the Arduino board and 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 block category and select the ‘declare i as integer value’ block.

We need to create two integers to control the direction of the servo motor when it recognizes the object. The ‘x’ integer is used to determine the position of the object. The ‘dx’ is used to determine the position of the object from left to right.

To create the integers ‘x’ and ‘dx’, go to the variable category and select the ‘declare i as integer value’ block.

Click on the ‘i’ dropdown menu arrow, and then click on the ‘make a variable’ to create the ‘x’ integer.

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

We have created the ‘x’ integer.

Next, follow the same steps to create the ‘dx’ integer.

We have now created the integers.

Lastly, set the servo motor to always start on a 90 degree angle by selecting the ‘rotate the servo-motor on pin 0 an angle (0-180)’ block and inserting it under the ‘declare dx as integer value’ block.

Select pin 9, because our servo motor is connected to the Grove connector (D9) and we want to set it at a 90 degree angle. This section makes sure there is communication to the servo motor via the Arduino board.

B. Second Layer of Blocks

Now, we will program blocks so that the servo-motor’s left and right
movement follows the blue object.

Select the ‘If () then’ block to create an event. Next, insert the ‘object is detected’ block inside of the ‘If () then’ block. This makes the VIA Pixetto report that it is detecting an object via a command from the Arduino board. If this condition is true, the block inside will activate the event.

To make this true, use the ‘If () then’ block again to create an event. Insert the ‘If () then’ block inside of the ‘If () then’ block.

Select a logic block and insert an ‘object type’ block from the vision sensor category, set to equal sign, then add a ‘yellow’ block from the vision sensor category. This allows the VIA Pixetto to report that it is detecting an object that is yellow.

Next, we will create the blocks controlling the direction of the servo motor. We want to make sure when VIA Pixetto detects a yellow object, the driverless car has a limited range of movement while turning left or right.

We will also create an ‘x’ and ‘dx’ integers block. We create the ‘x’ integer blocks to make sure that the VIA Pixetto detects and recognizes the width and size of the object.

First, select the ‘set i to ()’ block and click on the ‘i’ icon. From the drop-down menu, select ‘x’. Next, select two blocks from the math category and insert them into the block.

Within those two math blocks insert an ‘object position x’ block and ‘object width’ block. Set the equation ‘plus’ and ‘divide’ and set the value at 2 to track the center of the object.

Second, select the ‘set i to ()’ block and click on the ‘i’ icon from the drop-down menu and select ‘dx’. Next, select a block from the math category and insert it into the ‘set dx to ()’ block. Then, add an ‘x’ block and set the equation to ‘minus’ and value to ‘50’.

This makes sure the object is within a specific range and size, so the VIA Pixetto can recognize and detect the object.

Select the ‘put build-in LED to logic state ()’ and select high to turn on the main motor. Next, select the ‘write PWM pin () value ()’ and then enter the pin number ‘5’ and value at ‘55.’

Select the ‘If () then’ block to create the movement event and insert it under the ‘put build-in LED to logic state ()’ block.

Next, select the ‘logic’ block and insert a ‘dx’ block inside. Set the equation to a ‘<’ sign. Then, select a block from the math category. Within that block insert ‘object width,’ set the ‘divide’ sign, and enter ‘-2.’

Next, select ‘rotate the servo-motor’ and set it to pin 9. Insert a ‘constrain’ block category and add a block from the math category.

Next, we insert the ‘angle of the servo motor connected on the pin ()’. We set it to pin 9, because this pin is where our servo motor is connected. Now, set the equation to a ‘minus’ sign.

Then, we insert the ‘map value’ block from the math category. In the first part, add a ‘dx’ block and then add ‘from 0~-50’ to ‘1~4’. Lastly, enter the constrain values low ‘45 degree’ and high ‘135 degree’ to limit the range of movement of the driverless car to the left.

For this section, click on the ‘If () then’ icon to open its scrolling menu, then slide the ‘else if’ block underneath the ‘If () then’ block to create the next movement event.

First, select the ‘logic’ block and insert a ‘dx’ block inside, set the equation to a ‘>’ sign. Then, select a block from the math category. Next, insert ‘object width’ block, set an equation sign ‘divide,’ and enter a value of ‘2.’

Next, select ‘rotate the servo-motor’ and set it to pin 9. Insert a ‘constrain’ block and add two blocks from the math category. Next, insert the ‘angle of the servo motor connected on the pin ().’ We set it to pin 9, because this pin is where the servo motor is connected. Set the equation to a ‘plus’ sign.

Then, we insert ‘map value’ block from the math category. In its first part, add a ‘dx’ block, then add ‘from 0~50 to 1~4.’ Lastly, enter the constrain values low ‘45 degree’ and high ‘135 degree’ to limit the range of movement of the car to the left.

Next, insert the blocks created in the ‘If () then’ section, which creates the movement of the driverless car to the left and right.

C. How To Create the Blocks To Make the Driverless Car Stop

First, click on the ‘If () then’ block icon on the second layer of blocks to open its scrolling menu.

Second, slide the ‘Else () If’ underneath the ‘If () then’ block.

 Driverless Car Stop

Next, select a logic block and insert an object type block inside of it. Set the equal ‘=’ sign and then insert a ‘red’ block from the vision sensor category. This makes the driverless car stop when it recognizes a red object.

Next, insert the ‘rotate the servo-motor’ block and set it to pin 9. Set the angle to 90 degrees, so the front wheels are at an angle of 90 degrees when the servo motor powers off.

Lastly, select the ‘put build-in LED to logic state ()’ and select low to turn off the main motor. Select the ‘write PWM pin () value (),’ then enter the pin number 5 and value at 0.

The last layer of block is the ‘delay () milliseconds’. This sets a timer on
the main motor.

 Driverless Car Stop

Insert the delay block at the end of the combination blocks. We have now finished all the blocks.

 Driverless Car Stop Blocks

Step 3

Now, let us review the blocks we have made. Once we are done, we can upload the code to the Arduino board.

A. How To Upload the Code

Connect the Ardunio board on the driverless car to the PC.

Next, click the ‘No Hardware’ dropdown arrow on the top left corner of the VIA Pixetto Junior application and then select Arduino UNO to start establishing the connection.

 Driverless Car

Click on the ‘Not Connected’ dropdown arrow. A pop-up screen will appear. Click the ‘Connect’ button to establish the connection. Click the refresh button if the USB connection isn’t found on the PC. The COM port is COM4. (The COM port may be different depending on the PC.)

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

Click the ‘Upload’ button on the left side of the VIA Pixetto Junior application to upload the code to the driverless car. Wait until the ‘Upload Success’ message appears before unplugging the cable.

B. How To Save Your Project

We can save our project after we have uploaded the code to the driverless car. First, click on the icon next to ‘Project’ to open its scrolling menu. Select ‘Save Project.’

Save the Driverless Car

When we click on ‘Save Project’ a desktop pop-up will appear on the screen and we can name the project ‘Follow the object.’ Feel free to chose whatever name you like.

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

Step 4

Let us do test runs with our driverless car and the two programmed color objects (yellow and red). Now, we are done!

Have fun and don’t forget to share your own blocks creations with #VIAPixetto on Instagram!

Share this blog post!

Share on linkedin
Share on twitter
Share on facebook

This Post Has 3 Comments

  1. Gabriel

    Great stuff! Thanks for sharing.

  2. Jason

    Cool! Just tried it on my device and it works!!

    1. Helena

      Great stuff, Jason! Thanks for your feedback.

Leave a Reply