Skip to content

Commit 1cc0206

Browse files
authoredJan 5, 2022
Handling of multiple I2C network (following #5 issue) (#6)
* Test for #5 issue request (TwoWire handling) * Preparation of 1.2 release following #5 feature request - Adding 2 examples (one with 2 sensors on same I2C, one with 2 sensors on different I2C) - Improving explanations - 1.1 -> 1.2 #5
1 parent acea9c0 commit 1cc0206

File tree

6 files changed

+240
-23
lines changed

6 files changed

+240
-23
lines changed
 

‎LM75A.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* \brief I2C LM75A temperature sensor library (implementation)
33
*
44
* \author Quentin Comte-Gaz <quentin@comte-gaz.com>
5-
* \date 27 December 2021
5+
* \date 05 January 2022
66
* \license MIT License (contact me if too restrictive)
7-
* \copyright Copyright (c) 2021 Quentin Comte-Gaz
8-
* \version 1.1
7+
* \copyright Copyright (c) 2022 Quentin Comte-Gaz
8+
* \version 1.2
99
*/
1010

1111
#include "LM75A.h"
@@ -27,7 +27,13 @@ const int LM75A_REG_ADDR_TEMP = 0;
2727

2828
using namespace LM75AConstValues;
2929

30-
LM75A::LM75A(bool A0_value, bool A1_value, bool A2_value)
30+
LM75A::LM75A(
31+
#ifdef USE_TWO_WIRE_FOR_LM75A
32+
TwoWire* specific_wire,
33+
#endif
34+
bool A0_value,
35+
bool A1_value,
36+
bool A2_value)
3137
{
3238
_i2c_device_address = LM75A_BASE_ADDRESS;
3339

@@ -47,7 +53,12 @@ LM75A::LM75A(bool A0_value, bool A1_value, bool A2_value)
4753
_i2c_device_address += 4;
4854
}
4955

56+
// Select Wire (if one I2C range) or TwoWire (if two I2C range)
57+
#ifdef USE_TWO_WIRE_FOR_LM75A
58+
_specific_wire = specific_wire;
59+
#else
5060
Wire.begin();
61+
#endif
5162
}
5263

5364
float LM75A::fahrenheitToDegrees(float temperature_in_fahrenheit)
@@ -78,6 +89,26 @@ float LM75A::getTemperatureInDegrees() const
7889
uint16_t i2c_received = 0;
7990

8091
// Go to temperature data register
92+
#ifdef USE_TWO_WIRE_FOR_LM75A
93+
_specific_wire->beginTransmission(_i2c_device_address);
94+
_specific_wire->write(LM75A_REG_ADDR_TEMP);
95+
if (_specific_wire->endTransmission())
96+
{
97+
// Transmission error
98+
return real_result;
99+
}
100+
101+
// Get content
102+
if (_specific_wire->requestFrom(_i2c_device_address, 2))
103+
{
104+
_specific_wire->readBytes((uint8_t*)&i2c_received, 2);
105+
}
106+
else
107+
{
108+
// Can't read temperature
109+
return real_result;
110+
}
111+
#else
81112
Wire.beginTransmission(_i2c_device_address);
82113
Wire.write(LM75A_REG_ADDR_TEMP);
83114
if (Wire.endTransmission())
@@ -96,6 +127,7 @@ float LM75A::getTemperatureInDegrees() const
96127
// Can't read temperature
97128
return real_result;
98129
}
130+
#endif
99131

100132
// Modify the value (only 11 MSB are relevant if swapped)
101133
int16_t refactored_value;

‎LM75A.h

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
/*
22
* \brief I2C LM75A temperature sensor library
33
*
4+
* This library can :
5+
* - Show a LM75A sensor temperature in Degrees and Fahrenheit
6+
* - Use multiple LM75A sensors on the same I2C network (by changing A0/A1/A2 LM75A pin state)
7+
* - Use multiple LM75A sensors on default I2C board or using multi-I2C board logic (TwoWire) if available (this doubles the number of usable LM75A on compatible boards)
8+
*
49
* \author Quentin Comte-Gaz <quentin@comte-gaz.com>
5-
* \date 8 July 2016 & 14 January 2018
10+
* \date 05 January 2022
611
* \license MIT License (contact me if too restrictive)
7-
* \copyright Copyright (c) 2016 Quentin Comte-Gaz
8-
* \version 1.1
12+
* \copyright Copyright (c) 2022 Quentin Comte-Gaz
13+
* \version 1.2
914
*/
1015

