AZ-Delivery
Published

Arduino controlled 6-axis robot arm

How to build a robotarm with Servos and a Arduino.

IntermediateFull instructions provided4 hours8,206
Arduino controlled 6-axis robot arm

Things used in this project

Hardware components

Microcontroller Board (e.g. Arduino UNO)
×1
Servo Expansion Board PCA9685
×1
MG995 Servo
×3
MG90S Servo
×2
SG90 Micro Servo
×1
m2f Jumpercable
×1
3mm Plywood
×1
Wood Glue
×1
Wooden Dowels
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Parts

Schematics

Fritzing schematic

Code

Robot Arm

Arduino
/********************************************************************************************************
*													                                                                              *
*   This sketch controls the servo motors of the robotic arm. 				   	                              *
*   Lasta revision 07-11-2021                                                                           *
*   Miguel Torres Gordo											                                                            *
*   27-10-2021 Getafe (Madrid) - Spain									                                                *
*													                                                                              *
********************************************************************************************************/


#include <Wire.h>						// Library for I2C communications
#include <Adafruit_PWMServoDriver.h>				// PCA9685 Library

Adafruit_PWMServoDriver pca9685 = Adafruit_PWMServoDriver();	// Implementation of an object of module PCA9685 

#define SERVOMIN  100                           		// This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  500                           		// This is the 'maximum' pulse length count (out of 4096)


int velocidad = 450;         					// ******* Velocidad de movimiento



void setup() {
      Serial.begin(9600);
      pca9685.begin();
      pca9685.setPWMFreq(50);                         		// Analog servos run at ~50 Hz updates
}

void loop() {
        home();
        delay(1000);
        ready();
        delay(1000);
        get_ready();
        delay(1000);
        pick_up_first_object();
        delay(1000);
        prepare_download_first_object();
        delay(1000);
        download_first_object();
        delay(1000);
        exit_download_first_object();
        delay(1000);
        pick_up_second_object();
        delay(1000);
        prepare_download_first_object();
        delay(1000);
        download_second_object();
        delay(1000);
        ready();
        delay(1000);
        home();
        delay(1000);

        delay(60000);			// 60 sg wait to prevent scketch from being repeated
}


void home() {				// Servo Motors position for Robot Arm home position

       // Servo Motor 5
        pca9685.setPWM(5, 0, 265 );
        delay(velocidad);

        // Servo Motor 1
        pca9685.setPWM(1, 0, 499);
        delay(velocidad); 

        // Servo Motor 2
        pca9685.setPWM(2, 0, 499 );
        delay(velocidad); 
 
        // Servo Motor 4
        pca9685.setPWM(4, 0, 188);
        delay(velocidad);
        
        // Servo Motor 3
        pca9685.setPWM(3, 0, 150 );
        delay(velocidad);

        // Servo Motor 0
        pca9685.setPWM(0, 0, 166);
        delay(velocidad);
}

       
void ready() {				// Position of the Servo Motors for the first movement of the Robot Arm
 
        // Servo Motor 4
        pca9685.setPWM(4, 0, 295);
        delay(velocidad);
        
        // Servo Motor 3
        pca9685.setPWM(3, 0, 220);
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 300);
        delay(velocidad);
         
        // Servo Motor 1
        pca9685.setPWM(1, 0, 300);
        delay(velocidad);
}


void get_ready() {			// Position of the Servo Motors to prepare to pick up the first object

        // Servo Motor 5
        pca9685.setPWM(5, 0, 350);
        delay(velocidad);

        // Servo Motor 3
        pca9685.setPWM(3, 0, 150 );
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 415);
        delay(velocidad);

        // Servo Motor 4
        pca9685.setPWM(4, 0, 200);
        delay(velocidad);

        // Servo Motor 0
        pca9685.setPWM(0, 0, 200);
        delay(velocidad);   
}

