I'm using a Arduino Uno Rev 3 connected over usb to a Raspberry Pi Modell B+ running Rasbian.

Here ist the arduino code:

int input = 0;
String command = ""; // beinhaltet den aktuellen befehl
int Action = 1;

void setup() {
Serial.begin (9600);

void loop() {
  if (Serial.available() > 0) 
        input = Serial.read();
    char currentChar = input;

    if(currentChar  == '!') // frage nach trennzeichen
      Action = command.toInt();

      command = ""; // befehl wieder leeren
      command = command + currentChar ; // falls noch kein trennzeichen vorhanden, erweitere kommando

Here is the Raspberry Pi code (in python)

import serial
from time import sleep as sleep
ser = serial.Serial('/dev/ttyACM0',9600)
s = [0]
while True:
        a =  ser.read(1)
        print a

It always works if once after the code was uploaded to the arduino, but if I stop the script on the pi and restart it it stops working. What is the problem here?

  • Does the PI close the serial port as it exits? If it doesn't can it open it again? Commented Jul 12, 2016 at 12:11
  • It works on my PC running Ubuntu, although it prints tons of CR and LF. You may have noticed that you are overflowing the Arduino's input buffer. Commented Jul 12, 2016 at 12:17
  • @EdgarBonet how do I prevent the input buffer from overflowing?
    – jonathan
    Commented Jul 12, 2016 at 12:41
  • Speak slowly, e.g. by moving the sleep(2) inside the loop. You may also want to read the whole answer (3 bytes) in each iteration. Commented Jul 12, 2016 at 12:49
  • You do know that the Uno resets, when you create a new serial connection to it, right?
    – Gerben
    Commented Jul 12, 2016 at 12:55

1 Answer 1


"It always works if once after the code was uploaded to the arduino...." Whenever you you reset the arduino the link between the arduino and the rasperry pi is broken momentarily thus causing the code on the raspberry pi to hang/ffreeze and not responding/ showing any valid results.

Try giving a keyboard interrupt by ( Ctrl + Z ) and restart the python script and that should work just fine!

Your Answer

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

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