0

I'm following this tutorial: (http://www.seeedstudio.com/recipe/index.php?controller=recipe&action=show&recipe_id=166)

I have the Raspberry Pi connected to the Arduino via USB

When I run python in terminal, the commands work as expected and the arduino flashes as directed.

However when I put the commands in a python script and execute the .py file, regardless of the number I pass, the LED on pin 13 flashes a 3 or 4 times very quickly, then pauses and flashes slowly once more.

I can't understand why it isn't running as it is supposed to. There is a clear difference in how the arduino interprets serial messages sent via terminal python (>>>) and executing arduino.py.

When I run dmesg, I get:

pi@raspberrypi ~ $ dmesg | grep tty

[    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x4c5954ea smsc95xx.macaddr=B8:27:EB:59:54:EA bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
[    0.002073] console [tty1] enabled
[    0.195158] 3f201000.uart: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev2
[    6.986035] usb 1-1.3: ch341-uart converter now attached to ttyUSB0
[  271.283091] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[  273.926013] usb 1-1.5: ch341-uart converter now attached to ttyUSB0
[  455.883149] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[  530.727358] usb 1-1.3: ch341-uart converter now attached to ttyUSB0

When I run python --version in terminal, I get 2.7.3

When I print sys.version in the script, it shows [GCC 4.6.3]

arduino.py:

#!/usr/bin/env python
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.write("3")
8
  • If you follow the tutorial, the pi part says ser.write('3') so it flashes 3 times. If you changed it to ser.write('9') it would flash 9 times. What are you expecting it to do? Commented Sep 21, 2015 at 5:54
  • It doesn't though. It always flashes 3 times quickly waits a second then flashes once more. Whether I put ser.write("3") or ser.write("30") Commented Sep 22, 2015 at 16:52
  • Look at the code: what it does is subtract '0' from the character sent in. It only accepts one character. So if you sent in anything between '1' and '9', it will flash that number of times. If you know the ASCII set, then you'll know that the sequence following 9 is :;<=>?. So if you sent in '?', you would get 15 flashes. If you want it to interpret "30" as 30, then the code in Loop needs changing. Commented Sep 22, 2015 at 18:11
  • Update: if I load python in terminal, it works as expected. If I put the same commands in a arduino.py and run 'sudo python arduino.py', I get the error above Commented Sep 22, 2015 at 23:12
  • Can you post arduino.py . Does it have #!/usr/bin/python as the first line? Also, you don't need to sudo to run the script - sudo chmod 666 /dev/ttyAMA0 . After that you can just run arduino.py without going into sudo. Commented Sep 23, 2015 at 4:59

1 Answer 1

0

Solved!

It seems that opening the serial connection was causing my Arduino to reset... I put a time.sleep(3) after opening the connection.

My code is:

import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600)
time.sleep(3)
ser.write('2')

Very strange that this issue happens in an executable, but not in console.

Sign up to request clarification or add additional context in comments.

1 Comment

Possibly because the time taken to write the ser= and the ser.write is appx 3 seconds?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.