MeArm (II) – Testing the servos


This is the second post in this series to build and program a MeArm. After the unpacking and before I  start assembling the robotic arm, I’m going to test the servos to see if all of them move properly. Yes, I know that you may want to skip this step and start building the robot but I strongly don’t recommend it: once you begin with the structure of the MeArm, you will attach the servos to the base and the sides of the robot so, if something doesn’t work afterwards (and something won’t), you will have to disassemble all the structure of the robot and check all the steps again. However, if you take your time to check before carrying on the next step, you can be sure that everything is working fine so far, so any problem has to be on the last step you made. There are different ways to test the servos, for example, you can check how you can control the position of a servo motor with a potentiometer here, one option is to do the same for every servo and keep going. However, in this post I will first simplify the code and the circuit to add test all the servos at the same time.

– Arduino with an USB connection and the Arduino IDE installed.
– The 4 micro-servo motors that come with MeArmm: DoMan DM-S0090.
– Wires and a breadboard.

Circuit diagram:
There are two things to keep in mind when assembly this circuit: The fist one is to add a power supply for the servos because they will drain to much power from the Arduino Uno and you can damage the board, these servos need between 4.8 to 6V to work so a pack of 4 AA batteries should work (1.5V x 4 = 6V). The second thing to keep in mind is to connect the ground of the Arduino to the ground of the power supply or the servos will misbehave: to control the servos, the Arduino is sending small pulses of different widths through its pins to the servos, this is called pulse-width modulation. The reason you need to connect both grounds is because you require the Arduino and the micro servos to have the same ground level so, when the Arduino doesn’t send anything (low level signal) the micro servos also see a low level signal, so they don’t move. Then, when the Arduino sends a pulsed high level signal, because the ground level is the same for all the components, the micro servos understand that the pulsed signal they are receiving is high and then they move accordingly. You can see the circuit diagram on the picture below:

4 servo controlled by an Arduino
4 servo controlled by an Arduino

Arduino code:
We will use as a base the following code that I already used on a previous post where I used Python to control the servo. You only need the Arduino file which controls a single servo taking the angle from the serial port:

In line 1 we import the servo library for Arduino and in lines 3 to 5 we declare variables for the position of the servo, the pin where the servo is attached and a delay to let the servo settle down. In line 7 we crate the servo object called myServo and then we enter in the void setup: Line 10 starts the serial port and line 11 tells to what pin the servo is connected to. Inside the void loop, in line 15 we keep the Arduino waiting until there is information send through the serial port. When information is received through the serial port, in line 16 we read the serial port* and we store it in variable pos. After that, we send the position to the servo and we put a delay to let the servo reach the position.

*you can also use Serial.parseint() for this, the difference is that will store anything is sent though the serial port and Serial.parseInt() will store only the first long integer skipping all the characters that are not integers.

If you run this program and then open the serial monitor on the Arduino, you won’t see anything, but if you write a number between 0 and 180 and press enter, the servo will go to that position. The problem is that we don’t have one single servo, we have four, so we need to add three additional servos. For our first program, we are going to use the same angle for all the servos at the same time, so we just add more servos as in the following code:

Be aware that I’ve added something else on line 24: this Serial.println(“string_to_prompt”) just prompts a little message for the user, you can skip this line of code if you want, but it is useful to see when the Arduino is ready. Now, when you run this program and you send an angle using the serial monitor, all the servos should move to the same angle. This is nice and quite useful to test them and see if they work, however; this might not be very useful if we want to further test our robot. The final step now is to add a variable for the position of each additional servo and prompt the user accordingly. In the last program of this post we then send each position to the corresponding servo as in the next code:

The final note about this code is to remember to check the names. As said, the MeArm uses 4 servos, one on the base, two for the arm (I called them left and right hands servos on the code) and finally the one used to open and close the claw. The last program asks the user for the position of  each servo so in the future, you should be sure that you are connecting each one correctly. You can find these programs also in my github account.