danduino
Posts: 2
Joined: Sun Mar 17, 2013 7:05 pm

Python and Arduino with Pyserial

Sun Mar 17, 2013 7:12 pm

I wrote a basic arduino sketch. If it receives a '1' over the serial port, it turns the LED on. If it receives a '0' over the serial port, it turns the LED off. This works in the arduino IDE using the serial monitor. It also works in the IDLE python shell; however, if I save the code below in a .py file and try to run it in the terminal, it does not work. The LED just flickers really fast just for a second. What am I doing wrong?

Python code:

Code: Select all

import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write('1')
Terminal command:

Code: Select all

python somefile.py

User avatar
winkleink
Forum Moderator
Forum Moderator
Posts: 288
Joined: Mon Nov 07, 2011 9:12 am
Location: UK
Contact: Website

Re: Python and Arduino with Pyserial

Mon Mar 18, 2013 3:42 pm

If the LED is flickering then the chances are the Raspberry Pi is sending the data to the Arduino and that the problem is more likely to be at the Arduino end.

It might be the Arduino reset issue: Some useful reading
[u]http://playground.arduino.cc/Main/Di ... ection[/u]
[u]http://arduino.cc/forum/index.php/to ... 0.html[/u]
I know I had this when I tried controlling my Arduino over USB serial and it just sorted itself out.
The best alternative is to connect to the serial lines 0 and 1 on the Arduino from the Raspberry Pi serial connection the GPIO. This way the reset would not happen.
Twitter:Winkleink
Google+: Winkleink

danduino
Posts: 2
Joined: Sun Mar 17, 2013 7:05 pm

Re: Python and Arduino with Pyserial

Mon Mar 18, 2013 4:15 pm

I'll try connecting to the serial pins later tonight. I ended up just installing the Arduino IDE on the RPi itself and that works like a charm when running Arduino sketches over USB. Regardless, I would still like to see if your suggestion fixes the problem I was having.

guruzinho
Posts: 2
Joined: Tue Mar 19, 2013 1:35 am

Re: Python and Arduino with Pyserial

Tue Mar 19, 2013 1:38 am

Try in the serial instance stopbits attribute:

ser.stopbits=20

guruzinho
Posts: 2
Joined: Tue Mar 19, 2013 1:35 am

Re: Python and Arduino with Pyserial

Tue Mar 19, 2013 1:41 am

sorry ser.stopbits=2

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: Python and Arduino with Pyserial

Tue Mar 19, 2013 6:08 am

How to send data from PC to RPi using python code through the UART?Please help me on this.

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: Python and Arduino with Pyserial

Tue Mar 19, 2013 9:14 am

please help me on this.please solve my problems.. :( :(

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: Python and Arduino with Pyserial

Wed Mar 20, 2013 2:06 am

here the coding..

Code: Select all

import serial
usbport = '/COM47'
ser = serial.Serial(usbport, 9600, timeout=1)
still i cant read data from my PC.i want read the data from my PC and display it on raspberry Pi..please help me.. :( :( :( :( :( :(

Return to “Python”