1

Possible Duplicate:
Pyserial problem with Arduino - works with the Python shell but not in a program

So I just got myself an Arduino Uno, and now I'm trying to communicate with it using USB. I've got it working with the Serial Monitor provided by the Arduino IDE, and now I'm testing it with Python.

The program should make a LED on the board blink when it read data from the USB.

The way I do it is:

#!/usr/bin/env python
import serial

ser = serial.Serial("/dev/ttyACM0", 9600)
ser.write("something")

This doesn't work, but doing each step in the interactive shell works:

>>> import serial
>>> ser = serial.Serial("/dev/ttyACM0", 9600)
>>> ser.write("something")

I've tried using a timeout in the script before closing, but that didn't help. What am I doing wrong?

1
  • Ahh, awesome. Ive googled around for 1-2 hours, but haven't found this post, yet. Thx! Commented Dec 13, 2012 at 21:53

1 Answer 1

1

I hope this link will help you : arduino-python

--- edited ---- My apology

The concept is sending something from python script to arduino. So the arduino can interpret it as a command to turn the LED on or off.

in Python Idle

>>> import serial
>>> ser = serial.Serial('com7', 9600)
>>> ser.write('ON')

meanwhile in arduino:

int LedPin = 13;
char msg = ' ';

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

void loop()
{
  while (Serial.available()>0)
  {
    msg = Serial.read();
  }

  if (msg=='ON')
  {
    digitalWrite(LedPin, HIGH);
    msg=' ';
  } 
  else if (msg=='ON')
  {
    digitalWrite(LedPin, LOW);
  }

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

1 Comment

You should include some explanation too, posting links does not do too much as an answer. It would be a fine comment though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.