void pick_up_first_object() {		// Position of the Servo Motors to pick up the first object

        // Servo Motor 4
        pca9685.setPWM(4, 0, 210);
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 405);
        delay(velocidad);

        // Servo Motor 3				// Loop for to move the Servo Motor 3 slowly
        for (int pos=150; pos<180; pos +=1) {
            	pca9685.setPWM(3, 0, pos);
            	delay(10);
        }

        // Servo Motor 2				// Loop for to move the Servo Motor 2 slowly
        for (int pos=405; pos>350; pos -=1) {
              	pca9685.setPWM(2, 0, pos);
              	delay(10);
        }

        // Servo Motor 0				// Loop to close the clamp slowly
        for (int pos=200; pos>166; pos -=1) {
            	pca9685.setPWM(0, 0, pos);
            	delay(10);
        } 
}

void prepare_download_first_object() {			// Position of the Servo Motors to prepare for download the first object

        // Servo Motor 4
        pca9685.setPWM(4, 0, 295);
        delay(velocidad);
  
        // Servo Motor 3
        pca9685.setPWM(3, 0, 220);
        delay(velocidad);
  
        // Servo Motor 5
        pca9685.setPWM(5, 0, 265 );
        delay(velocidad);
        
        // Servo Motor 2
        pca9685.setPWM(2, 0, 300);
        delay(velocidad);
}

void download_first_object() {				// Position of the Servo Motors to download the first objet

        // Servo Motor 5
        pca9685.setPWM(5, 0, 210);
        delay(velocidad);

        // Servo Motor 3
        pca9685.setPWM(3, 0, 175);
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 460);
        delay(velocidad);

        // Servo Motor 4
        pca9685.setPWM(4, 0, 195);
        delay(velocidad);

        // Servo Motor 0				// Loop to open the clamp slowly
        for (int pos=166; pos<200; pos +=1) {
            	pca9685.setPWM(0, 0, pos);
            	delay(10);
        }
}        


void exit_download_first_object() {

        // Servo Motor 3				// Loop to start slowly backing out the Robot Arm
        for (int pos=175; pos>150; pos -=1) {
              	pca9685.setPWM(3, 0, pos);
              	delay(10);
        }

        // Servo Motor 4
        pca9685.setPWM(4, 0, 295);
        delay(velocidad);
    
        // Servo Motor 5
        pca9685.setPWM(5, 0, 265 );
        delay(velocidad);
        
        // Servo Motor 2
        pca9685.setPWM(2, 0, 300);
        delay(velocidad);
         
        // Servo Motor 1
        pca9685.setPWM(1, 0, 300);
        delay(velocidad);     
}

void pick_up_second_object() {

        // Servo Motor 5
        pca9685.setPWM(5, 0, 300 );
        delay(velocidad);

        // Servo Motor 0				// Open the clamp slowly
        pca9685.setPWM(0, 0, 200);
        delay(velocidad);
 
        // Servo Motor 4
        pca9685.setPWM(4, 0, 245);
        delay(velocidad);
        
        // Servo Motor 3
        pca9685.setPWM(3, 0, 220 );
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 170 );
        delay(velocidad); 
         
        // Servo Motor 1
        pca9685.setPWM(1, 0, 470);
        delay(velocidad);

        // Servo Motor 4				// loop to slowly approach to the second object
        for (int pos=230; pos>210; pos -=1) {
              	pca9685.setPWM(4, 0, pos);
              	delay(10);
        }

        // Servo Motor 0				// Loop to close the clamp slowly
        for (int pos=200; pos>166; pos -=1) {		
            	pca9685.setPWM(0, 0, pos);
            	delay(10);
        }
}

void download_second_object() {
  
        // Servo Motor 5
        pca9685.setPWM(5, 0, 360);
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 300);
        delay(velocidad);

        // Servo Motor 3
        pca9685.setPWM(3, 0, 210);
        delay(velocidad);

        // Servo Motor 0				// Open the clamp
        pca9685.setPWM(0, 0, 200);
        delay(velocidad);
}


         

pick up first Object

Arduino
/********************************************************************************************************
*													                                                                              *
*   This sketch controls the servo motors of the robotic arm. 				   	                              *
*   Lasta revision 07-11-2021                                                                           *
*   Miguel Torres Gordo											                                                            *
*   27-10-2021 Getafe (Madrid) - Spain									                                                *
*													                                                                              *
********************************************************************************************************/


