How to send values to Arduino from python?

I am trying to send angle values which gets from python script, and send this values to arduino for rotate servo motors. I used tutorials on youtube about mediapipe hand tracking. I am getting values of index finger’s angle from 0 degrees to 180. But how am i going to send these values to servo motor and rotate it? Here is my code below:

import cv2
import numpy as np
import HandTrackingModule as htm
import math

import Utilities
from Utilities import connectToRobot

portNo = "COM5"
wCam, hCam = 640, 480

cap = cv2.VideoCapture(1)
cap.set(3, wCam)
cap.set(4, hCam)
Utilities.connectToRobot(portNo)

detector = htm.handDetector(detectionCon=0.75)

while True:
    success, img = cap.read()

    img = detector.findHands(img)
    lmList = detector.findPosition(img, draw=False)
    if len(lmList) != 0:
        #print(lmList[8], lmList[0])

        x1, y1 = lmList[8][1], lmList[8][2]
        x2, y2 = lmList[0][1], lmList[0][2]

        cv2.circle(img, (x1, y1), 10, (255, 0, 255), cv2.FILLED)
        cv2.circle(img, (x2, y2), 10, (255, 0, 255), cv2.FILLED)
        cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3)

        length = math.hypot(x2 - x1, y2 - y1)
        #print(length)

        # Index finger range 330 - 140
        # servo motor angle range 180 - 0

        angle = np.interp(length, [140, 300], [0, 180])
        print(int(angle))

    cv2.imshow("Image", img)

    key = cv2.waitKey(1)
    if key & 0xff == ord('q'):
        break



Sorry for my bad english. Thanks for advice.

These are the angles. You see maximum angle is 180 degrees and minimum is 0 degrees. I did not take 0 degrees because this is just print(int(angle)) output for you to understand what i am trying to do.
hand

this is my hand btw. This photo show what code does. I drew purple line for calculate angle.

65
78
77
80
92
67
79
74
91
98
126
144
144
175
180
180
180


I am new here so if i opened this question in wrong topic sorry about that.

presumably you’re asking how to send information to the arduino and it is up to the arduino code to interpret it to drive the servo.

any PC application can communicate with the arduino over the same com port (e.g. com4) that the IDE uses once the IDE is closed (or closes its use of the port ??)

it is not clear to me what your python-function

Utilities.connectToRobot(portNo)

is doing. It seems to open a com-port.
But then the utilities-object isn't used anymore.

If this command "Utilities.connectToRobot(portNo)" does open a comport
you would have to send your number over this comport.

You have to check which comport your arduino is using when you have the arduino connected to the arduino-IDE and your python-code must use the same comport.

As long as you have the serial-monitor of the arduino-IDE opened the Arduino-IDE is occupying this comport. This means your python-script can only access the same comport if you close the serial-monitor of the arduino-IDE.

It would be a good idea if you give us a description how much you know about coding in general and com-ports in special. If answers are really helpful depends on if the answers are adapted to your knowledge-level.

best regards Stefan

My tutorial on Arduino to Arduino/PC
has Arduino and python code to send in both directions.
Also see a link to an alternative code solution at the top of that tutorial.

Yes i want to send values arduino.

I know how to check arduino port. In this code some modules i added and because of them if you run the code it will run. Utilities part is check the arduino port if it is connect then it says robot connected, this is just for avoiding error message.

Thank you, i am going to check it.

Hi mehmet,

Hello mehmet

I do not understand what you wrote

I recommend that you write your questions in your native language in your postings and then have them translated with google translate. His grammar will not be perfect. But it will be easy to understand.

I wrote this text in German and had it translated into google-Turkish.

Then I took the Turkish text and had the Google-translate translated from Turkish to English for the second time.
best regards Stefan

Sorry for that.

If you run the code I wrote here, it won’t work, because of the modules I added later. The reason I add the code is for you to understand what I’m trying to do. When my code runs, the value goes up to 180 degrees when I open my index finger fully, and it goes down to 0 degrees when I close my index finger. As you can see in the code, this process is in a loop and the values ​​are constantly changing. I want the servo motor to run at the same time as my finger. I haven’t written the arduino code yet. Python code only finds values. How can I do that?

Hope this works. Thanks

you are not very specific with your question "How can I do that"
How can I do what?

I wil not speculate about what you are meaning with "How can I do that"
You should write your postings in a style that you write cleary and with rich details what you want to do. It is annyoung to have asking back what you exactly mean.

Taking time to write a detailed posting needs a little more time but will save a lot of time throuog avoiding asking back

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.