Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I can't figure out why the next script is not working. I have attached the Arduino to one USB port of the raspberry pi, in which I've installed pyserial. I just want to turn off/on a led depending of the number I write on the Raspi console (1-> on, 2-> off).

    int led=13;

void setup(){
  Serial.begin(115200);
  pinMode(led,OUTPUT);
}
void loop(){
  if(Serial.available()){    
     mode(Serial.read() - '0');  
  }
}  
void  mode(int n){

    if (n== 1){
         digitalWrite(led,HIGH);
    }    
    if (n== 2){
        digitalWrite(led,LOW);     
    }   
  }

PS: I double checked and the baudrate is configured also at 115200 at the raspi side. In addition, if I place digitalWrite(led,HIGH) just before the line "mode(Serial.read() - '0');" the led turn on when I send some information from the raspi shell (so, works well).

share|improve this question

1 Answer 1

Finally I've found the error, it was the serial communication baud rate: 115200. It seems that at higher speeds it not works properly, lowering it to 9600 do the trick.

share|improve this answer
1  
Have you also tried other rates that worked (or not) between 9600 and 115200 bauds? – jfpoilpret May 3 '14 at 11:55
    
That is weird. Anyone know why that's happening? – AsheeshR May 3 '14 at 14:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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