BBC Micro Bit Projects
In this short article we are going to be using micropython to change the colour of a micro:pixel depending on the temperature being read from a TMP36.
First up I’m new to python, in fact I’ve only recently started looking at programming again (I was 17 when I last did any) so if you think something could be done better you’re probably right – so why not let us know in the comments?
Firstly I hooked up the TMP36 to pin 1 on the micro:bit using one of our exhi:bit boards. I then used crocodile clips to hook up the micro:pixel to the exhi:bit.
Then with a little help from Drew I managed to cobble together this code which basically takes a reading from pin1 which the TMP36 is hooked up to, it then creates a new variable called temp which is the analogue reading from pin1 multiplied by 3300/1024 – 500 then divided by 10 to get the temperature in Celsius.
We then display the temperature on the LED matrix. After this we create another variable called tempcolour which is an int by taking temp and multiplying it by 3.6 to scale it from 0-30 Celsius up to a scale of 128 for the colour value of the neopixels.
We then need to display the colour so we set the R, G and B value with Red being tempcolour, green being zero and blue being 255- (tempcolour*2) then we show the colours.
Much to my surprise this seems to work, I’m sure more can be done to make it work better but that’s for another day.
You can download the code here: TempToLight
You can get the micro:bit here
You can get the micro:pixel here
You can get the TMP36 here
You can get the exhi:bit here