#include <Wire.h>						// Library for I2C communications
#include <Adafruit_PWMServoDriver.h>				// PCA9685 Library

Adafruit_PWMServoDriver pca9685 = Adafruit_PWMServoDriver();	// Implementation of an object of module PCA9685 

#define SERVOMIN  100                           		// This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  500                           		// This is the 'maximum' pulse length count (out of 4096)


int velocidad = 450;         					// ******* Velocidad de movimiento



void setup() {
      Serial.begin(9600);
      pca9685.begin();
      pca9685.setPWMFreq(50);                         		// Analog servos run at ~50 Hz updates
}

void loop() {
        home();
        delay(1000);
        ready();
        delay(1000);
        get_ready();
        delay(1000);
        pick_up_first_object();
        delay(1000);
        prepare_download_first_object();
        delay(1000);
        download_first_object();
        delay(1000);
        exit_download_first_object();
        delay(1000);
        pick_up_second_object();
        delay(1000);
        prepare_download_first_object();
        delay(1000);
        download_second_object();
        delay(1000);
        ready();
        delay(1000);
        home();
        delay(1000);

        delay(60000);			// 60 sg wait to prevent scketch from being repeated
}


void home() {				// Servo Motors position for Robot Arm home position

       // Servo Motor 5
        pca9685.setPWM(5, 0, 265 );
        delay(velocidad);

        // Servo Motor 1
        pca9685.setPWM(1, 0, 499);
        delay(velocidad); 

        // Servo Motor 2
        pca9685.setPWM(2, 0, 499 );
        delay(velocidad); 
 
        // Servo Motor 4
        pca9685.setPWM(4, 0, 188);
        delay(velocidad);
        
        // Servo Motor 3
        pca9685.setPWM(3, 0, 150 );
        delay(velocidad);

        // Servo Motor 0
        pca9685.setPWM(0, 0, 166);
        delay(velocidad);
}

       
void ready() {				// Position of the Servo Motors for the first movement of the Robot Arm
 
        // Servo Motor 4
        pca9685.setPWM(4, 0, 295);
        delay(velocidad);
        
        // Servo Motor 3
        pca9685.setPWM(3, 0, 220);
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 300);
        delay(velocidad);
         
        // Servo Motor 1
        pca9685.setPWM(1, 0, 300);
        delay(velocidad);
}


void get_ready() {			// Position of the Servo Motors to prepare to pick up the first object

        // Servo Motor 5
        pca9685.setPWM(5, 0, 350);
        delay(velocidad);

        // Servo Motor 3
        pca9685.setPWM(3, 0, 150 );
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 415);
        delay(velocidad);

        // Servo Motor 4
        pca9685.setPWM(4, 0, 200);
        delay(velocidad);

        // Servo Motor 0
        pca9685.setPWM(0, 0, 200);
        delay(velocidad);   
}

void pick_up_first_object() {		// Position of the Servo Motors to pick up the first object

        // Servo Motor 4
        pca9685.setPWM(4, 0, 210);
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 405);
        delay(velocidad);

        // Servo Motor 3				// Loop for to move the Servo Motor 3 slowly
        for (int pos=150; pos<180; pos +=1) {
            	pca9685.setPWM(3, 0, pos);
            	delay(10);
        }

        // Servo Motor 2				// Loop for to move the Servo Motor 2 slowly
        for (int pos=405; pos>350; pos -=1) {
              	pca9685.setPWM(2, 0, pos);
              	delay(10);
        }

        // Servo Motor 0				// Loop to close the clamp slowly
        for (int pos=200; pos>166; pos -=1) {
            	pca9685.setPWM(0, 0, pos);
            	delay(10);
        } 
}

void prepare_download_first_object() {			// Position of the Servo Motors to prepare for download the first object

        // Servo Motor 4
        pca9685.setPWM(4, 0, 295);
        delay(velocidad);
  
        // Servo Motor 3
        pca9685.setPWM(3, 0, 220);
        delay(velocidad);
  
        // Servo Motor 5
        pca9685.setPWM(5, 0, 265 );
        delay(velocidad);
        
        // Servo Motor 2
        pca9685.setPWM(2, 0, 300);
        delay(velocidad);
}

