Reading temperature sensor v1.2 on gopigo[SOLVED]

I have a gopigo and bought a Grove Temperature sensor v1.2. Based on the documentation and examples I inferred I should plug this sensor into the gopigo’s A1 analog port and could then read the sensor with this simple python script:

import grovepi

However this gives me the following

Traceback (most recent call last):
File “<pyshell#14>”, line 1, in
File “/home/pi/Desktop/GrovePi/Software/Python/”, line 237, in temp
a = analogRead(pin)
File “/home/pi/Desktop/GrovePi/Software/Python/”, line 214, in analogRead
bus.write_i2c_block_data(address, 1, aRead_cmd + [pin, unused, unused])
OSError: [Errno 5] Input/output error

Is it possible that this v1.2 sensor is not supported? Your page

points to the page for v1.0 sensor. Any help much appreciated.


Hi @jonathan-conning,
It looks like the Temperature Sensor code that you are using is of the GrovePi and would not work as such with GoPiGo. You will have to write your own code to read it from Port A1(Pin-15).
First you will have to import gopigo and math libraries then do analogRead(15) to read the analog values and finally follow the formulae given below to convert it to temperature readings.
resistance = (float)(1023 - a) * 10000 / a
temperature = (float) (1 / (math.log(resistance / 10000) / bValue + 1 / 298.15) - 273.15)

Note: a= Analog Value from the Sensor
         bValue= 4250 for the Temperature Sensor V1.2

Please let us know if this helps,

Great, that works. Thank you! There was one tiny typo: there should be no parenthesis following 273.15.

Your hints led me to this page which explains the resistance-temperature relationships behind the code formula in a bit more detail:

For any relative newbies like myself here is a simple python function that does the above to read the sensor temperature in centigrade

from gopigo import analogRead
from math import log

def read_temp():
B = 4250
a = analogRead(15)
resistance = (1023-a)*10000/a
temperature = 1/(log(resistance/10000)/B + 1/298.15)-273.15
return temperature

To then print out a temperature stream (one reading per second):

from time import sleep
white True:

Indentation got lost in the paste… but you get the idea.

This topic was automatically closed after 12 hours. New replies are no longer allowed.