Learn how to control the position of a servomotor using the Arduino library.
DIFFICULTY LEVEL: Beginner.
DURATION OF THE EXERCISE: 20 min.
- 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.
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).
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).
- 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.
- The red cable corresponds to the 5V. Connect it to the V located in port 6 of the Build&Code UNO board.
- The orange cable corresponds to the signal. Connect it to the S located in port 6 of the Build&Code UNO board.
You can do this project using the Arduino program or a visual programming software by blocks compatible. Below you will find the necessary code.
Before starting to assemble the Arm Robot, follow these steps:
- Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
- Open the Arduino program and copy the following program in it:
Code for the visual programming software by blocks compatible
- 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.
- 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.