1116
#ifndef LM75A_h
@@ -17,18 +22,32 @@
1722
#include "WProgram.h"
1823
#endif
1924

25+
#include <Wire.h>
26+
27+
// TODO: Uncomment this line if you want to use TwoWire
28+
// #define USE_TWO_WIRE_FOR_LM75A 1
29+
2030
#define INVALID_LM75A_TEMPERATURE 1000
2131

2232
class LM75A
2333
{
2434
public:
2535
/*!
2636
* \brief LM75A Initialize I2C LM75A Temperature sensor instance
27-
* \param A0_value (bool) A0 Pin value (used for address)
28-
* \param A1_value (bool) A1 Pin value (used for address)
29-
* \param A2_value (bool) A2 Pin value (used for address)
37+
* \param specific_wire (TwoWire*, needed ONLY if USE_TWO_WIRE_FOR_LM75A is defined BEFORE include of this header)
38+
* This wire will be used instead of default board "Wire". It must be initialized with "TwoWire::begin"
39+
* \param A0_value (bool) A0 Pin value (connected to ground = false, this will be used for I2C address)
40+
* \param A1_value (bool) A1 Pin value (connected to ground = false, this will be used for I2C address)
41+
* \param A2_value (bool) A2 Pin value (connected to ground = false, this will be used for I2C address)
3042
*/
31-
LM75A(bool A0_value = false, bool A1_value = false, bool A2_value = false);
43+
LM75A(
44+
#ifdef USE_TWO_WIRE_FOR_LM75A
45+
TwoWire* specific_wire,
46+
#endif
47+
bool A0_value = false,
48+
bool A1_value = false,
49+
bool A2_value = false
50+
);
3251

3352
/*!
3453
* \brief getTemperatureInDegrees Get temperature from LM75A sensor in degrees
@@ -54,6 +73,9 @@ class LM75A
5473

5574
private:
5675
int _i2c_device_address;
76+
#ifdef USE_TWO_WIRE_FOR_LM75A
77+
TwoWire* _specific_wire;
78+
#endif
5779
};
5880

5981
#endif //LM75A_h

‎README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# I2C Temperature Sensor (LM75A) Library for Arduino
22
[![Build Status](https://travis-ci.org/QuentinCG/Arduino-LM75A-Temperature-Sensor-Library.svg?branch=master)](https://travis-ci.org/QuentinCG/Arduino-LM75A-Temperature-Sensor-Library) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/4264aac910e4471584e5af61e65497d4)](https://www.codacy.com/manual/QuentinCG/Arduino-LM75A-Temperature-Sensor-Library?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=QuentinCG/Arduino-LM75A-Temperature-Sensor-Library&amp;utm_campaign=Badge_Grade) [![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://github.com/QuentinCG/Arduino-LM75A-Temperature-Sensor-Library/blob/master/LICENSE.md) [![Donate](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://paypal.me/QuentinCG)
3-
3+
44
## What is it
55

6-
This library (with example) is designed to be integrated in projects using LM75A sensors to get temperature.
6+
This library (with example) is designed to be integrated in projects using LM75A sensors to get temperature with most Arduino boards.
7+
8+
You'll be able to use between 1 and 8 LM75A sensor on a single board (or up to 16 if your board handles multiple I2C).
79

810
<img src="device.png" width="300">
911

1012
## How to install
1113

12-
1) Download <a target="_blank" href="https://github.com/QuentinCG/Arduino-LM75A-Temperature-Sensor-Library/releases/download/1.1.0/LM75A_v1_1_0.zip">latest release</a>
14+
1) Download <a target="_blank" href="https://github.com/QuentinCG/Arduino-LM75A-Temperature-Sensor-Library/releases/download/1.2.0/LM75A_v1_2_0.zip">latest release</a>
1315

1416
2) On your Arduino IDE, click "Sketch" menu and then "Include Library > Add .ZIP Libraries"
1517

@@ -30,14 +32,24 @@ This library (with example) is designed to be integrated in projects using LM75A
3032

3133
<img src="schematics.png" width="400">
3234

35+
(Note: SDA and SCL of your board may change depending on your Arduino board)
36+
3337
## Examples
3438

35-
One example is provided with this library:
39+
Three examples are provided with this library:
3640

37-
### Show temperature in degrees and fahrenheit every second
41+
### Show temperature in degrees and fahrenheit every second (1 sensor)
3842

3943
<a target="_blank" href="https://github.com/QuentinCG/Arduino-LM75A-Temperature-Sensor-Library/blob/master/examples/LM75A_ShowTemperature/LM75A_ShowTemperature.ino">Link to source code</a>
4044

45+
### Show temperature in degrees and fahrenheit every second (2 sensors in same I2C network)
46+
47+
<a target="_blank" href="https://github.com/QuentinCG/Arduino-LM75A-Temperature-Sensor-Library/blob/master/examples/LM75A_ShowTemperature2Sensors/LM75A_ShowTemperature2Sensors.ino">Link to source code</a>
48+
49+
### Show temperature in degrees and fahrenheit every second (2 sensors in different I2C network)
50+
51+
<a target="_blank" href="https://github.com/QuentinCG/Arduino-LM75A-Temperature-Sensor-Library/blob/master/examples/LM75A_MultiI2cWire/LM75A_MultiI2cWire.ino">Link to source code</a>
52+
4153
## License
4254

4355
This project is under MIT license. This means you can use it as you want (just don't delete the library header).
@@ -48,4 +60,5 @@ If you want to add more examples or improve the library, just create a pull requ
4860

4961
## Contributors
5062

51-
Thank you <a target="_blank" href="https://github.com/andrzejboro">andrzejboro</a> for your help on a bug for negative temperature
63+
- Thank you <a target="_blank" href="https://github.com/andrzejboro">andrzejboro</a> for your help on a bug for negative temperature.
64+
- Thank you <a target="_blank" href="https://github.com/damianjwilliams">damianjwilliams</a> for your idea and help on multi I2C handling.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* \brief Show temperature in degrees and fahrenheit every second of 2 LM75A connected on DIFFERENT i2C networks (or 'on different i2c wires')
3+
*
4+
* IMPORTANT NOTE:
5+
* To use this example, you need to uncomment "#define USE_TWO_WIRE_FOR_LM75A 1" in LM75A.h IN the library BEFORE importing it (or edit
6+
* C:\Users\{YOUR_USERNAME_HERE}\Documents\Arduino\libraries\LM75A\LM75A.h if already imported)
7+
*
8+
* \author Quentin Comte-Gaz <quentin@comte-gaz.com>
9+
* \date 05 January 2022
10+
* \license MIT License (contact me if too restrictive)
11+
* \copyright Copyright (c) 2022 Quentin Comte-Gaz
12+
* \version 1.2
13+
*/
14+
15+
#include <LM75A.h>
16+
17+
//Define I2C pins for first I2C wire
18+
#define SDA_1 27
19+
#define SCL_1 26
20+
21+
//Define I2C pins for second I2C wire
22+
#define SDA_2 33
23+
#define SCL_2 32
24+
25+
//Create the two I2C Bus instances
26+
TwoWire I2C_one = TwoWire(0);
27+
TwoWire I2C_two = TwoWire(1);
28+
29+
// Create I2C LM75A instance
30+
LM75A lm75a_from_I2C_one(&I2C_one, // I2C network to use
31+
false, // A0 LM75A pin state (connected to ground = false)
32+
false, // A1 LM75A pin state (connected to ground = false)
33+
false); // A2 LM75A pin state (connected to ground = false)
34+
35+
LM75A lm75a_from_I2C_two(&I2C_two, // I2C network to use
36+
false, // A0 LM75A pin state (connected to ground = false)
37+
false, // A1 LM75A pin state (connected to ground = false)
38+
false); // A2 LM75A pin state (connected to ground = false)
39+
40+
void setup(void)
41+
{
42+
Serial.begin(9600);
43+
44+
Serial.println("Initializing both I2C wires");
45+
I2C_one.begin(SDA_1, SCL_1);
46+
I2C_two.begin(SDA_2, SCL_2);
47+
48+
Serial.println("Temperatures from both sensors from 2 different I2C wires will be displayed every second:");
49+
}
50+
51+
void loop()
52+
{
53+
// Get temperature of both sensors
54+
float temperature_in_degrees_one = lm75a_from_I2C_one.getTemperatureInDegrees();
55+
float temperature_in_degrees_two = lm75a_from_I2C_two.getTemperatureInDegrees();
56+
57+
// Display temperature of first sensor
58+
if (temperature_in_degrees_one == INVALID_LM75A_TEMPERATURE)
59+
{
60+
Serial.println("Error while getting temperature of sensor 1");
61+
}
62+
else
63+
{
64+
Serial.print("Temperature sensor 1: ");
65+
Serial.print(temperature_in_degrees_one);
66+
Serial.print(" degrees (");
67+
Serial.print(LM75A::degreesToFahrenheit(temperature_in_degrees_one));
68+
Serial.println(" fahrenheit)");
69+
}
70+
71+
// Display temperature of second sensor
72+
if (temperature_in_degrees_two == INVALID_LM75A_TEMPERATURE)
73+
{
74+
Serial.println("Error while getting temperature of sensor 2");
75+
}
76+
else
77+
{
78+
Serial.print("Temperature sensor 2: ");
79+
Serial.print(temperature_in_degrees_two);
80+
Serial.print(" degrees (");
81+
Serial.print(LM75A::degreesToFahrenheit(temperature_in_degrees_two));
82+
Serial.println(" fahrenheit)");
83+
}
84+
85+
delay(1000);
86+
}

‎examples/LM75A_ShowTemperature/LM75A_ShowTemperature.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
* \brief Show temperature in degrees and fahrenheit every second
33
*
44
* \author Quentin Comte-Gaz <quentin@comte-gaz.com>
5-
* \date 27 December 2021
5+
* \date 05 January 2022
66
* \license MIT License (contact me if too restrictive)
7-
* \copyright Copyright (c) 2021 Quentin Comte-Gaz
8-
* \version 1.0
7+
* \copyright Copyright (c) 2022 Quentin Comte-Gaz
8+
* \version 1.2
99
*/
1010

1111
#include <LM75A.h>
1212

1313
// Create I2C LM75A instance
14-
LM75A lm75a_sensor(false, //A0 LM75A pin state
15-
false, //A1 LM75A pin state
16-
false); //A2 LM75A pin state
14+
LM75A lm75a_sensor(false, // A0 LM75A pin state (connected to ground = false)
15+
false, // A1 LM75A pin state (connected to ground = false)
16+
false); // A2 LM75A pin state (connected to ground = false)
1717
// Equivalent to "LM75A lm75a_sensor;"
1818

1919
void setup(void)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* \brief Show temperature in degrees and fahrenheit every second from 2 different LM75A sensors on board I2C network
3+
*
4+
* \author Quentin Comte-Gaz <quentin@comte-gaz.com>
5+
* \date 05 January 2022
6+
* \license MIT License (contact me if too restrictive)
7+
* \copyright Copyright (c) 2022 Quentin Comte-Gaz
8+
* \version 1.2
9+
*/
10+
11+
#include <LM75A.h>
12+
13+
// Create I2C LM75A instance
14+
LM75A lm75a_sensor_1(false, // A0 LM75A pin state (connected to ground = false)
15+
false, // A1 LM75A pin state (connected to ground = false)
16+
false); // A2 LM75A pin state (connected to ground = false)
17+
// Equivalent to "LM75A lm75a_sensor_1;"
18+
19+
LM75A lm75a_sensor_2(true, // A0 LM75A pin state (connected to Vcc = true)
20+
false, // A1 LM75A pin state (connected to ground = false)
21+
false); // A2 LM75A pin state (connected to ground = false)
22+
23+
void setup(void)
24+
{
25+
Serial.begin(9600);
26+
Serial.println("Temperatures or 2 LM75A sensors will be displayed every second:");
27+
}
28+
29+
void loop()
30+
{
31+
// Get temperature of both sensors
32+
float temperature_in_degrees_1 = lm75a_sensor_1.getTemperatureInDegrees();
33+
float temperature_in_degrees_2 = lm75a_sensor_2.getTemperatureInDegrees();
34+
35+
// Display temperature of first sensor
36+
if (temperature_in_degrees_1 == INVALID_LM75A_TEMPERATURE)
37+
{
38+
Serial.println("Error while getting temperature of sensor 1");
39+
}
40+
else
41+
{
42+
Serial.print("Temperature of sensor 1: ");
43+
Serial.print(temperature_in_degrees_1);
44+
Serial.print(" degrees (");
45+
Serial.print(LM75A::degreesToFahrenheit(temperature_in_degrees_1));
46+
Serial.println(" fahrenheit)");
47+
}
48+
49+
// Display temperature of second sensor
50+
if (temperature_in_degrees_2 == INVALID_LM75A_TEMPERATURE)
51+
{
52+
Serial.println("Error while getting temperature of sensor 2");
53+
}
54+
else
55+
{
56+
Serial.print("Temperature of sensor 2: ");
57+
Serial.print(temperature_in_degrees_2);
58+
Serial.print(" degrees (");
59+
Serial.print(LM75A::degreesToFahrenheit(temperature_in_degrees_2));
60+
Serial.println(" fahrenheit)");
61+
}
62+
63+
delay(1000);
64+
}

0 commit comments

Comments
 (0)