I've set up an arduino uno to monitor room temperature and communicate this to my Raspberry Pi (old model B) over USB (2.0 cable) using pySerial to read the data on the raspberry pi side.
The code I am using on the arduino [see Update 1] is:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop() {
int valT = analogRead(0);
float mV = (valT / 1024.0) * 5000;
float temperature = (mV - 500) / 10;
lcd.setCursor(0, 1);
lcd.print(temperature);
Serial.println(temperature);
delay(5000);
}
as per example tutorial. On the raspberry pi side I am reading the data in the interpreter using the following commands:
import serial
ser = serial.Serial('/dev/ttyACM0',9600)
while : 1
ser.readline()
When I run this on the pi I receive values of around 25/26 degrees:
26.17
26.66
26.66
26.66
26.66
which seems a bit high. When I connect it to linux/mac desktop I receive temperatures of 19/20 degrees which seems correct:
19.34
19.82
19.82
19.34
20.31
I don't understand why the pi is printing numbers that are substantially higher. I've recompiled the arduino code on the pi and repeated the experiments and I get the same result. I've sent static numbers over the serial connection e.g. (Serial.println(13.77)
) and I can read that accurately on the pi.
Any ideas? Am I missing something obvious?
UPDATE 1
I think it is some way related to the USB port of the raspberry pi. I'm not moving the board so I'm pretty sure its the same temperature. I installed an LCD screen on the board to output the numbers without going via the serial port. When I plug it into the pi I get 28.61 and my linux desktop it displays 20.31.
UPDATE 2
I tried with a new (latest) Model B raspberry pi and it outputs the same temperature as the Mac/Linux desktop. Its something to do with the old raspberry pi.
while true; do; cat /dev/ttyACM0;echo;sleep5; done
and I receive "26 26.1 26.6 2" where the spaces denote newlines. On linux I get "19 2 1 29 20 20. 19.8"Serial.print("Temp is "); Serial.println(temperature); Serial.print("**");
and try to pin down where the curious conversion is taking placeSerial.print("**Temp is "); Serial.println(temperature); Serial.print("**");
and get '****Temp is 18.85\r\n' on the linux box and '****Temp is 26.17\r\n' on the pi. I dont understand why two of the asterisks are not at the end of the line but maybe thats a different issue.