A Robotic Arm, Based on Mg90s Servos and 28BYJ-48 Stepper Motor.

by tagvian in Circuits > Robots

359 Views, 6 Favorites, 0 Comments

A Robotic Arm, Based on Mg90s Servos and 28BYJ-48 Stepper Motor.

WhatsApp Image 2024-12-09 at 19.46.34.jpeg
Robotic arm picking up, and moving eraser
Robotic arm picking up 3 erasers.

Hi! I’m Wiktor, a 13-year-old electronics, robotics, and programming enthusiast. Recently, I created a robotic arm that you can control and program using a computer app (which I also developed!).

I designed this robotic arm using Fusion 360 for 3D modeling and Visual Studio Code for programming. It features MG90s servo motors, a 28BYJ-48 stepper motor, and a lot of 3D-printed parts, screws, and heat inserts.

Supplies

WhatsApp Image 2024-12-09 at 22.34.10.jpeg

3d printed parts.

  1. Base compartment x 1
  2. Base lid x 1
  3. Left gripper x 1
  4. Right gripper x 1
  5. Lower arm x 1
  6. Upper arm x 1
  7. Base disc x 1
  8. Shoulder servo holder x 1
  9. Gripper servo holder x 1
  10. Gripper holder x 1

Electronics

  1. MG90s servo x 2
  2. SG92R servo x 1 (can be replaced with an MG90s)
  3. 28BYJ-48 stepper motor x 1
  4. ULN2003 driver x 1 (I have a red one, bigger green ones are also common)
  5. Custom "servo driver" board x 1 (See Step 1)
  6. Arduino Uno

Hardware

  1. Servo horn x 3 (The type shown in the picture)
  2. M4x12mm bolt x 2
  3. M4 nut x 2
  4. M3x10mm bolt x 2 (12mm also works)
  5. M2x12mm bolt x 4
  6. M2 washer x 8
  7. M2x7mm bolt x 4 (12mm cut down to 7mm)
  8. M3X12mm bolt x 4
  9. Screws for securing servo horns x 3
  10. M2x7mm screw x 1 (From the servo "kit")
  11. M3x5mm heat insert x 2

Cables

  1. 20cm Male-Female jumper wire x 9
  2. 10cm Female-Female jumper wire x 2
  3. 10cm servo wire extension x 1 (Can be replaced with normal jumper wires)

Custom "servo Driver" Board

Custom_board_circuit.png
WhatsApp Image 2024-12-10 at 14.24.53.jpeg

Why make this board?

I didn't have access to a normal servo driver when I made this project, so I decided to make my own. It's very simple; it essentially just connects all the anodes and cathodes together.

How to make such a board?

To make such a board you are going to need:

  1. Small piece of perf board
  2. Pin headers
  3. Some small pieces of wire.
  4. Soldering tools.

Then you should take the board and pins and solder them in place, as shown on the image.

After which you can connect all the positive and negative pins.

You are also going to need to connect the signal pins,

signal pin 1(Next to power rails) - signal pin 1

signal pin 2(Next to power rails) - signal pin 2

etc.

Why is the board designed like this?

The most confusing part is the signal pins so let me explain.

I wanted to be able to plug the servo connectors straight into the board, which means we've got to have 3 pins next to each other. But if I just left the pins like this, there would be no way to connect it to the Arduino.

Base

WhatsApp Image 2024-12-10 at 15.20.37.jpeg
WhatsApp Image 2024-12-10 at 15.20.36.jpeg
WhatsApp Image 2024-12-10 at 15.20.36 (1).jpeg
WhatsApp Image 2024-12-10 at 15.20.36 (2).jpeg

Now let's start assembling our robot.

The base is designed with pretty loose tolerances so it can be printed on almost any 3d printer.

But this means you have to slightly bend it for it to be a tight fit.

Bending the base

Warning: To bend the base you will need a lighter. Please be careful! Lighters can be very dangerous.

First of all, you will need to pick a spot to bend the base.

I recommend bending slightly above the hole for our USB cable.

Now, because the plastic we print with is very hard it's impossible to bend in normal conditions, so you have to heat it so it becomes elastic.

To do that you can simply use a lighter.

After carefully heating the spot you chose press down with some kind of a round object.

If you've done everything correctly you should be able to press the lid on, and it shouldn't slip off.

If your lid keeps slipping, just repeat the above steps.

