Video není dostupné.
Omlouváme se.

How to Send Commands to an Arduino from a Python Script

Sdílet
Vložit
  • čas přidán 29. 01. 2022
  • These tutorials assume some basic understanding of Python syntax.
    Visit tinkerassist.com and Subscribe to the Tinker Assist Newsletter - keep up to date on what we are working on, and get your fix of programming, science, and engineering content straight to your inbox!

Komentáře • 32

  • @hansyboi
    @hansyboi Před rokem +5

    I've looked at dozens of arduino Python tutorials and none of them have worked so it was such a relief to see mine working after I followed yours. Thank you so much!

  • @steelcock
    @steelcock Před rokem +20

    import serial.tools.list_ports
    ports = serial.tools.list_ports.comports()
    serial_inst = serial.Serial()
    ports_list = []
    for port in ports:
    ports_list.append(str(port))
    print(str(port))
    val: str = input('Select Port: COM')
    for i in range(len(ports_list)):
    if ports_list[i].startswith(f'COM{val}'):
    port_var = f'COM{val}'
    print(port_var)
    serial_inst.baudrate = 9600
    serial_inst.port = port_var
    serial_inst.open()
    while True:
    command: str = input('Arduino Command: (ON/OFF): ').upper()
    print(command)
    serial_inst.write(command.encode('utf-8'))
    if command == 'EXIT':
    exit(0)
    #define LED_pin 9
    #define LED_error_pin 8
    void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    pinMode(LED_pin, OUTPUT);
    pinMode(LED_error_pin, OUTPUT);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    //String msg = "STATIC";
    if (Serial.available() > 0) {
    String msg = Serial.readString();
    if (msg == "ON") {
    digitalWrite(LED_pin, HIGH);
    }
    else if (msg == "OFF") {
    digitalWrite(LED_pin, LOW);
    }
    else {
    digitalWrite(LED_error_pin, HIGH);
    delay(100);
    digitalWrite(LED_error_pin, LOW);
    }
    }
    }

  • @robinreynaud9710
    @robinreynaud9710 Před 2 lety +3

    would be great to add your code ! thx

  • @elyakimlev
    @elyakimlev Před rokem +1

    Cool. I was wondering how I could combine computer vision with an Arduino controlled robotic arm. I thought I'd have to use Raspberry Pi, which isn't capable enough for real-time computer vision. Now I'm thinking of using this method to do the resource intensive action of computer vision on my PC and then just send the coordinates of the detected objects to the Arduino for it to decide how to move the robotic arm.

    • @TinkerAssist_
      @TinkerAssist_  Před rokem

      Yeah that’s a good idea! Glad this example is being used for such awesome use cases :)

    • @abdrazaqM.K
      @abdrazaqM.K Před rokem

      Your project is same as mine, i want to communicate with a robotic arm and using meArm library and only send x,y,z to arduino ino from python script

    • @_ronlarson_jimmywaynewells6384
      @_ronlarson_jimmywaynewells6384 Před 11 měsíci

      Hello I'm doing a similar project. Can I email you?

    • @godwinemeka904
      @godwinemeka904 Před 7 měsíci

      ​@@TinkerAssist_ similar project, I have been looking for a way to communicate with my Arduino using computer vision precisely using opencv, I think this will be a lot of help

  • @neknek1613
    @neknek1613 Před 7 měsíci

    hello, i tried to practice your code, but it seems I have lacking on my side,
    it shows some comments regarding error at line#15. what does this mean?
    if portsList[x].startswitch("COM" + str(val)):
    ^^^^^^^^^^^^^^^^^^^^^^^^
    AttributeError: 'str' object has no attribute 'startswitch'. Did you mean: 'startswith'?

    • @TinkerAssist_
      @TinkerAssist_  Před 7 měsíci

      @neknek1613 If you look at the error message you’ll see that you misspelled the method “startswith”

  • @superpayaseria
    @superpayaseria Před 4 měsíci

    So I'm trying to write 1,000's of commands for a humanoid robot project. I started coding in python. But as far as I can tell with the last few lessons Arduino ide only allows like a few commands at a time. So is it possible to write super complex code in python and use an arduino for the main processing unit for the entire robot??? Or will the arduino ide not handle it?

  • @benbenameur8429
    @benbenameur8429 Před rokem

    Hello
    I am looking for the equivalent of : arduino.write("X="+str(200)+'
    ') in matlab .

  • @vishal.s
    @vishal.s Před 9 měsíci

    please let know if it posssible to use this in esp32 cam module also

  • @hieule-ch3yb
    @hieule-ch3yb Před rokem

    why i can't connect between python and arduino in same com port?

  • @anmaleno6815
    @anmaleno6815 Před 9 měsíci

    how can we remove the delay between sending the message and lighting the LED? THANKS

    • @TinkerAssist_
      @TinkerAssist_  Před 8 měsíci

      A higher baud rate may help with this. If the delay is significant though, there may be something else causing this.

  • @nanakoii
    @nanakoii Před 10 měsíci

    How to fix this issue?? I'm using windows and I was run the file as administrator and still not work
    Traceback (most recent call last):
    File "D:\MIDI\test.py", line 21, in
    serialInst.open()
    File "C:\Users\NHQEN\AppData\Local\Programs\Python\Python311\Lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
    serial.serialutil.SerialException: could not open port 'COM7': PermissionError(13, 'Access is denied.', None, 5)

    • @hungrybulldog2725
      @hungrybulldog2725 Před 6 měsíci +1

      u gotta close the serial monitor on Arduino IDE, any monitoring system will avoid any other access to the COM

    • @stella4816
      @stella4816 Před 5 měsíci

      Have you got the solution..??

    • @hungrybulldog2725
      @hungrybulldog2725 Před 5 měsíci

      @@stella4816 u talking to me or him ? 😅

  • @ningguangleaks2001
    @ningguangleaks2001 Před 6 měsíci

    could you do this using esp32?

  • @nityanandadas5575
    @nityanandadas5575 Před rokem

    Nice video.
    I am automating a power supply by Python with SCPI Commands.
    At the same time I need to use Arduino to switch on and off control by Python.
    Can I synchronise the two devices - the power supply and the Arduino or, they are automatically synchronised with the PC time?
    Regards

  • @XanthiumIndustries
    @XanthiumIndustries Před rokem

    Nice work

  • @user-dt6zq7oi9z
    @user-dt6zq7oi9z Před rokem

    Very Nice, Thanks alot

  • @12linyih
    @12linyih Před 2 lety

    there seems to be lagging when input command from pc till the LED is on, is there a way to improve it?

    • @davidmikulic475
      @davidmikulic475 Před 2 lety

      Maybe by increasing the Baudrate, have not tried this tho.

    • @jakestimson3451
      @jakestimson3451 Před rokem

      Arduino is only sampling serial input from the python script every 1/10 of a second. This could be it

  • @francescocassini2212
    @francescocassini2212 Před rokem

    Your code is fantastic, but if I have to send a NUMBER (also as string) to control a servo motor, how can I change the .ino files to get a string ("0" or"180") and convert to an INT in .ino files? I have seen other videos that use FIRMATA, but the servo has always a bad beahaviour and many delays to reach the position. So I believe that it's better realize a system as yours. Thanks in advance if you can help me

    • @Ayyubzee
      @Ayyubzee Před rokem

      czcams.com/video/GRBQRneYT0s/video.html

    • @TinkerAssist_
      @TinkerAssist_  Před 8 měsíci +1

      Arduino’s “toInt” method does this
      docs.arduino.cc/built-in-examples/strings/StringToInt