You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+20-19Lines changed: 20 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1211,37 +1211,35 @@ const r = require('array-gpio');
1211
1211
1212
1212
let i2c =r.startI2C(); // using SDA1 and SCL1 (pin 3 & 5) pins
1213
1213
1214
-
/*set data transfer speed to 400 kHz */
1215
-
i2c.setTransferSpeed(400000);
1214
+
/*Set data transfer speed to 200 kHz */
1215
+
i2c.setTransferSpeed(200000);
1216
1216
1217
1217
/* MCP9808 hardware device address */
1218
1218
let addr =0x18;
1219
1219
1220
-
/*select the MCP9808 device for data trasfer */
1220
+
/*Select the MCP9808 device for data trasfer */
1221
1221
i2c.selectSlave(addr);
1222
1222
1223
-
/*setup the application read and write data buffer */
1223
+
/*Setup the application read and write data buffer */
1224
1224
constwbuf=Buffer.alloc(16); // write buffer
1225
1225
constrbuf=Buffer.alloc(16); // read buffer
1226
1226
1227
-
/*accessing the internal 16-bit manufacturer ID register within MCP9808 */
1227
+
/*Accessing the internal 16-bit manufacturer ID register within MCP9808 */
1228
1228
wbuf[0] =0x06; // from the MCP9808 datasheet, set the address of the manufacturer ID register to the write buffer
1229
1229
i2c.write(wbuf, 1); // writes 1 data byte to the slave device selecting the MCP9808 manufacturer ID register for data access
1230
1230
1231
-
/*master (rpi) device will now read the content of the 16-bit manufacturer ID register (should be 0x54 as per datasheet) */
1232
-
/*reading 2 data bytes - the upper byte (rbuf[0]) and lower byte (rbuf[1]) from the manufacturer ID register, ID value is on the lower byte from the datasheet */
1231
+
/*Master (rpi) device will now read the content of the 16-bit manufacturer ID register (should be 0x54 as per datasheet) */
1232
+
/*Reading 2 data bytes - the upper byte (rbuf[0]) and lower byte (rbuf[1]) from the manufacturer ID register, ID value is on the lower byte from the datasheet */
1233
1233
i2c.read(rbuf, 2);
1234
1234
1235
1235
console.log('MCP9808 ID: ', rbuf[1].toString(16)); // convert the ID value to hex value
1236
1236
1237
1237
/* Based on MCP9808 datasheet, compute the temperature data as follows */
1238
1238
functiongetTemp(){
1239
1239
1240
-
/* variable for temperature data */
1241
-
let Temp;
1242
-
1243
-
let UpperByte = rbuf[0];
1244
-
let LowerByte = rbuf[1];
1240
+
let Temp =null;
1241
+
let UpperByte = rbuf[0]; // MSB
1242
+
let LowerByte = rbuf[1]; // LSB
1245
1243
1246
1244
UpperByte = UpperByte &0x1F; // Clear flag bits
1247
1245
@@ -1251,33 +1249,36 @@ function getTemp(){
1251
1249
Temp =256- ((UpperByte *16) + (LowerByte /16));
1252
1250
1253
1251
/* Temp > 0 C */
1254
-
}else {
1252
+
}
1253
+
else {
1255
1254
Temp = ((UpperByte *16) + (LowerByte /16));
1256
1255
}
1257
1256
1258
1257
/* Print out temperature data */
1259
1258
console.log('Temp: ', Temp);
1260
1259
1260
+
return Temp;
1261
+
1261
1262
}
1262
1263
1263
-
/*get temperature readings every 2 seconds */
1264
+
/*Get temperature readings every 2 seconds */
1264
1265
setInterval( function(){
1265
-
/*accessing the internal 16-bit configuration register within MCP9808
1266
+
/*Accessing the internal 16-bit configuration register within MCP9808.
1266
1267
You can skip accessing this register using default settings */
1267
1268
wbuf[0] =0x01; // address of the configuration register
1268
-
/*change content of configuration register */
1269
+
/*Change content of configuration register */
1269
1270
wbuf[1] =0x02; // register upper byte, THYST set with +1.5 C
1270
1271
wbuf[2] =0x00; // register lower byte (power up defaults)
1271
1272
i2c.write(wbuf, 3);
1272
1273
1273
-
/*accessing the internal 16-bit ambient temp register within MCP9808 */
1274
+
/*Accessing the internal 16-bit ambient temp register within MCP9808 */
1274
1275
wbuf[0] =0x05; // address of ambient temperature register
1275
1276
i2c.write(wbuf, 1);
1276
1277
1277
-
/*read the content of ambient temp register */
1278
+
/*Read the content of ambient temp register */
1278
1279
i2c.read(rbuf, 2); // read the UpperByte and LowerByte data
1279
1280
1280
-
/*get temperature data and print out the results */
1281
+
/*Get temperature data and print out the results */
0 commit comments