0

I am trying to read analog read from ardunio connected to my raspberry pi. Following are my raspberry pi and arduino codes.

Raspbery pi:

import time
import serial 
ser = serial.Serial('/dev/ttyACM0',9600)
ser.flushInput()

while True:
    value = ser.readline()
    print (value)
    time.sleep(0.5)

Arduino Code:

int val_1,val_2;
void setup()
{
   Serial.begin(9600);
}

void loop()
{
    val_1 = analogRead(A0);
    val_2 = analogRead(A1);
    Serial.print(val_1);
    Serial.print(",");
    Serial.println(val_2);
}

When I try to run my python script, I get the initial analog readings from the arduino, but as the values seems to be not updating. I had to restart the script every time to read the analog pin readings.

Need to know where I am going wrong or have I missed out important factor in this.

Thanks in advance :)

  • Does the answer from oh.dae.su help you? If so, please accept it with a click on the tick on its left side. This finishes your question and it will not pop up again year by year. – Ingo Nov 5 at 11:52
0

You write with your Arduino to the serial interface continuously without any break.

On the Raspberry Pi you only read a single line every 0.5 seconds. This means your writing and reading are not in sync.

An easy remedy would be to add a delay in your Arduino code

delay(1000);

that is longer than the sleep in your Raspberry Pi code. You can drop the sleep in your Raspberry Pi code altogether.

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.