The finished product will look similar to the image.

Mounting the stepper motor.

To mount the stepper motor we will use two M4x12 bolts and two M4 nuts.

Place the stepper motor in such a way that the axle goes through the middle hole, and the screw holes align.

Put the bolts through the holes and secure them with nuts.

Make sure that the bolt heads are sticking out of the top, if the nuts are sticking out the top they will collide with the base disc.

If done correctly the result should be similar to the image.

Base disc

Now you can take the base disc, and add the M3 heat inserts.

Warning: Soldering irons are VERY dangerous, when using one please be very careful.

Gently place the heat inserts over the holes on the base disc.

Then take a soldering iron heated to ~300C, and gently push down the heat inserts into the part.

The heat inserts sometimes get stuck the the soldering iron, in this case just grab tweezers and make sure the heat inserts stay where they are supposed to be.

The finished result should be similar to the image.

Putting it all together

Now you can put the base disc on the stepper motor.

And put the lid on the box.

Congratulations!!! You assembled the base.

Wiring

circuit_image (1).png
WhatsApp Image 2024-12-10 at 15.59.32.jpeg

Before we continue with the rest it's a good idea to connect everything.

Stepper

First of all connect the stepper motor to the driver board, as shown in the diagram.

Now you can connect the pins to our Arduino.

  1. IN1 - 2(Arduino)
  2. IN2 - 3(Arduino)
  3. IN3 - 4(Arduino)
  4. IN4 - 5(Arduino)

The above connections are also shown on the diagram.

Custom board

Connect the topmost two pins to the Arduino in this way:

  1. Red pin - +5V
  2. Black pin - GND

Make sure that the power pins from the stepper driver board are connected to this board,

above the 3-pin wide section(Just under where you plugged in the Arduino pins).

You should also connect the signal pins to the Arduino.

  1. Signal pin 1(Left most) - 9(Arduino)
  2. Singal pin 2 - 10(Arduino)
  3. Singal pin 3 - 11(Arduino)

Note:

The bottom signal pin(next to the power rails) corresponds to the leftmost pin from the upper signal header etc.

The lower the servo connector the lower the joint ex. The bottom pins are for the shoulder joint, the next ones are for the elbow, etc.

Servos

Now the servo connections are very easy, just connect them to their corresponding pins as explained above.

Make sure to put all the servo wires through the hole in the lid.


An additional tip, place the Arduino in the same way I placed it in the image, this way you can plug it in without opening the box.

Shoulder Joint

WhatsApp Image 2024-12-22 at 13.06.55.jpeg
WhatsApp Image 2024-12-22 at 13.06.54.jpeg
WhatsApp Image 2024-12-22 at 13.06.54 (1).jpeg
WhatsApp Image 2024-12-22 at 13.06.54 (3).jpeg
WhatsApp Image 2024-12-22 at 13.06.53.jpeg

Now it's time to assemble some joints.

Shoulder servo holder

Take your shoulder servo holder and two M3x10 bolts, and secure the servo holder in place.

The finished result should look similar to the image.

Installing the servo

The servo installation is very simple.

Just put it in the holder and secure it with two M2x12mm bolts with two washers added.

The screws should just easily go into the plastic and hold pretty well, if they aren't holding use an M2 heat insert.

The finished result should look similar to the image

Attaching the lower arm

First of all, grab a servo horn, then put one of the little screws for securing horns in the horn.

Then put the horn(with the screw) into the lower arm. You can identify the lower arm by checking if it has a slot for another servo.

And secure it to the arm with an M2X7mm bolt.

Now before we continue we need to set the shoulder servo to the 90-degree position.

To do that we will need to upload a simple sketch to the Arduino.

To do this install Arduino IDE, plug in your Arduino, select your port, select "Arduino Uno" for the board, and upload the following code:

#include <Servo.h>

Servo myServo; // Create a Servo object

void setup() {
myServo.attach(9); // Attach the servo to pin 9
myServo.write(90); // Set servo to 90 degrees
}

void loop() {
// No action is needed in the loop for this task
}

Now you can simply place the lower arm with the horn on the servo and secure it, in such a way that the arm is pointing upwards. Like shown on the image.

Elbow Joint

WhatsApp Image 2024-12-22 at 13.06.53 (1).jpeg

The elbow joint is also straightforward, and the installation is almost identical to the shoulder joint.

