1

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

asked May 2, 2014 at 16:51
0

1 Answer 1

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.

answered May 3, 2014 at 8:11
2
  • 1
    Have you also tried other rates that worked (or not) between 9600 and 115200 bauds? Commented May 3, 2014 at 11:55
  • That is weird. Anyone know why that's happening? Commented May 3, 2014 at 14:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.