color sorter device

Color Detection Application: Color Sorter Device with Scratch

Hello everyone! For this project, we will show you how to use a color sorter device guided by color recognition using the VIA Pixetto vision sensor, two servo-motors, Arduino board, and an expansion board. The device sorts the color objects (bricks) according to their color. Alright, let’s get started!

The guide will involve four simple steps:

  • Step 1: Configuring the VIA Pixetto
  • Step 2: Assembling all components
  • Step 3: Programming the VIA Pixetto with Blocks
  • Step 4: Uploading the code to the Arduino board and saving the project

Step 1

You will need to configure the VIA Pixetto to recognize four different colors.
First, connect the VIA Pixetto to your PC using a Micro USB 2.0 cable. When the green, blue, and red LEDs are lit, it means the VIA Pixetto is successfully connected.

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

Configuring the VIA Pixetto

Once you have finished configuring the VIA Pixetto, test it to make sure it can detect red, green, blue, and yellow objects.

Step 2

Put together all the components to create the Color Sorter Device chassis. These include the VIA Pixetto, two servo-motors (with servo-arms), expansion board, and an Arduino board.

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

Assembling all components

Next, connect the Grove cable to the VIA Pixetto.

Assembling all components

Then, install the two servo-motors, and the VIA Pixetto on the pre-build device chassis.

Assembling all components

Then, attach the two servo-motors to the Grove connectors labeled D7 and D8 respectively on the expansion board, which in turn is attached to the Arduino board. Make sure you have attached the servo-motor arm for each servo-motor.

Then, attach the VIA Pixetto to the Grove connector labeled ‘UART’ on the expansion board.

Assembling all components

Step 3

You will need to program the VIA Pixetto with Blocks so that the two servo-motors will activate and rotate their servo-motor arms to the set degree when detecting the different colors.

First, open the VIA Pixetto Junior application.

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. 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 Output Unit category. Set the blocks for the two servo-motors by selecting two ‘rotate the servo-motor on pin () an angle (0~180⁰) of ()’ blocks.

Set the pins to pin 7 and 8 respectively, because the first servo-motor is connected to the Grove connector D7, and the second servo-motor is connected to the Grove connector D8. Set the block for the first servo-motor to a 56⁰ angle, and set the block for the second servo-motor to a 180⁰ angle as their default position.

The first servo-motor is the pick servo-motor which will pick up the bricks. The second servo-motor is the drop servo-motor which will drop the bricks to their specific container according to the color.

B. Second layer of Blocks

From the Control category, select the ‘delay () seconds’ block from the Control category. Set it to 1 second. This creates a time for the first servo-motor to respond to a movement.
Next, select the ‘count with i from () to () by () then’ block. Set the angles to 180⁰, 56⁰ and 1⁰. Then put the block underneath the ‘delay 1 second’ block. This makes the first servo-motor to pick-up the brick from the loader, and then move the brick to display to the VIA Pixetto for color recognition.

Go to the Output Unit category, select again ‘rotate the servo-motor on pin () an angle (0~180⁰) of ()’ block. Set the pin to pin 7. Then, select the ‘i’ block from the Variable category and insert it into the ‘rotate the servo-motor on pin () an angle (0~180⁰) of ()’ block.

Insert the ‘rotate the servo-motor on pin () an angle (0~180⁰) of ()’ block into the ‘count with i from () to () by () then’ block.

Next, select a ‘delay () seconds’ block from the Control category Set it to 2 milliseconds. Then put it underneath the ‘‘rotate the servo-motor on pin () an angle (0~180⁰) of ()’ block. This makes the first servo-motor move from pick-up position (180⁰) to a display position (56⁰).

C. Third layer of Blocks

From the Control category, select the ‘delay () seconds’ block from the Control category. Set it to 1 second. This creates a time for the first servo-motor to respond to a movement.

Next, select the ‘count with i from () to () by () then’ block. Set the angles to 56⁰, 7⁰ and 1⁰. Then put it underneath the ‘delay 1 second’ block. This makes the first servo-motor moves from displaying position (56⁰) to releasing position (7⁰).

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. Insert the ‘object is detected’ block inside the ‘if () then’ block.

Programming the VIA Pixetto with Blocks

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.

Then, insert the ‘if () then’ block into the ‘count with i from () to () by () then’ block.

Next, program the blocks to control the second servo-motor. You want to make sure that when the VIA Pixetto detects an object, the second servo-motor will turn to a specific angle to drop the brick to its designated container.

