> For the complete documentation index, see [llms.txt](https://doc.eimtechnology.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.eimtechnology.com/kt-series-project-based-learning/ai-with-hardware/robotic-hand.md).

# Robotic Hand

<table data-view="cards"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><strong>The Artificial Intelligence Learning (AI) + Machine Learning Kit</strong></td><td><a href="https://www.eimtechnology.com/collections/all-products/products/learn-convolutional-neural-networks-computer-vision-opencv-artificial-intelligence-learning-ai-and-machine-learning-kit?variant=50057188278591">Available Now</a></td><td></td><td><a href="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2FN9oSDFjrdMP1cSPaPx5H%2Fai-preivew.png?alt=media&amp;token=03e8de6f-04ec-43ed-a185-702092ad7bce">ai-preivew.png</a></td></tr><tr><td><strong>Instruction and resources</strong></td><td><a href="https://github.com/eimtechnology/AI-KickStarter">GitHub </a></td><td><a href="https://discord.com/invite/urh8hW2YFa">Discord</a></td><td><a href="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2F5rAUpcj5DiIvF80igdxb%2FAI_banner_1600_x_900_px.webp?alt=media&amp;token=d7011686-01b3-43d4-b7d3-629cda93bf43">AI_banner_1600_x_900_px.webp</a></td></tr><tr><td><strong>3D Printable Model</strong></td><td><a href="https://eimtechnology.blob.core.windows.net/3d-models/Hand_Model.zip">Download</a></td><td><a href="#assembly-the-hand">Instruction</a></td><td><a href="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2FRsFrswrlQV7UhsH4EPFe%2Fmodel.jpg?alt=media&amp;token=29dca034-ad21-4636-b36a-93a95c27d6b9">model.jpg</a></td></tr></tbody></table>

## Project Overview

In this project, we integrate multiple servo motors with a microcontroller to replicate human hand movements. The primary goal is to control a 3D-printed robotic hand using intuitive hand gestures. These gestures are captured, processed, and translated by the microcontroller into precise commands for the servo motors, enabling them to mimic the natural motion of human hand joints. The system demonstrates the seamless interaction between gesture recognition and robotic actuation, paving the way for innovative applications in robotics and human-machine interaction.

<figure><img src="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2FV7BHYBHhufi6aIRzBOOo%2FFigure%206.2.6.jpg?alt=media&amp;token=b33db951-e4c7-42cc-8076-6ee3d00d264f" alt="" width="563"><figcaption></figcaption></figure>

## Hardware

The following components will be prepared in the kit for this project:

* A breadboard
* A STEPico microcontroller
* Multiple jumper wires
* 5 SG90 servos
* A USB cable
* 3d printed hand model kit (optional)

<figure><img src="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2F6WjzYNr3GM5GrzhSFE9W%2FFigure%206.0.2.jpg?alt=media&amp;token=b4670dfa-d97e-4f08-8f46-75f6d71075a4" alt=""><figcaption><p>Breadboard, STEPico, Servos and accessories, jumper wires, and USB cable</p></figcaption></figure>

## Tutorial

In this project, we will further adjust serial communication to enable it to transmit more data in addition to character and binary representation. At the same time, a new type of hardware, servo, will be added to the system. The servo is also controlled by a microcontroller, which outputs hand-related information through the computer. The microcontroller sends a signal to make the servo operate to a specified angle, thereby simulating the movement of the hand joints.

<figure><img src="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2F9mUoxO7yAW9sZjQUJeKp%2F03.jpg?alt=media&amp;token=3a41b736-ef9f-4d02-9124-6934b06e090a" alt=""><figcaption></figcaption></figure>

## **The Learning Part**

### Hardware Setup

In this project, we will control five servos to simulate hand gestures, so we need to map the servo signal lines to the corresponding GPIO ports one by one. Here, we choose to use GP16 to GP20 in order from thumb to little finger.

<div><figure><img src="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2FLMKqGPdvX7hrxE1Mxhmk%2FFigure%206.2.4-1.png?alt=media&amp;token=d8d5d65b-97b8-477d-a096-f5ad49959a9d" alt="" width="563"><figcaption><p>Signal transmission</p></figcaption></figure> <figure><img src="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2Fjw5n94eshJ4nyMVoZR2b%2FFigure%206.2.4-2.png?alt=media&amp;token=42faf502-b465-45af-a9a9-dec453d11b61" alt="" width="563"><figcaption><p>Power Supply</p></figcaption></figure></div>

To facilitate the reception of serial communication data and streamline upcoming calculations, we import the following modules:

```python
import time
import uselect
import math
from sys import stdin
from machine import PWM, Pin
```

The PWM (Pulse Width Modulation) module in MicroPython is an integral part of its control functions, providing users with the ability to manage devices and signals with precision. The importance of this module lies in its comprehensive capabilities, enabling various applications such as motor control, LED brightness adjustments, and more. By incorporating PWM, there is no need for the installation of additional modules, streamlining the development process and optimizing resource utilization.&#x20;

### Servo Control Setup

To ensure proper coordination between finger joints and servos, we will assign each GPIO port to a specific finger.

```python
servo_thumb = Pin(20)
servo_index = Pin(19)
servo_middle = Pin(18)
servo_ring = Pin(17)
servo_pinky = Pin(16)
```

Define a PWM variable for each servo as follows: `servo_pwm5 = PWM(servo_pinky)` through `servo_pwm1 = PWM(servo_thumb)`. The `PWM()` function creates a PWM object, enabling PWM signal generation on microcontroller pins.

### Assembly the Hand

The hand model contains six parts: five fingers and one palm with servo connectors on it.

<figure><img src="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2FRgewcJ5lDO08O6hP3iox%2F01.png?alt=media&amp;token=f4425231-78fd-4b82-9e4f-d726e9d78f77" alt=""><figcaption></figcaption></figure>

This is a left-handed model, where the index finger and the ring finger shares the same 3d model. &#x20;

To assembly the hand, first connect the finger base and the servo arm with screws, the screws could be found in servo's component beg

<figure><img src="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2FPX8o8kFVlJUXDvw2tZTN%2F02.png?alt=media&amp;token=c45fc245-a0ea-46cd-a749-a263ee2cf6dc" alt=""><figcaption></figcaption></figure>

After connecting the finger and the servo arm, connect the servo arm to the servo.

<figure><img src="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2FxSXuEze4aNdFIaeLXIG0%2F03.png?alt=media&amp;token=0ecbb5d5-886a-4560-be7a-ff88fcffe043" alt=""><figcaption></figcaption></figure>

After connecting all the servo arms and servos, place them onto the connectors on the palm. Be sure to arrange the signal and power wires properly.

### Joint Angle Recognition

The first function, `finger_angle`, calculates the bending angle of a finger based on its metacarpophalangeal (MCP), proximal interphalangeal (PIP), and distal interphalangeal (DIP) joints. It computes the angle by evaluating the positions of the corresponding landmarks and returns it as a numerical value, providing a precise measure of finger flexion.

```python
def finger_angel(landmarks, mcp_index, pip_index, dip_index):
	# Determine if a finger is bent (based on angle)
		angle = calculate_angle(landmarks[mcp_index], landmarks[pip_index], landmarks[dip_index])
		return angle

```

The second function, `count_fingers_states`, calculates and stores the bending angles of all five fingers, from the thumb to the pinky, by analyzing the hand’s landmarks. It calls `finger_angle` for each finger, determining the angles of the relevant joints. The result is a list of five floating-point numbers, each representing the current bending angle of one finger joint. This list can be used to track finger states in real-time, enabling more advanced gesture recognition and interaction.

```python
def count_fingers_states(hand_landmarks):
    landmarks = hand_landmarks.landmark
    finger_states = [
        finger_angle(landmarks, 1, 2, 3),  # Thumb
        finger_angle(landmarks, 0, 5, 6),  # Index finger
        finger_angle(landmarks, 0, 9, 10),  # Middle finger
        finger_angle(landmarks, 0, 13, 14),  # Ring finger
        finger_angle(landmarks, 0, 17, 18)  # Pinky finger
    ]
    return finger_states
```

These functions allow for real-time tracking of finger joint angles, which is useful for gesture recognition and other interactive applications.

After you complete the project, the result looks like the following:

<figure><img src="https://2117864350-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy5TN04jkEM9s96yIKLT%2Fuploads%2FTkJGmKaVXY5H1ORwGqa6%2F1000012798.jpg?alt=media&amp;token=3c5eb9ab-6946-4a3c-8bd1-8b9ef6a12449" alt=""><figcaption></figcaption></figure>

### Video Walkthrough

{% embed url="<https://youtu.be/g7rBGOhQ94A>" %}
