Introduction: Measuring Temperature With I2C Sensor LM75A on Raspberry Pi
In this tutorial you will learn how to measure temperature using the I2C sensor LM75A connected to any model or version of Raspberry Pi. The tutorial also provides 3 open source code samples written in the C programming language, Python and Bash. Have a look at the next steps as well as the YouTube video for details and subscribe to my channel if you like it.
Step 1: LM75A
LM75A I2C module is a fairy cheap temperature-to-digital converter with NXP chip and configurable address. It has a wide range for temperature detection. You can have a look at details of the sensor in its data sheet at nxp.com.
The main advantage is that the I2C address of the sensor is configurable through pins A0, A1 and A2. This way you can use simultaneously up to 8 LM75A sensors attached to a single Raspberry Pi or another development board.
Step 2: Required Hardware
The following hardware is required for this tutorial:
- Raspberry Pi
- LM75A IC2 temperature sensor module
- USB power supply
- microSD card with Raspbian GNU/Linux distribution for Raspberry Pi
- Female to male Dupont jumper wires
- Male to male Dupont jumper wires
- 3 resistors
- Tools for soldering
LM75A module is compatible with any model and version of Raspberry Pi. In the YouTube video I am using Raspberry Pi 3.
Step 3: Soldering
A soldering iron is required to use LM75A module.
First solder the 5 pin header to the LM75A module. These pins will be used to mount the sensor to a breadboard.
After that solder the pads on the back to set an address. This step is more tricky. On the back side of the module you will see pads for A0, A1 and A2. As I said before these pads are used to set the I2C slave address of the sensor. It is important that they should not be left floating. You have to connect them to ground or VCC.
As you can see in the video I want to keep the I2C slave address as configurable as possible so I will do a workaround. I will solder low value resistors to each pad. Ideally 0 Ohm would be perfect. I have 100 Ohm resistors and this is also fine. After soldering the resistors I can easily change the I2C slave address by connecting the resistors to ground or VCC on my breadboard.
Step 4: Wiring
In general the wiring on the breadboard is straight-forward for module with LM75A or any I2C sensors.
Connect VCC of the module to pin 1 of Raspberry Pi (3.3V). GND of the module should go to pin 9 of Raspberry Pi. After that also connect SDA to pin 3 and SCL to pin 5 of Raspberry Pi.
In this case the tricky part is the workaround with resistors that I did. Initially I will connected A0, A1 and A2 to ground. This way the I2C address assigned to LM75A will be 48. Have a look at the list of the 8 different configuration that are available for the address.
Step 5: Installing Software
Now it is time to focus on the software. Please perform the following steps:
- Boot Raspbian GNU/Linux distribution on your Raspberry Pi.
- Install the following packages:
sudo apt-get update sudo apt-get install git build-essential python-dev python-smbus i2ctools
- After that type raspi-config and enable I2C.
- Reboot the board to complete this step.
Step 6: Retrieving Temperature Using the C Programming Language
I have published several free and open source examples for reading temperature on Raspberry Pi using LM75A. They are available in GitHub. Please give a star to the project if you find them useful.
The first example is written in the C programming language. Perform the following steps to use it:
- Get the source code from GitHub
git clone https://github.com/leon-anavi/rpi-examples.git
- Build the application using its Makefile:
cd rpi-examples cd LM75A/c make
- Run the application:
By default the application uses address 0x48 for LM75A. You can pass another I2C address as a command line argument.
Step 7: Retrieving Temperature Using Python
The python example requires a couple of additional dependencies which we have already installed on the previous step. Run the python script to get the temperature, for example:
cd ../python ./LM75A.py
Again you can specify address as a command line argument.
Step 8: Retrieving Temperature Using Bash
The bash example is straight forward. Just run it and if needed specify the I2C address as a command line argument, for example:
cd ../bash ./lm75a.sh
Participated in the
Sensors Contest 2016