Python PySerial read()返回不正确的值
我有一个带有标准模拟温度传感器的Arduino,由Raspberry Pi的USB端口供电,还有一个获取所有传入值的Python脚本 但是,我发现输入值的偏差约为10度 当我将其连接到计算机时,串行监视器返回正确的值。我想这会缩小到覆盆子皮上的一个错误 Python代码:Python PySerial read()返回不正确的值,python,arduino,raspberry-pi,pyserial,Python,Arduino,Raspberry Pi,Pyserial,我有一个带有标准模拟温度传感器的Arduino,由Raspberry Pi的USB端口供电,还有一个获取所有传入值的Python脚本 但是,我发现输入值的偏差约为10度 当我将其连接到计算机时,串行监视器返回正确的值。我想这会缩小到覆盆子皮上的一个错误 Python代码: import serial ser = serial.Serial('/dev/ttyACM0', 9600) while True: temp = float(ser.readline()) tempstri
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
temp = float(ser.readline())
tempstring = "{:.1f}".format(temp)
print(tempstring)
f = open("/var/www/temp", "w")
f.write(tempstring)
f.close()
Arduino良好测量代码
const int thermometer = A0;
void setup(){
Serial.begin(9600);
}
void loop(){
int sensorReading = analogRead(thermometer);
float volts = (sensorReading/1024.0) * 5.0;
float temp = (volts - 0.5) * 100.0;
int largeTemp = temp * 100.0;
Serial.println(largeTemp);
delay(10000);
}
我不想麻烦发送浮点,因此int-largeTemp=temp*100.0;线路。我打赌Pi的USB端口提供的电压太低,导致读数丢失。这确实是一种可能性。但这不会使读数下降,而不是上升吗?编辑:另外,我将如何使用另一个电源?我正在使用USB进行数据传输。我支持DavidSchwartz,更强的电源可能会改善这种情况。@user3433131电压越低,读数越高。算算。你的代码乘以5.0。它应该乘以,比如说,4.5。所以你得到的值太高了。执行此操作:1用电压表测量电源电压。2返回传感器读数的数值。3用测量电压替换5.0,重新计算温度。如果这解决了问题,我的理论就被证明了。顺便说一下,4.5V到4.75V是Pi USB端口的典型电压。尝试使用带电源的集线器,尽管其中一些集线器的电压超过5伏,高达0.6伏。如果您关心准确性,则应测量电源电压,而不是硬编码。或以最终形式校准的装置。或调节电源电压。或者其他解决方案。