Help with Serial Communication between Arduino Uno and Raspberry PI

I'm attempting to control about 12 LEDs using two 74HC595 shift registers. I have the LEDs and shift registers wired up correctly. Then using the Serial monitor I can send a string with either a 1 or a 0. This then turns on the LEDs if its a 1. The code is all working in the Arduino and when I use the desktop serial monitor. However, I wanted to next have the string be sent from a raspberry pi using a serial connection. This also works but only on the first attempt, if I run the python script on the raspberry pi again with a different string of 1s or 0s the LEDs do not change. I have to fully power cycle the Arduino and then reconnect it to the serial connection with the Pi for the Arduino to accept the new string. Is there a way to code in python on the Pi script or in the Arduino IDE to clear the previous serial message sent to the Arduino so it can read a new one? Code for both is below

Arduino Code
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


#include <Shifty.h>

Shifty reg;

void setup() {
reg.setBitCount(16);
reg.setPins(11 , 12, 8);
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) {
String data = Serial.readStringUntil('\n');
Serial.print(data);
reg.batchWriteBegin();
for( int i = 1; i <= 12 ; i++){
if (data.substring(i,i+1) == "1"){
reg.writeBit((i-1), HIGH);
}
if (data.substring(i,i+1) == "0"){
reg.writeBit((i-1), LOW);
}
}
reg.batchWriteEnd();
}
}

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Python RaspPi code
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

red_lightboard = serial.Serial('/dev/ttyACM0',9600, timeout=1) 

red_lightboard.reset_output_buffer()

testLED = 'r000111111111\n'

red_lightboard.write(testLED.encode('utf-8'))

line = red_lightboard.readline().decode().rstrip()

print(line)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience this is the easiest way to tidy up the code and add the code tags

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like

I've written a small tutorial on interfacing with Python. See Two ways communication between Python3 and Arduino

1 Like

This is nice, but I guess I have a more specific question as to why the Arduino is only receiving one serial message, which is a string when I run the code, Then it I run it again it doesn't receive another string/serial message?

I don't read code if it's not in code tags. You've been kindly asked in post #2 to do so.

➜ do yourself a favour and please read How to get the best out of this forum and post accordingly (edit your post and add code tags)

Apologies, I wasnt exactly sure how to do it but I think I posted it correctly now. So Im running the python code with python3 on a raspberry pi. Im running this code first. This then sends a message through a serial connection to an arduino uno. It works sending one string message to the arduino, but the arduino has to be reset for it to read another message from the Pi if I run the python script on the py again. Do you know why that is?

your python code does not loop, so it sends one message and that's it

As explained in my tutorial every time you open the Serial line your arduino will reboot so some data might or will be lost if your python scripts sends stuff right away

So to avoid that, I should have a delay in the python script so when I run it, it doesnt get wiped when the arduino resets?

A delay is just second guessing the time needed, see my tutorial

Are you fine rebooting the arduino at every command?

I would prefer not tooo

So I looked over your tutorial, while very handy it still doesn't full answer my question, how do I rerun the python script which sends a serial message to the Arduino a second time after the I ran the script once after the connection was made? Like how do I clear the first serial message sent so that way I can send a new one?

Hello, I am attempting to use a raspberry pi 3 to control LEDs on an Arduino uno. I am using shift registers on the Arduino uno to expand the outputs. I am connecting the Arduino to the Raspberry Pi through a serial connection cable. I control the Raspberry pi from my laptop using SSH and PUTTY. I am sending a string from the raspberry pi through the serial connection to the Arduino. The Arduino then reads the string and lights up the corresponding LEDs. When I have everything connected it all works. Except if I keep everything connected and run the script on the raspberry pi again this time changing the string, nothing happens. I have to fully disconnect and power cycle the Arduino and then reconnect it for it to read and accept my new string from the python code. Is there a reason for this? Is there someway to code in python to clear the initial string message sent to the Arduino so that way it can read the second one when I run the script a second time? I have attached both the python and the Arduino code below.

Arduino Code

#include <Shifty.h>

Shifty reg;

void setup() {
  reg.setBitCount(16);
  reg.setPins(11 , 12, 8);
  Serial.begin(9600);
  }
void loop() {
  while (Serial.available() > 0) {
    String data = Serial.readStringUntil('\n');
    Serial.print(data);
    reg.batchWriteBegin();
    for( int i = 1; i <= 12 ; i++){
      if (data.substring(i,i+1) == "1"){
        reg.writeBit((i-1), HIGH);
      }
      if (data.substring(i,i+1) == "0"){
        reg.writeBit((i-1), LOW);
      }
    }
    reg.batchWriteEnd();
  }
}

Python code

red_lightboard = serial.Serial('/dev/ttyACM0',9600, timeout=1) # Sets up serial comm with arduino uno using usb connection
red_lightboard.reset_output_buffer() # clears any data in the input buffer

testLED = 'r000000000001\n'

red_lightboard.write(testLED.encode('utf-8'))

line = red_lightboard.readline().decode().rstrip()

print(line)

```

@devonr363, do not cross-post. Threads merged.

Okay apologies, do you have any advice on my issue?

the script does not end. it's basically like this

Open the Serial port
awaits for the ACK from the Arduino
infinite loop dealing with commands

I see so I have to have a kind of quit function in the python script for it to be able to send a new serail message?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.