Exercise 36 - Robotic arm
1. INTRODUCTION
When this project is finished, you can control up to six servo-engines (on this example only control 5) from your Smartphone or tablet.
To start, it is necessary to know how to connect and control a servo-engine. It is all explained at CONNECTING_A_SERVO_MOTOR.
Materials
· Five or six servo-engines.
· Plastic or aluminium pieces to build the arm structure.
· Nuts and bolts to assemble the pieces.
· Arduino UNO board.
· Sensor Shield.
· Jumpers (male-female)
· 5V power supply or H-bridge to adapt the voltage.
2. HOW TO CONNECT?
You can choose the number of servos that you want to connect, but the maximum is six. On the other hand, you can connect them to the Arduino board directly, or you can use Sensor shield, which simplify the connections.
You can numerate the servos to look the project more clearly.
Before you connect the servos, you have to build the structure, but you can try the program without it. You can build it following the next steps:
![]() |
![]() |
---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
To connect the robotic arm you have to connect 5V and GND terminals from servos to power supply, you can use an H-bridge to transform the voltage to 5 volts if you do not have any 5v power supply.
Warning: Whether you go to connect the H - bridge, you will be careful when you connect the power supply. You have to connect the power supply to the H - bridge before connecting the servos to it. If you connect the servos before, you could break H-bridge because at the start all of servos move at the same time and this produce many harmonics in the circuit.
On the other hand, you have to connect data terminal from
servos to (pin13-12-11-9-8). Finally, it
is important to connect GND on Arduino board with Ground on the power supply.
EXERCISE_A_&_B |
Note: When you connect a two o more servos to the same Arduino board, they send to each other a noisy signal when they write their position. To reduce it you can connect capacitors like in the image for each servo-engine.
The capacitor between 5V_Arduino and GND_Arduino not is
necessary, but it is recommended.
3. PROGRAMMING
On both exercises, you have to include Servo.h library.
EXERCISE_A
This is a simple program to calibrate the servos before attach to structure, this is important because you can break it if you put it in a position that it cannot rotate.
#include <Servo.h>
Servo servo1; // create servo object to control a servo
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
int pos1;
int pos2;
int pos3;
int pos4;
int pos5;
int pos6;
void setup() {
servo1.attach(13); // attaches the servo on pin 9 to the servo object
servo2.attach(12);
servo3.attach(11);
servo4.attach(10);
servo5.attach(9);
servo6.attach(8);
Serial.begin(9600);
}
On loop, the position of the servos is written and read and then, it is printed on Serial monitor.
void loop() {
servo1.write(108);
servo2.write(180);
servo3.write(80);
servo4.write(80);
servo5.write(100);
servo6.write(90);
pos1 = servo1.read();
pos2 = servo2.read();
pos3 = servo3.read();
pos4 = servo4.read();
pos5 = servo5.read();
pos6 = servo6.read();
Serial.print(pos1);
Serial.print(" ");
Serial.print(pos2);
Serial.print(" ");
Serial.print(pos3);
Serial.print(" ");
Serial.print(pos4);
Serial.print(" ");
Serial.print(pos5);
Serial.print(" ");
Serial.print(pos6);
Serial.println(" ");
}
EXERCISE_B
Note: To use this exercise you have to install EDUTRONIX_ARM.apk
First, variables and servos are declared, also SoftwareSerial.h and Servo.h are included in sketch.
#include <SoftwareSerial.h> // Include SoftwareSerial library
#include <Servo.h>
SoftwareSerial BT(3, 2); /*Define pins Rx and Tx on Arduino, which are connected to Bluetooth.*/
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
String value = "";
String value2 = "";
int pos = 0;
int caso = 0;
On setup
void setup()
{
BT.begin(9600); //Initialize serial port which has been created.
// Serial.begin(9600); //Initialize serial port.
servo1.attach(13);
servo2.attach(12);
servo3.attach(11);
servo4.attach(10);
servo5.attach(9);
servo6.attach(8);
}
void loop()
{
// first read from Bluetooth
//If read is a capital letter get in switch
if (BT.available()) //If data arrives by BT, it is sent by Serial monitor.
{
value = String("");
On while, the received string is read character by character when string finishes all of characters read come together in the same variable.
while (BT.available()) {
value = value + char(BT.read());
delay(1);
}
}
When a key is pressed on the app, it sends a character (A...F) to Arduino, which select the servo in function of key pressed, and next on the app, you can modify the servo write by a slider. When you press "PRINCIPAL" key on any screen the app sends to Arduino "a" and then Arduino try to find a character to select Servo another time.
Whether character read is A, B, C, D, E or F the flow goes in while, which actuates as loop while value maintains the same value. On the other hand, while condition the servo where you want to write is selected by if conditions. Finally, when a character “a” is received the flow exit from while condition.
while (value == "A" || value == "B" || value == "C" ||
value == "D" || value == "E" || value == "F" ) {
// Serial.println("Ha entrado");
if (BT.available()) {
value2 = String("");
while (BT.available()) {
value2 = value2 + char(BT.read());
delay(1);
}
if (value2 != "a") {
pos = value2.toInt();
if (pos >= 0 && pos <= 180) {
//On this condition the write location is selected.
if(value=="A"){
servo1.write(pos);
}
if(value=="B"){
servo2.write(pos);
}
if(value=="C"){
servo3.write(pos);
}
if(value=="D"){
servo4.write(pos);
}
if(value=="E"){
servo5.write(pos);
}
if(value=="F"){
servo6.write(pos);
}
}
}
else if (value2 == "a") {
value = "";
}
}
}
}