Kontrol Robot ARM Dengan Arduino
Kontrol Robot ARM Dengan Arduino
Terdapat lima
servo : Base, Shoulder, Elbow, Wrist dan Grip. Jadi kita perlu lima pin
Arduino yang dihubungkan ke masing masing servo.
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
void loop()
{
if(Serial.available()>0)
{
str = Serial.read();
if(str!=’\n’)
{
inString += (char)str;
}
else if (str == ‘\n’)
{
temStr=inString.substring(0,1);
nomor=temStr.toInt();
temStr=inString.substring(2);
sudut=temStr.toInt();
Serial.print(nomor);
Serial.print(” : “);
Serial.println(sudut);
switch(nomor){
case 1:servo1.write(sudut); break;
case 2:servo2.write(sudut); break;
case 3:servo3.write(sudut); break;
case 4:servo4.write(sudut); break;
case 5:servo5.write(sudut); break;
}
inString = “”;
}
}
}
Jalannya program :
Untuk mengontrol gerakan masing-masing servo menggunakan perintah dari
serial komunikasi. Buka Serial Monitor. Perintah kontrolnya sebenarnya
bebas saja, dapat anda buat sendiri, pada contoh kita kali ini memakai
protokol komunikasi sebagai berikut :
[nomor servo] [spasi atau karakter lain] [besar sudut] [‘\n’]
Contoh, servo 1 kita set pada sudut 1200, cukup kita tulis “1 120” kemudian
klik Send maka servo 1 (base) akan berputar menuju posisi 1200.
Penjelasan program :
Pengontrolan motor servo menggunakan perintah dari library motor servo :
#include <Servo.h>
Jumlah servo ada 5, sehingga perlu kita membuat 5 identifikasi motor servo :
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Selanjutnya konfigurasi koneksi pin masing – masing servo dengan pin
Arduino, perhatikan schematic pada gambar 3.70. Servo1 terhubung ke pin 2,
servo 3 terhubung ke pin 4 dan seterusnya :
servo1.attach(2);//pin 2=servo1
servo2.attach(3);//pin 3=servo2
servo3.attach(4);//pin 4=servo3
servo4.attach(5);//pin 5=servo4
servo5.attach(6);//pin 6=servo5
Posisikan semua servo pada posisi tengah (900) :
servo1.write(90);
servo2.write(90);
servo3.write(90);
servo4.write(90);
servo5.write(90);
Berikutnya program utama hampir sama dengan program sebelumnya,
menunggu sampai ada perintah dari komputer. Perintahnya sesuai dengan
format/protokol yang sudah kita buat. Sebagai tanda kalau pengiriman
perintah sudah lengkap, disertakan karakter newline ‘\n’ pada akhir perintah.
Ketika ada data masuk maka Serial.available()>0 bernilai “true” sehingga
pengujian kondisinya menjadi benar. Variabel str untuk menampung data
yang masuk :
if(Serial.available()>0)
{
str = Serial.read();
Jika data bukan newline ‘\n’ maka pengiriman data belum komplit, tampung,
gabungkan dan simpan ke inString.
if(str!=’\n’)
{
inString += (char)str;
}
Setelah karakter newline diterima berarti sudah diterima sebuah perintah :
else if (str == ‘\n’)
Selanjutnya kita olah data yang diterima, kita ambil mana yang merupakan
perintah untuk nomor servo dan mana yang merupakan perintah besar
sudutnya.
Ambil satu karakter pertama, yaitu nomor servo yang akan dikontrol :
temStr=inString.substring(0,1);
Nilainya masih string, kita konversi menjadi integer :
nomor=temStr.toInt();
Posisi karakter yang menunjukkan besaran sudut dimulai pada karakter
ketiga atau urutan kedua. Contoh : “2 150”, karakter 1 (angka pertama dari
150) ada pada posisi kedua, sehingga perintah berikutnya adalah mengambil
karakter mulai urutan kedua :
temStr=inString.substring(2);
Nilai inipun masih bertipe string sehingga perlu dikonversi ke integer :
sudut=temStr.toInt();
Selanjutnya kita cek servo keberapa yang akan dikontrol, dengan perintah :
switch(nomor){
case 1:servo1.write(sudut); break;
case 2:servo2.write(sudut); break;
case 3:servo3.write(sudut); break;
case 4:servo4.write(sudut); break;
case 5:servo5.write(sudut); break;
}
Jika servo yang dikontrol adalah servo 1 maka isi variabel nomor adalah 1 dan
case 1 yang akan dikerjakan yaitu mengontrol servo 1 sesuai dengan nilai
sudutnya. Demikian juga untuk nomor servo yang lain, terakhir variabel
inString dikosongkan nilainya untuk dipakai pada proses penerimaan
perintah berikutnya :
inString = “”;
Ini foto hardwarenya :
Ok, kalau tadi ngontrolnya pake program serialnya Arduino, besok saya
sampaikan pake yang lebih ‘berwarna’ semisal Delphi, tunggu aja ya…
CARA 2
Components Required:
Arduino Uno
1000uF Capacitor (4 pieces)
100nF Capacitor (4 pieces)
Servo Motor (SG 90- four pieces)
10K pot- Variable Resistor (4 pieces)
Power Supply (5v- preferably two)
Servo Motor:
First we talk a bit about Servo Motors. Servo Motors are excessively used when there is
a need for accurate shaft movement or position. These are not proposed for high speed
applications. Servo motors are proposed for low speed, medium torque and accurate
position application. So these motors are best for designing robotic arm.
Servo motors are available at different shapes and sizes. We are going to use small
servo motors, here we use four SG90 servos. A servo motor will have mainly there
wires, one is for positive voltage another is for ground and last one is for position setting.
The RED wire is connected to power, Black wire is connected to ground and YELLOW
wire is connected to signal. Go through this tutorial of Controlling Servo Motor using
Arduino to learn more about it. In Arduino we have predefined libraries to control the
Servo, so it is very easy to control servo, which you will learn along with this tutorial.
Place a small piece of cardboard on top of first servo and then place the second
servo on this piece of board and glue it in place. The servo rotation must match the
diagram.
Take some cardboards and cut them into 3cm x 11cm pieces. Make sure the piece is not
softened. Cut a rectangular hole at one end (leave 0.8cm from bottom) just enough to fit
another servo and at another end fit the servo gear tightly with screws or by glue. Then fit
the third servo in the first hole.
Now cut another cardboard piece with lengths shown in figure below and glue another
gear at the bottom of this piece.
Now glue the fourth and last servo at the edge of second piece as shown in figure.
With this, two pieces together looks like.
Mount this piece on top and with this we have done building our Robotic Arm.
With this, our basic robotic arm design got completed and that's how we have built our
low cost robotic arm. Now connect the circuit in breadboard as per circuit diagram.
Now, for the UNO to convert analog signal into digital signal, we need to Use ADC
Channel of Arduino Uno, with the help of below functions:
1. analogRead(pin);
2. analogReference();
3. analogReadResolution(bits);
Arduino ADC channels have a default reference value of 5V. This means we can give a
maximum input voltage of 5V for ADC conversion at any input channel. Since some
sensors provide voltages from 0-2.5V, so with a 5V reference, we get lesser accuracy, so
we have an instruction that enables us to change this reference value. So for changing
the reference value we have “analogReference();”
As default we get the maximum board ADC resolution which is 10bits, this resolution can
be changed by using instruction (“analogReadResolution(bits);”).
In our Robotic hand circuit, we have left this reference voltage to the default, so we
can read value from ADC channel by directly calling function “analogRead(pin);”, here
“pin” represents pin where we connected the analog signal, say we want to read “A0”.
The value from ADC can be stored into an integer as int SENSORVALUE0 =
analogRead(A0);.
Now let’s talk about the SERVO, the Arduino Uno has a feature which enables us to
control the servo position by just giving the degree value. Say if we want the servo to be
at 30, we can directly represent the value in the program. The SERVO header (Servo.h)
file takes care of all the duty ratio calculations internally.
#include <Servo.h>
servo servo0;
servo0.attach(3);
servo0.write(degrees);
Here first statement represents the header file for controlling the SERVO MOTOR.
Second statement is naming the servo; we leave it as servo0 as we are going to use four.
Third statement states where the servo signal pin is connected; this must be a PWM pin.
Here we are using PIN3 for first servo. Fourth statement gives commands for positioning
servo motor in degrees. If it is given 30, the servo motor rotates 30 degrees.
Now, we have SG90 servo position from 0 to 180 and the ADC values are from 0-1023.
We will use a special function which matches both values automatically.
This statement maps both values automatically and stores the result in
integer ‘servovalue0’.
This is how we have controlled the Servos in our Robotic Arm project using
Arduino. Check the full codebelow.
Code
#include <Servo.h>
Servo servo0;
Servo servo1;
Servo servo2;
Servo servo3;
int sensorvalue0;
int sensorvalue1;
int sensorvalue2;
int sensorvalue3;
void setup()
{
pinMode(A0,INPUT);
pinMode(3,OUTPUT);
servo0.attach(3);
pinMode(A1,INPUT);
pinMode(5,OUTPUT);
servo1.attach(5);
pinMode(A2,INPUT);
pinMode(6,OUTPUT);
servo2.attach(6);
pinMode(A3,INPUT);
pinMode(9,OUTPUT);
servo3.attach(9);
}
void loop()
{
sensorvalue0 = analogRead(A0);
sensorvalue0 = map(sensorvalue0, 0, 1023, 0, 180);
servo0.write(sensorvalue0);
sensorvalue1 = analogRead(A1);
sensorvalue1 = map(sensorvalue1, 0, 1023, 0, 180);
servo1.write(sensorvalue1);
sensorvalue2 = analogRead(A2);
sensorvalue2 = map(sensorvalue2, 0, 1023, 0, 180);
servo2.write(sensorvalue2);
sensorvalue3 = analogRead(A3);
sensorvalue3 = map(sensorvalue3, 0, 1023, 0, 180);
servo3.write(sensorvalue3);
}
Video