Arduino Uno Raspberry Pi Serial Communication double readings

Arduino Uno Raspberry Pi Serial Communication double readings



I use an Arduino Uno to convert Analog data to digital from a light sensor and send this data to raspberry pi by an USB cable. However when I run the code I read values like 1923 from a 10-bit sensor.



Here is the arduino code


int a = A0;
int meas = 0;
void setup()
Serial.begin(9600);

void loop()
meas = analogRead(a);
if(Serial.readString() == "1") //Check that Raspberry wants data or not
Serial.println(meas);




Here is the Python code in Raspberry Pi


import serial
from datetime import datetime
now = datetime.now()

ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write("1".encode())
s = ser.readline()

file = open("dataset.txt", "a")
file.write(now.strftime("%Y-%m-%d %H:%M") + " Sensor Value:" + str(s)+ "n")
file.close()



Here is the example output after running code once in every 5 minutes


14:08 Sensor Value:6
14:10 Sensor Value:8
14:15 Sensor Value:8
14:20 Sensor Value:10
14:25 Sensor Value:6
14:30 Sensor Value:9
14:35 Sensor Value:6
14:40 Sensor Value:7
14:45 Sensor Value:5
14:50 Sensor Value:5
14:55 Sensor Value:12
15:00 Sensor Value:1
15:05 Sensor Value:1
15:10 Sensor Value:10
15:15 Sensor Value:12
15:20 Sensor Value:14
15:25 Sensor Value:1922
15:30 Sensor Value:2211
15:35 Sensor Value:11
15:39 Sensor Value:6
15:40 Sensor Value:7
15:45 Sensor Value:8
15:50 Sensor Value:10
15:55 Sensor Value:1
16:00 Sensor Value:
16:05 Sensor Value:11



I want to get rid of these 1's and 1922 like things they are certainly meaningless data.



PS: Sensor is on the top of a mountain, I am using remote connection the check the data and manipulate the code.



How can I do that? Thank you for your time.





Try testing serial.available() before reading on the Arduino and waiting 200ms before the readline() on the Pi maybe.
– Mark Setchell
Aug 27 at 15:56


serial.available()


readline()




2 Answers
2



I think Mark Setchell is right. You are getting data from past measurements.



Personally I'd implement a more robust protocol, but since your application is quite basic you can try with a simpler approach, which is what he suggested.



This is solved easily by adding a small delay in the python program, between the request and the reading. Something like this can be enough:


from time import sleep
...
ser.write("1".encode())
sleep(0.05);
s = ser.readline()



In the mean time, I don't like the way you handle the reading in the arduino. If you are always sending single-char commands, I suggest this approach:


void loop()
meas = analogRead(a);
if (Serial.available())

if (Serial.read() == '1')

Serial.println(meas);





This does not block the execution of the loop (which can come in handy if you plan to expand the functionalities)





I tried what you said in my Python Code but now I am getting nothing from the arduino. In Raspberry Pi
– EkinEkinEkin
Aug 29 at 12:11





@EkinEkinEkin did you change the arduino code or just the python? which version of python are you running?
– frarugi87
Aug 29 at 13:36





I change both and I am using python 2
– EkinEkinEkin
Aug 31 at 6:31





@EkinEkinEkin can you try with your arduino code?
– frarugi87
Aug 31 at 9:39





It's working sometimes, still not working properly
– EkinEkinEkin
Aug 31 at 10:29



You might have a look at calibration, here's a sample piece of code,



https://www.arduino.cc/en/Tutorial/Calibration





But sensor is in a box on the top of a mountain
– EkinEkinEkin
Aug 27 at 14:05





The code is for calibrating the values, emitted by the sensor. You need a method to limit the range of values.
– AYA
Aug 27 at 14:14






I think it's caused by the asynchronous communication, have you any idea about to fix that?
– EkinEkinEkin
Aug 27 at 15:35





No amount of miscalibration will make analogRead return more than 1023. Calibration won't fix it.
– gre_gor
Aug 27 at 19:48


analogRead





As you've mentioned miscalibration here, which implies a misunderstanding , calibration is not on the device but to determine and specify a valid range of values within the code. @EkinEkinEkin, have you tried posting on Sensors forums under arduino.cc. They would ask for the diagram and detailed sensor info as well.
– AYA
Aug 27 at 22:01






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)