1
votes

I need to communicate raspberry pi with the arduino over serial communication. And to communicate, I'm using same baud rates in both side but still i'm unable to do this thing..

this is my Arduino code

int ledPinSpeedOne = 11;
int ledPinSpeedTwo = 12;
int ledPinSpeedThree = 13;

char inbyte;

void setup() {

  Serial.begin(9600);

  pinMode(ledPinSpeedOne, OUTPUT);
  pinMode(ledPinSpeedTwo, OUTPUT);
  pinMode(ledPinSpeedThree, OUTPUT);

  digitalWrite(ledPinSpeedOne, LOW);
  digitalWrite(ledPinSpeedTwo, LOW);
  digitalWrite(ledPinSpeedThree, LOW);
}

void loop()
{

  if (Serial.available() > 0) {
    delay(100);
    inbyte=Serial.read();
    if ( inbyte == '3' ) functionSpeedTwo();
    }
}

//functionSpeedTwo
void functionSpeedTwo() {
  digitalWrite(ledPinSpeedOne, LOW);
  digitalWrite(ledPinSpeedTwo, HIGH);
  digitalWrite(ledPinSpeedThree, LOW);
}

And here is what i have in raspberry pi side,

#!/usr/bin/python
import serial  
ser = serial.Serial('/dev/ttyACM0',9600) 
ser.write('3')

this thing is not working for sometimes but sometimes it's worked. Can anyone help me to solve this problem.

1
See if the arduino resets when running the python script. If so, set the control line state before opening the port with pyserialhandle
what do you mean by "control line state" ???Ishara Madushani
Does your Arduino reset when you run the script?handle
no it's not.. i have solved the problem.Ishara Madushani

1 Answers

0
votes

i have solved my problem.There was a time gap to access the value, i just had to add a while loop in order to get the value. In my arduino code i have added a delay in line no 24.