roger_marin
Published

Robotic arm 4 servos

How to control and program a 4 servos robotic arm with a 4x4 keypad

IntermediateFull instructions provided4,668
Robotic arm 4 servos

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
RGB LCD Shield Kit, 16x2 Character Display
RGB LCD Shield Kit, 16x2 Character Display
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
I changed them for MG90S
×4
Parallax 4x4 membrane keypad
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Robotic Arm Kit
×1

Story

Read more

Schematics

Robot Arm servos

Code

Robot Arm 4 Servos controled by 4x4 kepad

Arduino
#include <Servo.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


Servo servoA; //Hand (open/close)
Servo servoB; // forward Backward 
Servo servoC; //up down
Servo servoD; // Rotate (left-right)


// The kepad
const byte ROWS = 4; 
const byte COLS = 4;
char keys[ROWS][COLS] = {
    
    {'1','2','3','A'}, 
    {'4','5','6','B'}, 
    {'7','8','9','C'},
    {'*','0','#','D'}};

byte rowPins[ROWS] = { 9, 8, 7, 13 };
byte colPins[COLS] = { 12, 4, 3, 2 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); 
String command;


char leter ='A'; // (Leter) servo to perform the command 
int angle = 0;  // (angle) Initial command

void setup()
{
// initialize the lcd  
lcd.init();                      
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Enter command"); 
// Pins for the servos
servoA.attach(5);
servoB.attach(6);
servoC.attach(10);
servoD.attach(11);

//Starting point 
servoA.write(80);
servoB.write(30);
servoC.write(120);
servoD.write(90);




}

void loop(){
 
char key = keypad.getKey();


  if (key){
            lcd.init();
            lcd.setCursor(0,0);
            lcd.print(String("Servo ")  + String(leter));
            lcd.setCursor(0,1); 
            lcd.print(String("Command  ")+String(command + key));

// * on the keyapd will reset the command
              if(key == '*') {
                      command = ""; 
                      lcd.init();
                      lcd.setCursor(0,0);
                      lcd.print(String("Servo ")  + String(leter)); 
                      lcd.setCursor(0,1); 
                      lcd.print("Command  ");
                      } 
//# on the keypad will execute the command on the chosen servo defined by (leter)                     
              else if(key == '#') 
                    {
                        
                                        lcd.init();                      // initialize the lcd
                                        lcd.setCursor(0,0);
                                        lcd.print(String("Servo ")  + String(leter)); // show on the LCD the servo that is chosen
                                        lcd.setCursor(0,1);
                                        lcd.print(String("Execute ")+String(command));  // show on the LCD the servo the comand is performing
                                        int i = command.toInt(); // capture on (i) the integer of the command (only numbers)
          
                             // For SERVO A
                              if(leter == 'A') {
                                                angle =servoA.read();
                                                if ( i >= angle) {
                                                              for (angle = angle; angle <= i; angle=angle +1)
                                                               {servoA.write(angle);                             
                                                               delay(10);                                                                              
                                                                }
                                                                }
                                                else 
                                                              { for (angle = angle; angle >= i; angle=angle- 1)
                                                               {servoA.write(angle);
                                                               delay(10);                                                                                                                                                     
                                                                }              
                                                                }
                                                                
                                           }
                               //For  SERVO A
                              if(leter == 'B') {
                                                angle =servoB.read();
                                                if ( i >= angle) {
                                                              for (angle = angle; angle <= i; angle=angle +1)
                                                               {servoB.write(angle);
                                                               delay(10);                                                                              
                                                                }
                                                                }
                                                else 
                                                              { for (angle = angle; angle >= i; angle=angle- 1)
                                                               {servoB.write(angle);
                                                               delay(10);                                                                                                                                                    
                                                                }              
                                                                }                                                                
                                           }
                             
                               // For  SERVO C
                              if(leter == 'C') {
                                                angle =servoA.read();
                                                if ( i >= angle) {
                                                              for (angle = angle; angle <= i; angle=angle +1)
                                                               {servoC.write(angle);
                                                               delay(10);
                                                                              
                                                                }
                                                                }
                                                else 
                                                              { for (angle = angle; angle >= i; angle=angle- 1)
                                                               {servoC.write(angle);
                                                                delay(10);                                                                                                                                               
                                                                }              
                                                                }
                                                                
                                           }
                               // For  SERVO D
                              if(leter == 'D'){
                                                angle =servoA.read();
                                                if ( i >= angle) {
                                                              for (angle = angle; angle <= i; angle=angle +1)
                                                               {servoD.write(angle);
                                                               delay(20);
                                                                              
                                                                }
                                                                }
                                                else 
                                                              { for (angle = angle; angle >= i; angle=angle- 1)
                                                               {servoD.write(angle);
                                                                delay(20);                                                                                                                                                       
                                                                }                                                                              }
                                                                
                                           }                                               
                              lcd.init(); 
                              lcd.setCursor(0,0);
                              lcd.print(String("Servo ")  + String(leter)); //write on the screen the servo chosen
                              command = "";// empty command 
                              }
              // Chosing the servo        
              else if(key == 'A' or key == 'B' or key == 'C' or key == 'D') 
                               {
                                  leter = key;
                                  lcd.init(); 
                                  lcd.setCursor(0,0);
                                  lcd.print(String("Servo ")  + String(leter));//print the name of the servo chosen  
                               if(leter == 'A') {
                                                
                                                lcd.println(servoA.read());    //print the value from the servo chosen           
                                                }
                               //  SERVO A
                              if(leter == 'B') {
                                                lcd.println(servoB.read());    //print the value from the servo chosen  
                                               }
                             
                               //  SERVO C
                              if(leter == 'C') {                                       
                                                lcd.println(servoC.read());   //print the value from the servo chosen
                                                }
                               //  SERVO D
                              if(leter == 'D') {
                                                lcd.println(servoD.read());   //print the value of the servo chosen
                                               }
                                  lcd.setCursor(0,1); 
                                  lcd.print(String("Command  ")+String(command)); //print the value of the command                 
                                  }

             
               else {
                command += key; //Add a digit to the command  
               
              }
  }}

Credits

roger_marin

roger_marin

6 projects • 13 followers

Comments