Skip to content

Commit a28e8cd

Browse files
committed
Pass in wire to begin function
1 parent 5d03e7d commit a28e8cd

File tree

5 files changed

+58
-48
lines changed

5 files changed

+58
-48
lines changed
File renamed without changes.

examples/MLX90614_Get_ID/MLX90614_Get_ID.ino

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ IRTherm therm; // Create an IRTherm object to interact with throughout
2828

2929
void setup()
3030
{
31-
Serial.begin(9600); // Initialize Serial to log output
32-
therm.begin(); // Initialize the MLX90614
31+
Serial.begin(115200); // Initialize Serial to log output
32+
Wire.begin(); //Join I2C bus
33+
34+
if (therm.begin() == false){ // Initialize the MLX90614
35+
Serial.println("Qwiic IR thermometer did not acknowledge! Freezing!");
36+
while(1);
37+
}
38+
Serial.println("Qwiic IR thermometer acknowledged.");
3339

3440
if (therm.readID()) // Read from the ID registers
3541
{ // If the read succeeded, print the ID:

examples/MLX90614_Serial_Demo/MLX90614_Serial_Demo.ino

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,40 @@ SparkFun IR Thermometer Evaluation Board - MLX90614
2929

3030
IRTherm therm; // Create an IRTherm object to interact with throughout
3131

32-
const byte LED_PIN = 8; // Optional LED attached to pin 8 (active low)
33-
3432
void setup()
3533
{
36-
Serial.begin(9600); // Initialize Serial to log output
37-
therm.begin(); // Initialize thermal IR sensor
34+
Serial.begin(115200); // Initialize Serial to log output
35+
Wire.begin(); //Joing I2C bus
36+
37+
if (therm.begin() == false){ // Initialize thermal IR sensor
38+
Serial.println("Qwiic IR thermometer did not acknowledge! Freezing!");
39+
while(1);
40+
}
41+
Serial.println("Qwiic IR Thermometer did acknowledge.");
42+
3843
therm.setUnit(TEMP_F); // Set the library's units to Farenheit
3944
// Alternatively, TEMP_F can be replaced with TEMP_C for Celsius or
4045
// TEMP_K for Kelvin.
4146

42-
pinMode(LED_PIN, OUTPUT); // LED pin as output
43-
setLED(LOW); // LED OFF
47+
pinMode(LED_BUILTIN, OUTPUT); // LED pin as output
4448
}
4549

4650
void loop()
4751
{
48-
setLED(HIGH); //LED on
49-
52+
digitalWrite(LED_BUILTIN, HIGH);
53+
5054
// Call therm.read() to read object and ambient temperatures from the sensor.
5155
if (therm.read()) // On success, read() will return 1, on fail 0.
5256
{
5357
// Use the object() and ambient() functions to grab the object and ambient
5458
// temperatures.
5559
// They'll be floats, calculated out to the unit you set with setUnit().
5660
Serial.print("Object: " + String(therm.object(), 2));
57-
Serial.write('°'); // Degree Symbol
5861
Serial.println("F");
5962
Serial.print("Ambient: " + String(therm.ambient(), 2));
60-
Serial.write('°'); // Degree Symbol
6163
Serial.println("F");
6264
Serial.println();
6365
}
64-
setLED(LOW);
66+
digitalWrite(LED_BUILTIN, LOW);
6567
delay(500);
6668
}
67-
68-
void setLED(bool on)
69-
{
70-
if (on)
71-
digitalWrite(LED_PIN, LOW);
72-
else
73-
digitalWrite(LED_PIN, HIGH);
74-
}

src/SparkFunMLX90614.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ IRTherm::IRTherm()
2828
_rawMin = 0;
2929
}
3030

31-
uint8_t IRTherm::begin(uint8_t address)
31+
bool IRTherm::begin(uint8_t address, TwoWire &wirePort)
3232
{
3333
_deviceAddress = address; // Store the address in a private member
34+
_i2cPort = &wirePort;
3435

35-
Wire.begin(); // Initialize I2C
36-
//! TODO: read a register, return success only if the register
37-
//! produced a known-good value.
38-
return 1; // Return success
36+
return (isConnected());
37+
}
38+
39+
bool IRTherm::isConnected()
40+
{
41+
_i2cPort->beginTransmission(_deviceAddress);
42+
if (_i2cPort->endTransmission() == 0)
43+
return true;
44+
return false;
3945
}
4046

4147
void IRTherm::setUnit(temperature_units unit)
@@ -267,10 +273,10 @@ uint8_t IRTherm::sleep(void)
267273
crc = crc8(crc, MLX90614_REGISTER_SLEEP);
268274

269275
// Manually send the sleep command:
270-
Wire.beginTransmission(_deviceAddress);
271-
Wire.write(MLX90614_REGISTER_SLEEP);
272-
Wire.write(crc);
273-
Wire.endTransmission(true);
276+
_i2cPort->beginTransmission(_deviceAddress);
277+
_i2cPort->write(MLX90614_REGISTER_SLEEP);
278+
_i2cPort->write(crc);
279+
_i2cPort->endTransmission(true);
274280

275281
// Set the SCL pin LOW, and SDA pin HIGH (should be pulled up)
276282
pinMode(SCL, OUTPUT);
@@ -281,7 +287,7 @@ uint8_t IRTherm::sleep(void)
281287
uint8_t IRTherm::wake(void)
282288
{
283289
// Wake operation from datasheet
284-
Wire.endTransmission(true); // stop i2c bus transmission BEFORE sending wake up request
290+
_i2cPort->endTransmission(true); // stop i2c bus transmission BEFORE sending wake up request
285291
pinMode(SCL, INPUT); // SCL high
286292
pinMode(SDA, OUTPUT);
287293
digitalWrite(SDA, LOW); // SDA low
@@ -293,7 +299,7 @@ uint8_t IRTherm::wake(void)
293299
digitalWrite(SCL, LOW); // SCL low
294300
delay(10); // Delay at least 1.44ms
295301
pinMode(SCL, INPUT); // SCL high
296-
Wire.beginTransmission(_deviceAddress); // reactivate i2c bus transmission AFTER sending wake up request
302+
_i2cPort->beginTransmission(_deviceAddress); // reactivate i2c bus transmission AFTER sending wake up request
297303
}
298304

299305
int16_t IRTherm::calcRawTemp(float calcTemp)
@@ -355,15 +361,15 @@ float IRTherm::calcTemperature(int16_t rawTemp)
355361

356362
uint8_t IRTherm::I2CReadWord(byte reg, int16_t * dest)
357363
{
358-
Wire.beginTransmission(_deviceAddress);
359-
Wire.write(reg);
364+
_i2cPort->beginTransmission(_deviceAddress);
365+
_i2cPort->write(reg);
360366

361-
Wire.endTransmission(false); // Send restart
362-
Wire.requestFrom(_deviceAddress, (uint8_t) 3);
367+
_i2cPort->endTransmission(false); // Send restart
368+
_i2cPort->requestFrom(_deviceAddress, (uint8_t) 3);
363369

364-
uint8_t lsb = Wire.read();
365-
uint8_t msb = Wire.read();
366-
uint8_t pec = Wire.read();
370+
uint8_t lsb = _i2cPort->read();
371+
uint8_t msb = _i2cPort->read();
372+
uint8_t pec = _i2cPort->read();
367373

368374
uint8_t crc = crc8(0, (_deviceAddress << 1));
369375
crc = crc8(crc, reg);
@@ -409,12 +415,12 @@ uint8_t IRTherm::I2CWriteWord(byte reg, int16_t data)
409415
crc = crc8(crc, lsb);
410416
crc = crc8(crc, msb);
411417

412-
Wire.beginTransmission(_deviceAddress);
413-
Wire.write(reg);
414-
Wire.write(lsb);
415-
Wire.write(msb);
416-
Wire.write(crc);
417-
return Wire.endTransmission(true);
418+
_i2cPort->beginTransmission(_deviceAddress);
419+
_i2cPort->write(reg);
420+
_i2cPort->write(lsb);
421+
_i2cPort->write(msb);
422+
_i2cPort->write(crc);
423+
return _i2cPort->endTransmission(true);
418424
}
419425

420426
uint8_t IRTherm::crc8 (uint8_t inCrc, uint8_t inData)

src/SparkFunMLX90614.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ SparkFun IR Thermometer Evaluation Board - MLX90614
2424
// Default I2C PIN for non Atmega Boards //
2525
///////////////////////////////////////////
2626
#ifndef SDA
27-
#define SDA (digitalPinToPinName(PIN_WIRE_SDA))
27+
#define SDA PIN_WIRE_SDA
2828
#endif
2929
#ifndef SCL
30-
#define SCL (digitalPinToPinName(PIN_WIRE_SCL))
30+
#define SCL PIN_WIRE_SCL
3131
#endif
3232

3333
//////////////////////////////////
@@ -74,7 +74,9 @@ class IRTherm
7474
// communication with an MLX90614 device at the specified 7-bit I2C
7575
// address.
7676
// If no parameter is supplied, the default MLX90614 address is used.
77-
uint8_t begin(uint8_t address = MLX90614_DEFAULT_ADDRESS);
77+
bool begin(uint8_t address = MLX90614_DEFAULT_ADDRESS, TwoWire &wirePort = Wire);
78+
79+
bool isConnected();
7880

7981
// setUnit(<unit>) configures the units returned by the ambient(),
8082
// object(), minimum() and maximum() functions, and it determines what
@@ -156,6 +158,8 @@ class IRTherm
156158

157159
private:
158160
uint8_t _deviceAddress; // MLX90614's 7-bit I2C address
161+
TwoWire *_i2cPort;
162+
159163
temperature_units _defaultUnit; // Keeps track of configured temperature unit
160164

161165
// These keep track of the raw temperature values read from the sensor:

0 commit comments

Comments
 (0)