1

I am trying to send commands via serial from Python to Arduino with the code below.

If I save this code in a dedicated python file "Stepper.py" (Python2.7 on Mac OSX) , the code runs successfully but nothing happens (the code does not trigger a motor connected to Arduino).

If I start terminal and run the same code from the interactive shell it works as expected.

How can I run it via a dedicated file ?

import serial 
import time

ser = serial.Serial(port="/dev/cu.usbmodem144231", baudrate=9600, timeout = 0)
ser.write("200+")
time.sleep(1)
print ser.read()
2
  • 2
    You need to wait after opening the serial port for the board to finish resetting and the bootloader to run and exit before you can send any commands.
    – Majenko
    Commented Feb 18, 2019 at 14:08
  • You are the man !!! Awesome works. Thank you so much
    – digit
    Commented Feb 18, 2019 at 14:12

1 Answer 1

2

Majenkos comment (above) solved the problem:

"You need to wait after opening the serial port for the board to finish resetting and the bootloader to run and exit before you can send any commands."

import serial 
import time

ser = serial.Serial(port="/dev/cu.usbmodem144231", baudrate=9600, timeout = 0)
time.sleep(1)
ser.write("200+")
print ser.read()

Not the answer you're looking for? Browse other questions tagged or ask your own question.