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 goMadivad– Madivad2015年12月14日 15:25:12 +00:00Commented Dec 14, 2015 at 15:25
1 Answer 1
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();
-
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 RPiAndrew.Stack.Overflow– Andrew.Stack.Overflow2015年12月12日 15:23:51 +00:00Commented Dec 12, 2015 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– NVRAM2015年12月12日 20:49:58 +00:00Commented Dec 12, 2015 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 firstSerial.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 isSerial.read()
expects one?dlu– dlu2015年12月13日 06:31:27 +00:00Commented Dec 13, 2015 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– Andrew.Stack.Overflow2015年12月13日 15:05:51 +00:00Commented Dec 13, 2015 at 15:05