I am building something where a Raspberry Pi connects to a database to check if a value is right. If the value is right it sends a signal to an Arduino through Serial.write(). Then the Arduino reads this data and if the Arduino reads 1 it should send a HIGH signal trough pin 13.
This worked a few months ago, but when I tried it last week it only worked a few times. It seems as if the Arduino isn’t always able to read the signal the Pi sends. Most of the times it only works the first time after I make a change and then it seems like it doesn’t read the signals anymore.
Arduino code:
int n;
void setup(){
pinMode(13,OUTPUT);
pinMode(10,INPUT);
Serial.begin(9600);
n=7;
}
void loop(){
if (Serial.available()){
n = Serial.read() - '0';
}
if(n==1){
digitalWrite(13,HIGH);
}
else{
digitalWrite(13,LOW);
}
int rst = digitalRead(10);
if(rst==HIGH){
n=7;
}
}
Raspberry Pi Python code which sends the signal via Serial.write ((arduinoSerialData.write()).
if strscan == barcode:
print ('code valid')
arduinoSerialData.write('1')
break
else:
print ('code invalid')
arduinoSerialData.write('2')
Have you checked manually if the RPi is actually sending something?
If you have, and it is, I’m afraid the answer to your question is not so software related. Bottom line is that RPi works with 3v3 signals where most Arduino boards work with 5V. To make it even more complex, the pi can send out signals of 5V but can only take up to 3v3 on its inputs. When you put 5V on such a pin, the pi probably won’t go up in smoke immediately but prolonged exposure to 5V may hurt the pi in the end and cause inexplicable behavior, like for example what you’re describing.
You’re saying the pi is connected through serial. If it is connected bidirectionally and the Arduino is sending stuff over to the pi this will cause problems.
Also, the digital input pins of your Arduino are configured such that they trigger above a certain threshold voltage. I don’t know this value for your the Arduino board you are using. But if it is around 3.3 volts or higher, the signal of the pi may be just to low voltage to trigger the Arduino.
The solution to the problems described above are logic level converters. You hook one Vcc up to the 3v3 and the other to 5V. Then you connect the pi and the Arduino to each other by routing the signal wires through this logic level converter. The llc will take care of voltage conversion. Make sure you get a llc that is bidirectional, this will save a lot of hassle. For example, this one is a suitable one.