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(){
void loop(){
     mode(Serial.read() - '0');  
void  mode(int n){

    if (n== 1){
    if (n== 2){

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).


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.

| 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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