4 DOF Arduino Robot Arm – Full Tutorial

Intro

In this tutorial, I will cover how to build this 4 DOF Arduino robot arm you can order under $40. The tutorial contains 4 parts: 0. Unboxing; 1. Assembly; 2. Electronics; 3. Codes

You can order it here: https://www.banggood.com/custlink/KGvvDNs7RV

Part 0: Unboxing

Part 1: Assembly

Part 2: Electronics

Part 3: Codes


#include "VarSpeedServo.h"
#include <EEPROM.h>
//120a120b120c120d
//90a90b90c90d

//120A120B120C120D
//90a90b90c90d

VarSpeedServo servo1;
VarSpeedServo servo2;
VarSpeedServo servo3;
VarSpeedServo servo4;

int potpin1 = 0;//Define the Jaw servo potentiometer to A0
int potpin2 = 1;//Define the Front Arm servo potentiometer to A1
int potpin3 = 2;//Define the Back Arm servo potentiometer to A2
int potpin4 = 3;//Define the Base servo potentiometer to A3


int val1;
int val2;
int val3;
int val4;

byte  mode ;
void setup()
{

  servo1.attach(11);               
  servo2.attach(10);  
  servo3.attach(9);                
  servo4.attach(6);


  Serial.begin(9600);
  Serial.write("begin");
}
void loop()
{
  static int v = 0;
  

  if ( Serial.available()) 

  {

    char ch = Serial.read();      
    switch(ch) {
    case '0'...'9':
      v = v * 10 + ch - '0'; 
      Serial.println(v);

      break;
    case 'a':             
      {
        if(mode==1)
          servo1.write(v,10);
      }
      v = 0;
      Serial.println(v);
      break;
    case 'b':             
      {  
        if(mode==1)
          servo2.write(v,10);
      }
      v = 0;
      Serial.println(v);
      break;   
    case 'c':             

      {
        if(mode==1)
          servo3.write(v,10);

      }
      v = 0;
      Serial.println(v);
      break;   
    case 'd':             
      {
        if(mode==1)
          servo4.write(v,10);

      }
      v = 0;
      Serial.println(v);
      break; 

      case 'A':             
      {
        if(mode==1)
          servo1.write(v,10,1);
      }
      v = 0;
      Serial.println(v);
      break;
    case 'B':             
      {  
        if(mode==1)
          servo2.write(v,10,1);
      }
      v = 0;
      Serial.println(v);
      break;   
    case 'C':             

      {
        if(mode==1)
          servo3.write(v,10,1);

      }
      v = 0;
      Serial.println(v);
      break;   
    case 'D':             
      {
        if(mode==1)
          servo4.write(v,10,1);

      }
      v = 0;
      Serial.println(v);
      break; 

    case 'W':
      { delay(2000);
        }
        break;
    case 'm':             
      {

        Serial.print("Mode = pot  ");
        EEPROM.write( 1001, 0);
        mode = EEPROM.read(1001);
        Serial.println(mode);
      }
      break; 
      v = 0;

    case 'n':             
      {

        Serial.print("Mode = pc   ");
        EEPROM.write( 1001, 1);
        mode = EEPROM.read(1001);
        Serial.println(mode);
      }
      break;
      v = 0;
    }
  }

  if(mode==0)

  {
    /////Jaw Servo Control///////
    val1 = analogRead(potpin1);
    Serial.println(val1);
    val1 = map (val1, 0, 1023, 0, 179);
    Serial.println(val1);
    servo1.write(val1);
    delay(10);


    //////Front Arm Servo Control///////
    val2 = analogRead(potpin2);
    val2 = map (val2, 0, 1023, 0, 179);
    servo2.write(val2);
    delay(10);  

    //////Back Arm Servo Control///////
    val3 = analogRead(potpin3);
    val3 = map (val3, 0, 1023, 0, 179);
    servo3.write(val3);
    delay(10);

    //////Base Servo Control/////// 
    val4 = analogRead(potpin4);
    val4 = map (val4, 0, 1023, 0, 179);
    servo4.write(val4);
    delay(10);






  }



}

Download the codes here:

Robot Arm Code

Leave a Reply

Your email address will not be published. Required fields are marked *