Learn How to Program a Color Controlled Driverless Car

For this project, we will show you how to use VIA Pixetto and an Arduino board to control a driverless car using color recognition.

The guide will involve four steps: connecting VIA Pixetto to a PC, configuring VIA Pixetto and connecting it to an Arduino board using a Grove connector, programming VIA Pixetto with blocks, and finally, uploading the code to the Arduino board of the driverless car.

Step 1

You will need to connect your VIA Pixetto to your PC using a Micro USB 2.0 cable.

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

Don’t forget to remove the lens cover.

Step 2

Open the VIA Pixetto Utility and configure VIA Pixetto to recognize the colors green and red.

When you are finished configuring the VIA Pixetto, you can test it to make sure it can detect green and red.

Color Control Driverless Car

Connecting the VIA Pixetto vision sensor to the driverless car.

Firstly, connect the Grove cable to the VIA Pixetto vision sensor.

Attach the other end of the Grove cable to the expansion board on the driverless car.

In the example shown above, the VIA Pixetto is connected to an expansion board, which in turn is attached to the Arduino board.

Step 3

Open the VIA Pixetto Junior application and then build the blocks for the two colors that will allow you to control the driverless car.

The color green will tell the driverless car to move forward, and the color red will make it stop.

Color Control Driverless Car

A. First layer of blocks

Select the first layer from the control category, which is the ‘setup’ block, then add the ‘initialize vision sensor’ block from the vision sensor category and place it within the setup block.

This block will relate information to VIA Pixetto.

Lastly, select pins 0 and 1.

This makes sure there is a connection between the Arduino board and VIA Pixetto. ‘RX‘ means receive and ‘TX‘ means transmit.

Note: We recommend using pins 0 and 1 for optimal reliability.

B. Second layer of blocks

    i. How to create the blocks needed to make the driverless car move forward.

The first step is to select the ‘If () then’ block to create an event. This makes the VIA Pixetto report that it is detecting an object using a command from the Arduino board.

The block inside is ‘object is detected’. If this condition is true, the block inside will activate the event.

Color Control Driverless Car

To make this true, use the ‘If () then’ block again to create an event; this will allow the VIA Pixetto to detect any green objects.

Select a ‘logic’ block and insert an ‘object type’ block from the vision sensor category, set to equal sign, then add a ‘green’ block from the vision sensor category.

Next, from the ‘Inout’ category, select the ‘put built-in LED to logic state high’ block, so that the VIA Pixetto reports it has detected the green color and that the motor will be powered on.

Select the ‘write to PWM pin () value ()’ block and set to pin 5. This is the control pin of the motor (this may vary depending on the board being used).

Next, set the speed value of the motor at 65 (this can be adjusted according to the speed you want) and place it underneath the ‘put built-in logic state’ block.

The Arduino board will command the motor to turn on and move when it recognizes the color green.

Color Control Driverless Car

Review your Arduino board manual if you are not sure about these pins. If the pins are incorrectly selected within the different blocks, the motor may not start.

This is the layer of blocks needed for the green color command.

Color Control Driverless Car

    ii. How to create the blocks to make the driverless car stop.

Now we want to instruct the driverless car to stop when the VIA Pixetto recognizes a red object.

For this, click on the ‘if () then’ icon to open its scrolling menu and drag the ‘else if’ block under the ‘if‘ block.

First, selecting the ‘else if’ block will make sure the event is true.

Second, select the logic block and insert an ‘object type’ block from the vision sensor category.

Third, select the equal sign, and then add the ‘red’ block from the vision sensor category.

The VIA Pixetto vision sensor will report the event to the Arduino board, which will command the motor to stop when it sees a red color.

Color Control Driverless Car

From the ‘Inout’ category, select the ‘put built-in LED to logic state low’ block, so it reports that it has detected the red color and the motor will power off.

Select the ‘write to PWM pin () value to ()’ block and set to pin 5. Pin 5 is the control pin of the motor (again, this may differ based on the design of the board ).

Next, set the speed value of the motor to zero (this can be adjusted according to the speed you want).

The Arduino board will command the motor to turn off when VIA Pixetto recognizes a red color.

Color Control Driverless Car

Lastly, we will add a ‘delay 20-millisecond‘ block from the control category at the end to set up a timer for the motor.

Color Control Driverless Car

Review your Arduino board manual if you are not sure about these pins. If the pins are incorrectly selected within the different blocks, the motor may not start.

This is the layer of Blocks needed for the green and red color commands.

Color Control Driverless Car

Step 4

After completing the blocks, review them to make sure they are in order and that the pins are set correctly.

Then upload the code to the Arduino board.

    A. How to connect the Arduino board to PC

First, connect the Micro USB 2.0 cable to the PC using a USB 2.0/3.0 port. Then connect the other end of the cable to the Micro USB 2.0 port on the
Arduino board of the driverless car.

Color Control Driverless Car

    B. How to upload the codes

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

Color Control Driverless Car

When you click on the ‘Not Connected’ dropdown arrow a pop-up screen will be displayed.

Click the ‘Connect’ button to establish the connection. You can use the refresh button if the USB connection isn’t found on the PC.

The COM port should be set to COM4 (note: this may vary depending on your PC’s configuration).

Color Control Driverless Car

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

Color Control Driverless Car

Click on the ‘Upload’ button to upload the codes to the driverless car. Wait to see the ‘Upload completed’ message before unplugging the cable.

Now that we have attached the VIA Pixetto vision sensor and uploaded the codes to the Arduino board on the driverless car, it’s time to do some test runs.

Color Control Driverless Car

And you are done!

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

Share this blog post!

Share on linkedin
Share on twitter
Share on facebook

Leave a Reply