Almucantarat
Posts: 2
Joined: Sat May 31, 2014 1:48 pm

python serial to arduino [SOLVED]

Thu Jan 22, 2015 10:58 am

Hi to you all,
I have a problem sending commands to arduino with python 2 or 3.
I am using this script to blink a led a defined number of times on Arduino

Code: Select all

import serial
ser=serial.Serial("/dev/ttyACM0",9600)
num="3"
ser.write(bytes(num.encode("ascii")))
On the arduino there is this code compiled

Code: Select all

const int ledPin = 12;

void setup(){
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  if (Serial.available())  {
     light(Serial.read() - '0');
  }
  delay(500);
}

void light(int n){
  for (int i = 0; i < n; i++)  {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }
}
taken from here http://blog.oscarliang.net/connect-rasp ... usb-cable/
the thing i don't understand is:
if i run the code from the interactive shell (python 2 or 3), the led on Arduino blinks. If i save the python script to a file blink.py and run it with python3 blink.py (or python2 blink.py) the the led on Arduino does not light up. Why oh Why?


Thank you
Last edited by Almucantarat on Fri Jan 23, 2015 1:19 pm, edited 1 time in total.

User avatar
dexterindustries
Posts: 47
Joined: Thu Mar 07, 2013 3:51 am
Location: Somewhere
Contact: Website

Re: python serial to arduino

Fri Jan 23, 2015 1:08 pm

Have you seen some of the examples we have up to communicate with an Arduino here: https://www.dexterindustries.com/Ardube ... pberry-pi/

There are some examples for communicating between the Raspberry Pi and Arduino. One is even used to control an LED: http://www.dexterindustries.com/Arduber ... -pi/blink/

Hopefully this helps you get going with the Arduino and Raspberry Pi!
John Cole
Dexter Industries

Code: Select all

http://www.dexterindustries.com

Almucantarat
Posts: 2
Joined: Sat May 31, 2014 1:48 pm

Re: python serial to arduino

Fri Jan 23, 2015 1:18 pm

Thank you, i have figured out the solution.
The problem is that Arduino needs time to open the port.
If the code is issued via interactive shell, Arduino has plenty of time after you call the serial.Serial(...) command and are writing the next line of code.
With the file python is faster than Arduino, and all the commands are lost.
Thus you must insert a
time.sleep(..) after the serial.Serial(...) command .

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: python serial to arduino

Sat Jan 24, 2015 10:49 am

Almucantarat wrote:The problem is that Arduino needs time to open the port.
this is depending on the Arduino type ( its the UNO... what is resetting when you open
the terminal / serial port ) more on that reset and the DTR pls see http://playground.arduino.cc/Main/Disab ... Connection
From a PC with HTerm i tested DTR and could connect to Arduino UNO without reset!
but i would not know if the serial init of python could be modified ( not to reset UNO ).
and testing Arduino diagnosis... i see something i would not have expected:
when you close the line ( like the Arduino IDE terminal ) the UNO resets again!
That you might see by RPI python serial too.

Return to “Python”