How To GuidesRaspberry Pi

Building a Raspberry Pi Thermal Imaging Camera – MLX90640 guide

Raspberry Pi Thermal Imaging Camera MLX90640 guide

In today’s current global situation, thermometers and thermal cameras are more in demands probably than ever before. Here in the UK, many of the shops and restaurants have started taking the temperatures of customers at the door before allowing customers to enter the premises. This got me thinking, is there a better way to do this that doesn’t require human interaction?

I also realised I hadn’t done a Raspberry Pi project here yet either, which is absolutely not acceptable given how much I use the Raspberry Pi! So definitely something I had to quickly rectify!

That’s when I came across the MLX90640 – a thermal imaging camera from Melexis – that uses the i2c interface, making it perfect for use with the Raspberry Pi.

The MLX90640 features a 32×24 array of thermal sensors, can operate from -40 degrees Celsius to 300 degrees Celsius, and is accurate to within 1 degree. It’s also very compact, which ties into my other objective.

Making something portable, I wanted to be able to take this around and use it without being tied down to finding a power plug. This whole unit actually runs from a portable battery bank. In my (limited!) testing, I was able to get around 2 hours of use from a 10000Mah battery bank with the camera running.

Let’s get started!

Video Guide


Parts List

Note if you are struggling to find the MLX90640, try these alternate links to The Pi Hut, Mouser UK and Mouser US


I would start by printing the enclosure to save a little time whilst working on the hardware and software.

Head over to the Thingiverse and print the base, lid and camera clip. It’s worth noting that this case can actually be used as a standalone case without the camera if you so wish! Here are what the 3 parts look like:


First connect the display to your Raspberry Pi by pushing it onto the pin header. You’ll know you have it right when the HDMI slots line up, and you can then connect the HDMI bridge:

Boot the Pi up so that you can install the drivers required for your display, you’ll find the link for these in the manual of your particular model of display. I’m using an Elecrow display and I installed the display like so:

Power the Raspberry Pi back off for now.

With the display now working, taking a look at the underside of the display, you’ll notice that the header pins are exposed through solder pads on the PCB, these allow you to utilise the pins on the Pi that the display covers.

I started by soldering a 5 pin right angled header to the pads, giving me 5v, SDA, SCL and Ground which will be connected directly to the camera:

Connect 5v, SDA, SCL and Ground from the headers, directly to the MLX90640 camera with some DuPont wires. You’ll notice that the pin order on the MLX90640 is the exact same as on the Raspberry Pi which is awesome:


Powering the Pi back up, there is a few packages we need to install. In a terminal, run the following:

sudo apt install python3-matplotlib python3-scipy python3-numpy

I tried using the pip install command for this but one of the packages took almost an hour to install, I found the apt command to be much faster in this instance. We will also want to run:

sudo apt install python-smbus i2c-tools

Then run the following 2 pip commands to install the required dependencies:

sudo pip3 install RPI.GPIO adafruit-blinka
sudo pip3 install adafruit-circuitpython-mlx90640

Next we will want to enable i2c and adjust the baudrate of the i2c interface by setting these lines in the Raspberry Pi’s config.txt file located in /boot/config.txt:

sudo nano /boot/config.txt

#Make sure the following line is uncommented:

#Then add the following to the end:

#The full line should be:

This will allow the i2c interface to run at a faster speed and allow the camera to provide a faster framerate. Reboot to make sure these changes take effect.

Next you’ll want to clone my thermal camera GitHub repository using the following command:

git clone https://github.com/EverythingSmartHome/mlx90640-thermal-camera.git
cd mlx90640-thermal-camera/

Next we want to ensure that our i2c camera is detect properly by using the i2cdetect command. Run the followoing:

sudo i2cdetect -y 1

This will scan for all devices found on the i2c interface. It will output something that looks like this:

Notice how my camera is on address 0x33 which is as per the manufacturer spec.

Finally we can run the program by running the following:

python3 thermal-camera-advanced.py

It will take a few moments for the program to start up, but if everything went correctly you should now have a working thermal camera:

In my testing with this program, you will get around 1.5fps with the Raspberry Pi 3B, I would suspect the Pi 4 would be able to do quite a bit better.

Note if you want the camera to face towards you, run start the program using the following command to enable mirror/selfie mode:

python3 thermal-camera-advanced.py --mirror

Finally, we can take everything for a test run, I suggest aiming it at something that you know the temperature of, I am using my 3D printers print bed set at 60 degrees, and with the camera right up to it, it’s within half a degree which is awesome:

