PROJECT Nº 3: Control the servomotor

Learn how to control the position of a servomotor using the Arduino library.

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE:  20 min.

MATERIALS:

  • 1 Servomotor
  • 1 Build&Code UNO board
  • 1 Sensor shield
  • 1 USB – USB type B cable
  • 1 Computer

What is a servomotor?

A servo motor is a DC motor that instead of turning around continuously, it is designed to turn around an specific angle indicated by a control signal, and it will remain on that position.

Arm Robot-parts-servomotor

This signal is given by the PWM digital pins. To control the servomotor, pulses are sent every 20 ms, to 50Hz. The width of the pulse will determine the rotation angle, known as PWM. This width can vary, but it normally goes between 0.5 and 2.5 ms. This gives the servomotor an operation range, so that it can be moved from 0º (pulse with a 0.5 ms width) and a maximum, that is usually 180º (pulse with a 2.5 ms width).

activity-3-arm-robot

To program and control small servo motors, like the ones included in the Arm Robot, you will use a PWM digital pin of the Build&Code UNO, where the width of the pulse will determine the angle position.

A servo motor has 3 pins: 1 red (5V), 1 brown or black (Ground), and 1 yellow or white (Control).

CONNECTIONS:

  1. The brown cable corresponds to the servomotor’s GND. Connect it to the G pin located in port 6 of the Build&Code UNO board.
  2. The red cable corresponds to the 5V. Connect it to the V located in port 6 of the Build&Code UNO board.
  3. The orange cable corresponds to the signal. Connect it to the S located in port 6 of the Build&Code UNO board.

PROGRAMMING CODE

You can do this project using the Arduino program or a visual programming software by blocks compatible. Below you will find the necessary code.

Arduino Code

Before starting to assemble the Arm Robot, follow these steps:

  1. Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
  2. Open the Arduino program and copy the following program in it:
    #include <Servo.h>
    
    Servo motor1; // DECLARE SERVO 1
    int degree1 = 0; // SERVO 1 DEGREES
    
    void setup() { 
    motor1.attach (9); // PWM DIGITAL PIN 9 WHERE SERVO 1 IS CONNECTED
    }
    
    void loop() {
    degree1 = 0;
    motor1.write (degree1); // SEND DEGREES TO SERVO 1
    delay(1000);  // WAIT 1 SECOND ON THIS POSITION
    
    degree1 = 60;
    motor1.write (degree1); 
    delay(1000);
    
    degree1 = 120;
    motor1.write (degree1);
    delay(1000);
    
    degree1 = 180;
    motor1.write (degree1); 
    delay(1000);
    
    degree1 = 120;
    motor1.write (degree1); 
    delay(1000);
    
    degree1 = 60;
    motor1.write (degree1); 
    delay(1000);
    }
    

Code for the visual programming software by blocks compatible

  1. Download and install the program.
    1.1 Open the software.
    1.2 Configure the program to save code into the Build&Code UNO board. You will find the instructions in the Arm Robot First Steps guide.
  2. Open the program and copy the following code. Use the following image as a guide:

RESULT OF THE EXERCISE

You have learned how to use the Servo.h library in the Arduino programming software. Once the library has been installed, you can program and control the servomotor in Arduino and in the visual programming software by blocks compatible.

1 0

0