1

I am trying to get Arduino Uno with Raspberry Pi to communicate with each other. I want the python script to write a message and for my Arduino to read it. This was working before, but I came back a week later and it stopped. Essentially, I want the Raspberry Pi to write '3' and for my Arduino to recognize this 3 and light up some LEDs. Where do you think I am going wrong? The Arduino works when testing it through the IDE and Serial Monitor. There are no errors when running the python script, the LEDS just don't light up, so I assume there is some miscommunication.

Python Code

import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write('3')

Arduino Code

String serialRead;
int val;
void setup() {
  Serial.begin(9600);
  strip.begin();  
  strip.setBrightness(25);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  serialRead = Serial.read();
  if (Serial.available() > 0) {
    val = Serial.read();

    if (val > '0' && val <= '9') {
      Serial.println(val);
      val = val - '0';
      Serial.println(val);

      if (val == 3)
      {
        //strip.setBrightness(100);
        colorWipe(strip.Color(127, 127, 127), 50);
        colorWipe(strip.Color(0, 0, 0), 50);        
      }
    }
  }
}
  • What you can do is remove the rPi from the equation. Run the sketch and send your own serial commands direct via your computer and see if the LEDs change as expected. That will narrow down which end is having the problem: works, it's the rPi send/arduino rcv. doesn't work, it simply your code or the LED setup. Report back and tell us how you go – Madivad Dec 14 '15 at 15:25
4

Just at a glance:

Your loop consumes the byte and stores it in serialRead, so there is probably no available data at the outer if, so it is never entered.

Try deleting this line:

serialRead = Serial.read();
|improve this answer|||||
  • Unfortunately, that did not work. I am able to get it to work through the Arduino IDE using Serial Monitor both ways, but still not with the RPi – Andrew.Stack.Overflow Dec 12 '15 at 15:23
  • I suspect a timing issue; perhaps the rPi sends the '3' before the Arduino is listening? Have you tried to put a loop w/delay in the Python code? – NVRAM Dec 12 '15 at 20:49
  • The first Serial.read(); is a problem for sure – unless the Raspberry Pi sends more than one '3' this line could consume it, and then nothing will happen. I'm suspicious that there are garbage characters that are consumed by that first Serial.read(); when you're testing with Serial Monitor. Why don't you print what your get from it. Also is the pi sending a newline? Could the problem be that there is Serial.read() expects one? – dlu Dec 13 '15 at 6:31
  • I took out the first Serial.read(); and the problem persisted. I think NVRAM may be right with the timing issue. I was able to get it to work with constantly writing a 3. Unfortunately, I had to give the setup back to my friend so I won't be able to test this issue further. Thanks for the help. – Andrew.Stack.Overflow Dec 13 '15 at 15:05

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.