At a further distance it is less accurate as the image above because it depends on a number of variables.


With everything working we can now put everything in the case. Start by screwing the RPI into the base:

Then with the MLX90640 camera wires attached to the headers on the display, push the display onto the Raspberry Pi. Then you can route the wires through the camera clip, plug them into the camera and slide the camera into the rails on the camera clip:

Next put the base into the lid and make sure it clips into place. Finally slide the camera clip into the slot to secure it:

Huge thanks to LuckyPants from our awesome Discord community for helping with a the camera clip, couldn’t have done it without their help!


There we have it, a fully portable, versatile thermal imaging camera using the Raspberry Pi and an MLX90640! I’m really happy with how this project turned out actually, I think it looks great personally and I have quite a few uses for it. One thing that would be an awesome addition to this would be to have a normal camera in addition to the thermal camera, with the thermal image overlaid. Maybe that’s one for the future!

If you are looking for other guides, perhaps consider checking out the WiFi enabled MQTT Fingerprint sensor!

Want to support me?

If you like the content I put out and would like to support me, you can do so using the options below.

I can't state enough how much any support is greatly appreciated and will go directly back into funding new/bigger projects and website upkeep.

Tagged ,
  1. Svavar

    Great tutorial! I’ve had mine laying around for months and never got around to it. With your help I had it up and running in minutes!

  2. Duane

    Many thanks. A great tutorial and very accurate! Would you consider releasing the full case cad files or make it customisable as I hope to make a few changes/improvements?

  3. Duane

    Hi Lewis,

    I meant to add that the changes I would like to make are to provide a protective cover over the HDMI link, to incorporate the thermal camera module into the main chassis and to slightly close up the screen aperture. Hopefully those changes will be useful to other people who drop things!


  4. Lucy

    Hello sir is it applicable to transfer the Temperature Data into a from or textbox? Thank you and more power

  5. Duane

    Hi Lewis. Now I am back in lockdown I have more time to look into projects again so did you manage to look into releasing the STL files for the case?



  6. Pritesh Metha

    Hi I am getting following error.

    Traceback (most recent call last):
    File “thermal-camera-advanced.py”, line 6, in
    from scipy import ndimage
    File “/home/pi/.local/lib/python3.7/site-packages/scipy/ndimage/__init__.py”, line 153, in
    from .interpolation import *
    File “/home/pi/.local/lib/python3.7/site-packages/scipy/ndimage/interpolation.py”, line 37, in
    from scipy import special
    File “/home/pi/.local/lib/python3.7/site-packages/scipy/special/__init__.py”, line 633, in
    from . import _ufuncs
    ImportError: libf77blas.so.3: cannot open shared object file: No such file or directory

  7. Aakin

    So, I been looking for MLX90640 and it’s sold out on all over internet. What other model do you recommend with this project?

  8. Derek

    Hi keep getting the error
    I2c.py, line 62 in writeto_then_readfrom NotImplementError (¡
    “Currently can only write a single byte in writeto_then_readfrom

  9. Rolfer

    Hi. Great project. 2 questions –
    Is there a way to record the video for later analysis?
    And – similar question to earlier it would be great to log temperatures as text file. Thanks.

  10. Derek

    I’ve solved this error by re-flashing pi sd card. Now I get a constant red screen. Maybe there’s an answer pse?

  11. Duane


    If you could let me have any details of the STL files for the case I would be very grateful. I have searched Thingiverse but no luck finding it there….


  12. John


    Just come across this tutorial and inspired me to have a go but a really mundane question around power supply – you mention using a power bank but wasn’t clear to me if this would fit in the enclosure or if you have to have external and have a trailing power cord between the bank and the camera ? Assuming it is external would it be possible to get an enclosure which would also hold the battery ?

    Thanks John

    1. Lewis Barclay

      Hey John,

      It would need to be an external powerbank yes with a cable between them, it would be possible to design an enclosure for it but unfortunately not something I can do at the moment.


  13. jev

    tried running this but cam to a problem when trying to clone github repository asking for login details?
    i tried my details but nothing
    any thoughts?

  14. Min

    Hi Lewis,

    great application! I recently use the same sensor with a raspberry pi 3B for Infrared surface temperature measurement, but my frame rate is very low, only 0.7 fps. As you mentioned, in your application is the frame rate of 1.5 fps. Could you please give me some advice to improve the frame rate without changing a new version of raspberry pi?


Leave a Reply

Your email address will not be published. Required fields are marked *