Skip to content

Commit e53bf48

Browse files
committed
Change I2C address working
1 parent 4ba078a commit e53bf48

File tree

5 files changed

+177
-88
lines changed

5 files changed

+177
-88
lines changed

examples/Example3_Set_Emissivity/Example3_Set_Emissivity.ino

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ void setup()
6565
while (!Serial.available()) ;
6666

6767
if (Serial.available()){
68-
float newEmissivity = 0;
69-
newEmissivity = Serial.parseFloat();
68+
float newEmissivity = Serial.parseFloat();
7069

7170
// Edge-case gaurds
7271
if (newEmissivity < 0.1)
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/******************************************************************************
2+
MLX90614_Set_Address.ino
3+
Configure an MLX90614 to use a new I2C address.
4+
5+
This example demonstrates the setAddress function.
6+
If an MLX90614 is configured for the default address (0x5A),
7+
it'll change the address to a new address (0x5B).
8+
If the Arduino can communicate with an MLX90614 on the new
9+
address, it'll print the device's ID registers.
10+
11+
Note: for the new address to take effect, you need to cycle
12+
power to the MLX90614.
13+
14+
Hardware Hookup (if you're not using the eval board):
15+
MLX90614 ------------- Arduino
16+
VDD ------------------ 3.3V
17+
VSS ------------------ GND
18+
SDA ------------------ SDA (A4 on older boards)
19+
SCL ------------------ SCL (A5 on older boards)
20+
21+
Jim Lindblom @ SparkFun Electronics
22+
October 23, 2015
23+
https://github.com/sparkfun/SparkFun_MLX90614_Arduino_Library
24+
25+
Development environment specifics:
26+
Arduino 1.6.5
27+
SparkFun IR Thermometer Evaluation Board - MLX90614
28+
******************************************************************************/
29+
30+
#include <Wire.h> // I2C library, required for MLX90614
31+
#include <SparkFunMLX90614.h> // SparkFunMLX90614 Arduino library
32+
33+
IRTherm therm; // Create an IRTherm object to interact with throughout
34+
35+
void setup()
36+
{
37+
Serial.begin(115200); // Initialize Serial to log output
38+
Wire.begin(); // Join I2C bus
39+
40+
// check if qwiic IR thermometer will acknowledge over I2C
41+
if (therm.begin() == false) {
42+
Serial.println("Qwiic IR thermometer did not acknowledge! Running I2C scanner.");
43+
}
44+
45+
else {
46+
Serial.println("Device acknowledged!");
47+
48+
Serial.println();
49+
Serial.println("Enter a new I2C address for the Qwiic IR Thermometer to use!");
50+
Serial.println("Don't use the 0x prefix. For instance, if you wanted to ");
51+
Serial.println("change the address to 0x5B, you would type 5B in the serial");
52+
Serial.println("input bar and press enter.");
53+
Serial.println();
54+
Serial.println("One more thing! Make sure your line ending is set to 'Both NL & CR'");
55+
Serial.println("in the Serial Monitor.");
56+
Serial.println();
57+
Serial.println("Note, for the new I2C address to take effect you need to power cycle");
58+
Serial.println("the MLX90614.");
59+
60+
// Wait for serial data to be available
61+
while (Serial.available() == 0);
62+
63+
if (Serial.available()) {
64+
uint8_t newAddress = 0;
65+
String stringBuffer = Serial.readString();
66+
char charBuffer[10];
67+
stringBuffer.toCharArray(charBuffer, 10);
68+
uint8_t success = sscanf(charBuffer, "%x", &newAddress);
69+
70+
if (success) {
71+
if (newAddress > 0x08 && newAddress < 0x77) {
72+
Serial.println("Character received and device address is valid!");
73+
Serial.print("Attempting to set device address to 0x");
74+
Serial.println(newAddress, HEX);
75+
76+
if (therm.setAddress(newAddress) == true) {
77+
Serial.println("Device address set succeeded!");
78+
}
79+
else {
80+
Serial.println("Device address set failed!");
81+
}
82+
83+
delay(100); // give the hardware time to do whatever configurations it needs
84+
85+
if (therm.isConnected()) {
86+
Serial.println("Device acknowledged on new I2C address!");
87+
} else {
88+
Serial.println("Device did not acknowledge on new I2C address!");
89+
}
90+
}
91+
else {
92+
Serial.println("Address out of range! Try an address between 0x08 and 0x77");
93+
}
94+
}
95+
else {
96+
Serial.println("Invalid text, try again.");
97+
}
98+
}
99+
delay(100);
100+
}
101+
// byte address;
102+
// if (!therm.readID()) // Try reading the ID registers
103+
// {
104+
// // If the read fails, try re-initializing with the
105+
// // new address. Maybe we've already set it.
106+
// therm.begin(newAddress);
107+
//
108+
// if (therm.readID()) // Read from the ID registers
109+
// { // If the read succeeded, print the ID:
110+
// Serial.println("Communicated with new address.");
111+
// Serial.println("ID: 0x" +
112+
// String(therm.getIDH(), HEX) +
113+
// String(therm.getIDL(), HEX));
114+
// }
115+
// else
116+
// {
117+
// Serial.println("Failed to communicate with either address.");
118+
// }
119+
// }
120+
// else
121+
// {
122+
// // If the read suceeds, change the address to something
123+
// // new.
124+
// if (!therm.setAddress(newAddress))
125+
// {
126+
// Serial.println("Failed to set new address.");
127+
// }
128+
// else
129+
// {
130+
// Serial.println("Set the address to 0x" + String(newAddress, HEX));
131+
// Serial.println("Cycle power to try it out.");
132+
// }
133+
// }
134+
}
135+
136+
void loop()
137+
{
138+
// If no I2C device found or Qwiic IR thermometer is set to new address,
139+
// scan for available I2C devices
140+
byte error, address;
141+
int nDevices;
142+
143+
Serial.println("Scanning...");
144+
145+
nDevices = 0;
146+
for (address = 1; address < 127; address++) {
147+
// The i2c_scanner uses the return value of the Write.endTransmission()
148+
// to see if a device ddi acknowledge to the address.
149+
Wire.beginTransmission(address);
150+
error = Wire.endTransmission();
151+
152+
if (error == 0) {
153+
Serial.print("I2C device found at address 0x");
154+
if (address < 16)
155+
Serial.print("0");
156+
Serial.print(address, HEX);
157+
Serial.println(" !");
158+
159+
nDevices++;
160+
}
161+
else if (error == 4) {
162+
Serial.print("Unknown error at address 0x");
163+
if (address < 16)
164+
Serial.print("0");
165+
Serial.print(address, HEX);
166+
}
167+
}
168+
if (nDevices == 0)
169+
Serial.println("No I2C devices found\n");
170+
else
171+
Serial.println("done\n");
172+
173+
delay(5000); // Wait 5 seconds for next scan
174+
}

examples/MLX90614_Set_Address/MLX90614_Set_Address.ino

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/SparkFunMLX90614.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ uint8_t IRTherm::readAddress()
221221
return 0; // Else return fail
222222
}
223223

224-
uint8_t IRTherm::setAddress(uint8_t newAdd)
224+
bool IRTherm::setAddress(uint8_t newAdd)
225225
{
226226
int16_t tempAdd;
227227
// Make sure the address is within the proper range:

src/SparkFunMLX90614.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class IRTherm
122122
// The <newAdd> parameter should be a value between 0x01 and 0x7F.
123123
// The function returns 1 on success and 0 on failure.
124124
// The new address won't take effect until the device is reset.
125-
uint8_t setAddress(uint8_t newAdd);
125+
bool setAddress(uint8_t newAdd);
126126

127127
// readID() reads the 64-bit ID of the MLX90614.
128128
// Return value is either 1 on success or 0 on failure.

0 commit comments

Comments
 (0)