Secure the servo, grab the upper arm, put in the horn(with the screw), and secure it.

Set the elbow joint to 90 degrees, by changing

myServo.attach(9);

To

myServo.attach(10);

Attach the arm, and you're good to go!

The result should look similar to the image

Gripper

WhatsApp Image 2024-12-22 at 13.06.53 (2).jpeg
WhatsApp Image 2024-12-22 at 13.06.53 (3).jpeg

Now the gripper is probably the most complicated step.

First of all, we are going to need to attach the gripper servo holder and gripper holder.

The servo holder will go onto the right side, you can use two m3x12mm bolts to attach it.

You can also attach the gripper holder onto the left side using the same hardware.

Then simply attach the servo to the holder and secure it with two M2x7mm screws.

Then place a servo horn(with a screw) into the right side of the gripper, and secure it with the little servo screw.

Then you can put it on your servo.

Now for the last thing we need to do is put the left side of the gripper on the holder, in such a way that when the gripper servo(pin 11) is set to 180, the gripper is closed.



Uploading the Code to the Arduino

Now we are going to need to upload the code I've written to the Arduino, we can do that the same way we have done it earlier:

  1. Connect the Arduino to your computer
  2. Install & open Arduino IDE
  3. Paste the code from below into the IDE
  4. Select the right port and board(Arduino Uno)
  5. Hit upload!

This is the code:

// CONFIG //
const int STEPPER_IN1 = 2;
const int STEPPER_IN2 = 3;
const int STEPPER_IN3 = 4;
const int STEPPER_IN4 = 5;
const int STEPS_PER_REVOLUTION = 2048;

const int SHOULDER_IN = 9;
const int ELBOW_IN = 10;
const int GRIPPER_IN = 11;
// INCLUDE //
#include <Arduino.h>
#include <Servo.h>
#include <Stepper.h>
// VARIABLES //
Servo shoulder;
Servo elbow;
Servo gripper;
Stepper base(STEPS_PER_REVOLUTION, STEPPER_IN1, STEPPER_IN3, STEPPER_IN2, STEPPER_IN4);
int joint;
int amount;
int currentShoulder;
int currentElbow;
int currentGripper;
int currentBase;
// FUNCTIONS //
void moveServos(int shoulderAngle, int elbowAngle, int gripperAngle);
void moveStepper(Stepper stepper, int degrees);
void calibrate();

// MAIN CODE //

void setup(){
//Start Serial
Serial.begin(9600);
//Set up motors
shoulder.attach(SHOULDER_IN);
elbow.attach(ELBOW_IN);
gripper.attach(GRIPPER_IN);
base.setSpeed(10); // lower than 15 is recommended, above 25 will break the motor
currentBase = 0;
//Set servos into Zero position
shoulder.write(97);
currentShoulder = 97;
elbow.write(90);
currentElbow = 90;
gripper.write(160);
currentGripper = 160;

//calibrate();
// delay(1000);
// moveServoSmooth(elbow, 0, 45, 5);
// moveStepper(base, -0.25f);
// delay(1000);
// moveServoSmooth(shoulder, 90, 0, 5);
// moveServoSmooth(elbow, 45, 0, 5);
}

void loop() {
while (Serial.available() > 0) {Serial.read();} // Clear
while (Serial.available() == 0) {} // Wait for data
String consoleInput = Serial.readString(); //Read data
consoleInput.trim(); //Trim, to remove any EOL characters or similiar

if(consoleInput.length() == 12){
int baseInput = consoleInput.substring(0,3).toInt();
int shoulderInput = consoleInput.substring(3,6).toInt();
int elbowInput = consoleInput.substring(6,9).toInt();
int gripperInput = consoleInput.substring(9,12).toInt();

moveStepper(base, baseInput-360);
moveServos(shoulderInput,elbowInput,gripperInput);
Serial.print("recieved: '");
Serial.print(consoleInput);
Serial.println("'");
Serial.println("done");
}
else{
Serial.println("Wrong input!");
}
}