To make sure this occurs, use another ‘if () then’ block from the Control category to create an event. Insert the second ‘if () then’ block into the first ‘if () then’ block.

Select a ‘logic’ block from the Logic category. Then, select the ‘object type’ block and ‘red’ block from the Vision Sensor category. Insert an ‘object type’ block and ‘red’ block into the ‘logic’ block. Then, set the ‘logic’ block to ‘=’ sign.

Programming the VIA Pixetto with Blocks

Put the ‘logic’ block inside the ‘if () then else if then’ block. This makes the VIA Pixetto report that it is detecting an object that is red. If this condition is true, the blocks inside will activate the event which is movement of the second servo-motor.

Next, select the ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ block from the Output Unit category. Set the pin to ‘8’, and then set the angle to 180⁰. Insert it into the second ‘if () then’ block as shown in the diagram below. This makes the second servo-motor turn to 180⁰ to drop the detected red object on its designated container.

Next, we will create blocks for the other events. Click on the gear icon on the second ‘if () then’ block to open its scrolling menu, then drag the ‘else if’ block underneath the ‘if’ block.

Repeat the process. Drag two more ‘else if’ blocks but this time put it underneath the ‘else if’ block as shown in the diagram below. These are for the next events which are the movements of the second servo-motors.

Select three ‘logic’ blocks from the Logic category. Then, select three ‘object type’ blocks and three ‘red’ blocks from the Vision Sensor category. Change the ‘red’ blocks to blue, yellow, and green. Insert each ‘object type’ block and the ‘blue’, ‘yellow’, and ‘green’ blocks into each ‘logic’ block. Then, set all the ‘logic’ blocks to ‘=’ sign.

Put the three ‘logic’ blocks inside the ‘if () then else if then’ block as shown in the diagram below. This makes the VIA Pixetto report that it is detecting objects that are blue, yellow, and green. If these conditions are true, the blocks inside will activate the event.

Next, select three ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ blocks from the Output Unit category. Set their pins to ‘8’, and then set each with angles 150⁰, 120⁰, and 90⁰. These are for the movements of the second servo-motors when it detects objects that are blue, yellow, and green.

Insert the three blocks into the second ‘if () then’ block as shown in the diagram below. The three ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ blocks with 150⁰, 120⁰, and 90⁰ are for color blue, yellow, and green objects respectively.

Go to the Output Unit category, select again ‘rotate the servo-motor on pin () an angle (0~180⁰) of ()’ block. Set the pin to pin 7. Then, select the ‘i’ block from the Variable category and insert it into the ‘rotate the servo-motor on pin () an angle (0~180⁰) of ()’ block. This makes the first servo-motor move from display position (56⁰) to a release position (7⁰).

Next, select a ‘delay () seconds’ block from the Control category. Set it to 5 milliseconds. Then put it underneath the ‘‘rotate the servo-motor on pin () an angle (0~180⁰) of ()’ block.

Next, insert them underneath the first ‘if () then’ block.

D. Fourth layer of Blocks

Next, to create the fourth layer of blocks, just make a duplicate copy of the second layer of blocks. Change only the degrees of the ‘count with i from () to () by () then’ block to 7⁰, 180⁰ and 1⁰. These blocks make the first servo-motor move from release position (7⁰) to a pick-up position (180⁰).

Then put the four layer of blocks right after the third layer of blocks.

You have successfully created the all the blocks.

Review the composed blocks. Double check the pins, degrees, and delay time values to make sure they are correct.

Step 4

Upload the code to the Arduino board and save the project.

A. How to upload the code

  1. Connect the Arduino board to your PC using the Micro USB 2.0 cable.
  2. 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.
  3. 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).
  4. Click on the ‘Go to Editor’ button to go back to the VIA Pixetto Junior application.
  5. Click the ‘Upload’ button on the right side of the VIA Pixetto Junior application to upload the code to the Arduino board. Wait until the ‘Upload Success’ message appears before unplugging the cable.

B. How to save your project

  1. Click on the icon next to the ‘Project’ to open its scrolling menu.
  2. Select the ‘Save Project As’.
    When you click on the ‘Save Project As’ a window pop-up will appear on the screen and you can name the project ‘Color Controlled LEDs’. Feel free to choose whatever name you like.
  3. Create or find a suitable folder to keep all your projects.

Now that you have connected the VIA Pixetto vision sensor, attached the two servo-motors, and uploaded the code to the Arduino board, it is time to do some test with your Color Sorter 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