I am playing with arduino. Inside the projects book it has a project for getting the temperature from a temperature sensor. I hooked as it says and wrote the following simple code
const int sensorPin = A0;
void loop(){
int sensorVal = analogRead(sensorPin);
float voltage = (sensorVal / 1024.0)* 5.0;
float temperature = (voltage - .5) * 100;
Serial.println(temperature);
}
to print the temperature in the serial monitor. I used pySerial to get the temp from the arduino like this
try:
ser = serial.Serial('/dev/ttyACM0', 9600)
except serial.SerialException as se:
print se
exit()
while True:
temperature = ser.readline()
print "Temp from arduino", temperature
try:
temperature = float(temperature)
print "FLoat temperature", temperature
except ValueError as ve:
print ve
continue
But the problem is that while the Serial monitor from the arduino ide shows the temperature normally (22.3 e.g) a normal float number that is python has the number divided by 10
Temp from arduino 2.27
FLoat temperature 2.27
How can I deal with that?
float()
much ealier, beacause after recieving the data.. You've already ruined it by not treating it as afloat()
from the start. For instance, you can't dox = 10.15; x = int(x); x = float(x);
because it will end up10
even tho you try to convert it back. Not that this will help but dotemperature = float(ser.readline()
instead. – Torxed- .5
? Try to print thevoltage
value to the serial to see the results. – Andrei Boyanovtemperature
is a string and this is a good idea to print in for debugging before converting it to float. Really it's a better practice to use different variables for that... – Andrei Boyanov