void download_first_object() {				// Position of the Servo Motors to download the first objet

        // Servo Motor 5
        pca9685.setPWM(5, 0, 210);
        delay(velocidad);

        // Servo Motor 3
        pca9685.setPWM(3, 0, 175);
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 460);
        delay(velocidad);

        // Servo Motor 4
        pca9685.setPWM(4, 0, 195);
        delay(velocidad);

        // Servo Motor 0				// Loop to open the clamp slowly
        for (int pos=166; pos<200; pos +=1) {
            	pca9685.setPWM(0, 0, pos);
            	delay(10);
        }
}        


void exit_download_first_object() {

        // Servo Motor 3				// Loop to start slowly backing out the Robot Arm
        for (int pos=175; pos>150; pos -=1) {
              	pca9685.setPWM(3, 0, pos);
              	delay(10);
        }

        // Servo Motor 4
        pca9685.setPWM(4, 0, 295);
        delay(velocidad);
    
        // Servo Motor 5
        pca9685.setPWM(5, 0, 265 );
        delay(velocidad);
        
        // Servo Motor 2
        pca9685.setPWM(2, 0, 300);
        delay(velocidad);
         
        // Servo Motor 1
        pca9685.setPWM(1, 0, 300);
        delay(velocidad);     
}

void pick_up_second_object() {

        // Servo Motor 5
        pca9685.setPWM(5, 0, 300 );
        delay(velocidad);

        // Servo Motor 0				// Open the clamp slowly
        pca9685.setPWM(0, 0, 200);
        delay(velocidad);
 
        // Servo Motor 4
        pca9685.setPWM(4, 0, 245);
        delay(velocidad);
        
        // Servo Motor 3
        pca9685.setPWM(3, 0, 220 );
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 170 );
        delay(velocidad); 
         
        // Servo Motor 1
        pca9685.setPWM(1, 0, 470);
        delay(velocidad);

        // Servo Motor 4				// loop to slowly approach to the second object
        for (int pos=230; pos>210; pos -=1) {
              	pca9685.setPWM(4, 0, pos);
              	delay(10);
        }

        // Servo Motor 0				// Loop to close the clamp slowly
        for (int pos=200; pos>166; pos -=1) {		
            	pca9685.setPWM(0, 0, pos);
            	delay(10);
        }
}

void download_second_object() {
  
        // Servo Motor 5
        pca9685.setPWM(5, 0, 360);
        delay(velocidad);

        // Servo Motor 2
        pca9685.setPWM(2, 0, 300);
        delay(velocidad);

        // Servo Motor 3
        pca9685.setPWM(3, 0, 210);
        delay(velocidad);

        // Servo Motor 0				// Open the clamp
        pca9685.setPWM(0, 0, 200);
        delay(velocidad);
}


         

Servo Check

Arduino
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN  100                           // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  500                           // This is the 'maximum' pulse length count (out of 4096)

void setup() {
  Serial.begin(9600);
  Serial.println("Reference check of 120 degrees with respect to the pulse length count");

  pwm.begin();
  pwm.setPWMFreq(50);                         // Analog servos run at ~50 Hz updates

}

void loop() {

    for (int pos=165; pos<430; pos +=10) {		// Loop with movement slow from 30 degrees to 150 degrees 
      pwm.setPWM(0, 0, pos );
      Serial.println("165 pulse length count --> 30 degrees");
      delay(50);
    }

    delay (5000);

    for (int pos_m=430; pos_m>165; pos_m -=10) {	// Loop with movement slow from 150 degrees to 30 degrees
      pwm.setPWM(0, 0, pos_m );
      Serial.println("430 pulse length count --> 150 degrees");
      delay(50);
    }

    delay (5000);

}

Credits

AZ-Delivery

AZ-Delivery

4 projects • 1 follower
We are "AZ-Delivery - Your Expert for Microelectronics". Visit our shop with regularly new Blogs and free E-Books.
Thanks to Miguel Torres Gordo.

Comments