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.
2 Answers 2
I had the same issue with the old Model B raspberry Pi, but also with the new Raspberry Pi, and in my case the problem was solved by powering the Arduino independently through the power connector rather than relying on the USB for power.
In my case I had the Arduino connecting alternately to my Mac/Linux laptop and to the Pi. The output values on the laptop were slightly low (at first I thought they were correct, but once I resolved the problem & compared the values to a thermometer I realised they were a little low). Weirdly, when connecting by USB to the Pi, the values were way too high (as the OP found). So it was a voltage problem.
-
1Good call. The difference in measurements corresponds to just under a half volt difference in the USB rail, which is entirely believable. It would probably better to provide an ADC reference voltage (or use a digital sensor). Another option can be to measure a known voltage as well and compensate the unkown voltage by the proportionate error of the known one. Even the pi's regulated 3.3v is probably a better reference than a cheap USB charger operating under substantial variable load.Chris Stratton– Chris Stratton2015年11月19日 23:09:11 +00:00Commented Nov 19, 2015 at 23:09
Given that the Arduino code operates independently from whatever system is connected to it the most probable cause is, that you are moving the Arduino/breadboard between locations.
The system you are using to read the serial port can not be at fault. Provided, of course, both use the same baudrate settings etc. Are you using the Arduino IDE serial-port interface? Or a commandline tool like cu
or picocom
?
This is very likely an environmental problem.
Probably the Arduino is standing in a sun-lit and/or poorly ventilated area when near the Pi and in the shadows when near the desktop computer.
-
I think it is some way related to the USB port of the raspberry pi. I'm not moving the board. 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.miepstei– miepstei2015年05月06日 19:13:34 +00:00Commented May 6, 2015 at 19:13
-
Could you update your original question with this additional info (including the code you're now using)?Mausy5043– Mausy50432015年05月07日 15:51:06 +00:00Commented May 7, 2015 at 15:51
-
Have you looked at the data when you power it independently? I.e. nothing connected via the USB-port.Mausy5043– Mausy50432015年05月07日 15:52:07 +00:00Commented May 7, 2015 at 15:52
-
Can we exclude sensor-self-heating due to multiple reads in short succession?Mausy5043– Mausy50432015年05月07日 16:03:16 +00:00Commented May 7, 2015 at 16:03
-
I have updated the original question. I don't think it is sensor self heating as the temperature reads are constant in both cases - they consistently differ by 6-7 degrees. They are not getting hotter.miepstei– miepstei2015年05月07日 20:17:46 +00:00Commented May 7, 2015 at 20:17
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.