Control Two Servo-motors

How to Control Two Servo-Motors using Shape Recognition

Hello everyone! For this project, we will show you how to control two servo-motors guided by shape recognition using the VIA Pixetto vision sensor, two servo-motors, expansion board, and an Arduino board.

Alright, let’s get started!

The guide will involve four simple steps

Step 1: Configuring the VIA Pixetto

Step 2: Assembling all components to control two servo-motors

Step 3: Programming the VIA Pixetto with Blocks

Step 4: Uploading the code to the Arduino board to control two servo-motors and saving the project

Step 1

You need to configure the VIA Pixetto to recognize different shapes to control two servo-motors.

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.

control two servo-motors

Open the VIA Pixetto Utility and configure the VIA Pixetto to recognize triangle, rectangle, and pentagon shapes.

Once you have finished configuring the VIA Pixetto, test it to make sure it can recognize the shapes.

Step 2

Put together all the components to assemble the two servo-motors. These include the VIA Pixetto, Grove cable, two servo-motors, expansion board, and an Arduino board.

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

control two servo-motors

Then, attach the two servo-motors to the Grove connectors labeled D2and D6 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.

control two servo-motors

Next, connect the Grove cable to the VIA Pixetto.

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

control two servo-motors

Step 3

You 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 shapes.

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.

control two servo-motors

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

control two servo-motors

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.

control two servo-motors

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 2 and 6 respectively, because the first servo-motor is connected to the Grove connector D2, and the second servo-motor is connected to the Grove connector D6. Then set both to a 180⁰ angle as its default position.

control two servo-motors

B. Second Layer of Blocks

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. 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.

Next, program blocks for the servor-motors’ behaviour. You want to make sure that when the VIA Pixetto detects a shape, the servor-motors will turn to a set degree.

To make this true, 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.

Servo-motor 1 rotation

Create the blocks for the rotation of the servo-motor 1.

Select a ‘logic’ block from the Logic category. Then, select the ‘object type’ block and ‘round’ block from the Vision Sensor category. Then, change the ‘round’ block to ‘triangle’.

control two servo-motors

Insert an ‘object type’ block and ‘triangle’ block into the ‘logic’ block. Then, set the ‘logic’ block to ‘=’ sign.

control two servo-motors

Put the ‘logic’ block inside the second ‘if () then’ block. This makes the VIA Pixetto report that it is detecting a shape that is a triangle. If this condition is true, the blocks inside will activate the event which is rotating the first servo-motor.

control two servo-motors

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 ‘2’, and then setthe angle to 5⁰. This makes the first servo-motor turn.

Then, select a ‘delay ()’ block from the Control category, and set it to 1 second.

Next, insert them into the second ‘if () then’ block as shown in the diagram below.

control two servo-motors

Next, duplicate the ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ block and the ‘delay ()’ block that you have created earlier.

This time, set the angle of the ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ block to 180⁰. This makes the first servo-motor turn a 180⁰. Keep the ‘delay ()’ block to 1 second..

Then, insert the two blocks into the second ‘if () then’ block underneath the ‘delay ()’ block.

control two servo-motors

You have successfully created the blocks for your first servo-motor.

Servo-motor 2 rotation

Create the blocks for the rotation of the second servor-motor.

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 for the next event which is rotating the second servo-motor.

control two servo-motors

Next, select a ‘logic’ block from the Logic category. Then, select the ‘object type’ block and the ‘round’ block from the Vision Sensor category. Then, change the ‘round’ block to ‘rectangle’.

Insert an ‘object type’ block and ‘rectangle’ block into the ‘logic’ block. Then, set the ‘logic’ block to ‘=’ sign.

control two servo-motors

Put the ‘logic’ block inside the ‘if () then else if then’ block. This makes the VIA Pixetto report that it is detecting a shape that is a rectangle. If this condition is true, the blocks inside will activate the event which is rotating 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 ‘6’, and then set the angle to 5⁰. This makes the second servo-motor turn.

Then, select a ‘delay ()’ block from the Control category and set it to 1second.

control two servo-motors

Put the them inside the second ‘if () then’ block as show in the diagram below.

control two servo-motors

Next, duplicate the ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ blocks and the ‘delay ()’ block that you have created earlier.

This time, set the angle of the ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ block to 180⁰. This makes the second servo-motor turn a 180⁰. Then, keep the ‘delay ()’ block to 1 second.

Insert the two blocks into the second ‘if () then’ block underneath the ‘delay ()’ block.

control two servo-motors

You have successfully created the blocks for the second servo-motor.

Two servo-motor rotation

Create the blocks for the rotation of the two servo-motors.

Click again the gear icon on the second ‘if () then’ block to open its scrolling menu, then drag the ‘else if’ block underneath the ‘else if’ block for the next event which is rotating the two servo-motors.

control two servo-motors

Next, select a ‘logic’ block from the Logic category. Then, select the ‘object type’ block and the ‘round’ block from the Vision Sensor category. Then, change the ‘round’ block to ‘pentagon’.

Insert an ‘object type’ block and ‘pentagon’ block into the ‘logic’ block.Then, set the ‘logic’ block to ‘=’ sign.

Put the ‘logic’ block inside the ‘if () then else if then’ block. This makes the VIA Pixetto report that it is detecting a shape that is a pentagon. If this condition is true, the blocks inside will activate the event which is the rotation of the two servo-motors.

Next, select two ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ blocks from the Output Unit category and the ‘delay ()’ block from the Control category.

Set the pins to 2 & 6, and then set the angle to 5⁰. This makes the two servo-motors turn. Then, set the ‘delay ()’ block to 1 second.

Then, insert them into the second ‘if () then else if then’ block.

Next, duplicate the two ‘rotate the servo-motor on the pin () an angle (0~180⁰) of ()’ and the ‘delay ()’ blocks that you have created earlier. Change the angles from 5⁰ to 180⁰, and keep the delay to 1 second.

Then, insert them into the ‘if () then else if then’ block underneath the ‘delay ()’ block as shown in the diagram below.

Next, go to the Control category and select the ‘repeat while’ block.Then, go to the Vision Sensor category to select the ‘object is detected’ block and insert it into the ‘repeat while’ block section.

Lastly, put the ‘repeat while’ block underneath the second ‘if then ()’ block as shown in the diagram below.

You have successfully created the blocks for the rotation to control two servo-motors.

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

Step 4

Upload the code to the Arduino board to control two servo-motors 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 ofthe 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 ‘Control two servo-motors’. 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 Basic Shape Controlled sevor-motors.

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

Share this blog post!

Share on linkedin
Share on twitter
Share on facebook

Leave a Reply