// FUNCTIONS //
void moveServos(int shoulderAngle, int elbowAngle, int gripperAngle) {
//17ms
bool finishedServos[] = {false,false,false};

while(true){
if(currentShoulder != shoulderAngle){
if (currentShoulder < shoulderAngle){
shoulder.write(currentShoulder+1);
currentShoulder++;
delay(17);
}
if (currentShoulder > shoulderAngle){
shoulder.write(currentShoulder-1);
currentShoulder--;
delay(17);
}
}
else{
finishedServos[0] = true;
}


if(currentElbow != elbowAngle){
if (currentElbow < elbowAngle){
elbow.write(currentElbow+1);
currentElbow++;
delay(17);
}
if (currentElbow > elbowAngle){
elbow.write(currentElbow-1);
currentElbow--;
delay(17);
}
}
else{
finishedServos[1] = true;
}


if(currentGripper != gripperAngle){
if (currentGripper < gripperAngle){
gripper.write(currentGripper+1);
currentGripper++;
delay(17);
}
if (currentGripper > gripperAngle){
gripper.write(currentGripper-1);
currentGripper--;
delay(17);
}
}
else{
finishedServos[2] = true;
}

if(finishedServos[0] && finishedServos[1] && finishedServos[2]){
break;
}
}
finishedServos[0] = false;
finishedServos[1] = false;
finishedServos[2] = false;
}

void moveStepper(Stepper stepper, int degrees){
stepper.step((2048/360)*(currentBase-degrees));
currentBase = degrees;
digitalWrite(STEPPER_IN1, LOW);
digitalWrite(STEPPER_IN2, LOW);
digitalWrite(STEPPER_IN3, LOW);
digitalWrite(STEPPER_IN4, LOW);
}

void calibrate(){
// Serial.println("The servos in 5s will be set to a pole like position");
// delay(5000);
// moveServoSmooth(shoulder, 0, 90, 5);
// moveServoSmooth(elbow, 0, 90, 5);
// Serial.println("If the servos aren't in a pole like position, please adjust them, you have 10s");
// delay(10000);
// Serial.println("Now please set the stepper motor into such a positon, that the arm is facing toward the hole that the wires go into. You have 10s");
// moveServoSmooth(elbow, 90, 0, 5);
// delay(10000);
// Serial.println("Calibration complete!");
}



After uploading the code when you open the Serial Monitor, set it to 9600 baud, and send for example '123', the Arduino (after 200-300ms) should respond "Wrong input!". That's because it only accepts inputs that are 12 characters long. I will explain why in the next step.

The Protocol

So the code was written so that you can very easily control the robot over Serial.

First of all, all the messages have to be 12 characters long, why?

It's because the robot has four joints, and you can send a 3-character long position to each one, so 3x4=12.

But what exactly do these numbers mean?

The first three are for the base, keep in mind that the position the base will go to is what you input minus 360.

The second three are for the shoulder, it will simply go to the position you input.

The third three are for the elbow, and work the same as the shoulder joint.

The last three are for the gripper, they work the same as the elbow, but I wouldn't recommend going below 90 degrees.


Why this protocol?

Well, first of all, it's very simple, second of all doing it this way allows you to add your own functionality, like you could for example add an esp32 to make it wi-fi controlled.

The PC Program

So just sending strings of 12 numbers isn't so nice and intuitive is it? That's why I decided to also make a PC program to control it all.

Downloading and (Optionally) compiling

First of all, you will need to figure out which port your Arduino is using A.K.A. the same port you used to upload the code.

Then download the Robo Control.rar file, extract it, and to use it just got to the bin folder and run the program with your port.

The files are located here: Google Drive


If there isn't a pre-compiled .exe for your port you will need to compile it yourself. Luckily for you I made a CMake file.

If you don't know how to compile with CMake, this website should help.


How Do I Use It?

asd asd.png

1. Sets the robot to its Pole-like position.

2. Set all the motors to zero.

3. Sends the values from 11,12,13,14, to the robot.

4. Plays the program currently loaded

5. Save the current program

6. Load the program with your input name in the top bar. It is also used to create new files.

7. Take in the name of your program.

8. Remove the last position from the loaded file.

9 . Adds the current values from 11,12,13,14 to the current file.

1 0.Displays roughly what you are about to send to the arm

11. Takes in the value for the gripper,and also shows it

12. Takes in the value for the elbow

13. Takes in the value for the shoulder

14. Takes in the value for the base

15. Displays roughly how the base is oriented

Ending

Thanks for staying with me, also special thanks to my mom for motivating me and supplying me snacks.

Please let me know what you think about this in the comments, it has been my first real big scale project.

Also if you need help, just message me on discord: xtagz_69