/* jquery */ /* jquery accordion style*/ /* jquery init */

22 May 2017

Temperature Sensor (micro:bit MicroPython)

In this example we obtain the temperature of the micro:bit processor, which rises quite quickly after it is powered on.

from microbit import *

while True:
   # obtain the CPU temperature
   t = temperature()
   display.scroll(str(t) + 'C')
   # wait 5 seconds before repeating

Once again we indent the main code inside a While True: endless loop. However, this time there's a five second delay between each loop iteration.

Notice that we also use the str() function to convert the integer temperature value in the variable t into a string. Leaving it as an integer will result in an error condition when the program runs.

To see the temperature rise disconnect the micro:bit for a while, so it can cool down, then reconnect to downloaded and run the program. Next try modifying the code to display the temperature in Kelvin (hint: 0 Celsius is 273 Kelvin).

Return to the micro:bit MicroPython Coding Tutorials page.

No comments: