Microbit and TMP175 digital temperature sensor example

The TMP75 and TMP175 devices are digital temperature sensors ideal for NTC and PTC thermistor replacement. The devices offer a typical accuracy of ±1°C without requiring calibration or external component signal conditioning. IC temperature sensors are highly linear and do not require complex calculations or look-up tables to derive the temperature. The on-chip 12-bit ADC offers resolutions down to 0.0625°C. The devices are available in the industry standard LM75 SOIC-8 and MSOP-8 footprint.

The TMP175 and TMP75 feature SMBus, Two-Wire, and I2C interface compatibility. The TMP175 device allows up to 27 devices on one bus. The TMP75 allows up to eight 8 on one bus. The TMP175 and TMP75 both feature an SMBus Alert function.

The TMP175 and TMP75 devices are ideal for extended temperature measurement in a variety of communication, computer, consumer, environmental, industrial, and instrumentation applications.

Features

  • TMP175: 27 Addresses
  • TMP75: 8 Addresses, NIST Traceable
  • Digital Output: SMBus™, Two-Wire™, and I2C
    Interface Compatibility
  • Resolution: 9 to 12 Bits, User-Selectable
  • Accuracy:
    • ±1°C (Typical) from –40°C to 125°C
    • ±2°C (Maximum) from –40°C to 125°C
  • Low Quiescent Current: 50-µA, 0.1-µA Standby
  • Wide Supply Range: 2.7 V to 5.5 V
  • Small 8-Pin MSOP and 8-Pin SOIC Packages

 

Connection

Module Connection Microbit Connection
VCC 3v3
GND Gnd
SDA SDA – 20
SCL SCL – 19

 

Code

#include <Wire.h> 
 
byte TempHi;              // Variable hold data high byte
byte TempLo;              // Variable hold data low byte
boolean P_N;              // Bit flag for Positive and Negative
unsigned int Decimal;     // Variable hold decimal value
 
void Cal_Temp();
/*******************************************************************************
                      Setup
*******************************************************************************/ 
void setup() 
{ 
  Serial.begin(9600);
  Wire.begin();             // join i2c bus (address optional for master) 
  delay(1000);
} 
 
/*******************************************************************************
                      Main Loop
*******************************************************************************/  
void loop() 
{
  const int I2C_address = 0x37;  // I2C write address 
 
  delay(100);
  Wire.beginTransmission(I2C_address);
  Wire.write(1);             // Setup configuration register
  Wire.write(0x60);          // 12-bit
  Wire.endTransmission(); 
 
  Wire.beginTransmission(I2C_address);
  Wire.write((byte)0);             // Setup Pointer Register to 0
  Wire.endTransmission(); 
 
  while (1)
  {
    delay(1000);
 
    // Read temperature value
    Wire.requestFrom(I2C_address, 2);
    while(Wire.available())          // Checkf for data from slave
    {                                
      TempHi = Wire.read();       // Read temperature high byte
      TempLo = Wire.read();       // Read temperature low byte
    } 
    Cal_Temp ();
 
    // Display temperature
    Serial.print("The temperature is ");
    if (P_N == 0)
      Serial.print("-");
    Serial.print(TempHi,DEC);
    Serial.print(".");
    Serial.print(Decimal,DEC);
    Serial.println(" degree C");
  }  
}
 
void Cal_Temp()
{
  if (TempHi&0x80)          // If bit7 of the TempHi is HIGH then the temperature is negative
    P_N = 0;
  else                      // Else the temperature is positive
    P_N = 1;
 
  TempHi = TempHi & 0x7F;   // Remove sign
  TempLo = TempLo & 0xF0;   // Filter out last nibble
  TempLo = TempLo >>4;      // Shift right 4 times
  Decimal = TempLo;
  Decimal = Decimal * 625;  // Each bit = 0.0625 degree C
 
}

 

Output

Open the serial monitor and you will see something like this

The temperature is 23.5000 degree C
The temperature is 23.5625 degree C
The temperature is 25.3125 degree C
The temperature is 26.7500 degree C
The temperature is 27.8125 degree C
The temperature is 28.5000 degree C
The temperature is 28.9375 degree C
The temperature is 29.3750 degree C
The temperature is 29.6875 degree C
The temperature is 29.9375 degree C
The temperature is 30.1250 degree C
The temperature is 30.3125 degree C

 

Link

1pcs CJMCU-175 TMP175 27 Address Digital Temperature Sensor

Microbit and MS5611 barometric pressure sensor example

This barometric pressure sensor is optimized for altimeters and variometers with an altitude resolution of 10 cm. The sensor module includes a high linearity pressure sensor and an ultra-low power 24 bit ΔΣ ADC with internal factory calibrated coefficients. It provides a precise digital 24 Bit pressure and temperature value and different operation modes that allow the user to optimize for conversion speed and current consumption. A high resolution temperature output allows the implementation of an altimeter/thermometer function without any additional sensor. The MS5611-01BA can be interfaced to virtually any microcontroller. The communication protocol is simple, without the need of programming internal registers in the device.

Small dimensions of only 5.0 mm x 3.0 mm and a height of only 1.0 mm allow for integration in mobile devices. This new sensor module generation is based on leading MEMS technology and latest benefits from MEAS Switzerland proven experience and know-how in high volume manufacturing of altimeter modules, which have been widely used for over a decade. The sensing principle employed leads to very low hysteresis and high stability of both pressure and temperature signal.

features

 

  • High resolution module, 10 cm
  • Fast conversion down to 1 ms
  • Low power, 1 µA (standby < 0.15 µA)
  • QFN package 5.0 x 3.0 x 1.0 mm3
  • Supply voltage 1.8 to 3.6 V
  • Integrated digital pressure sensor (24 bit ΔΣ ADC)
  • Operating range: 10 to 1200 mbar, -40 to +85 °C
  • I2C and SPI interface up to 20 MHz
  • No external components (Internal oscillator)
  • Excellent long term stability

 

 

Connection

Microbit Module connection
3.3v Vcc
Gnd Gnd
SCL – 19 SCL
SDA – 20 SDA

 

Code

This example comes from the https://github.com/jarzebski/Arduino-MS5611 library

#include <Wire.h>
#include <MS5611.h>
 
MS5611 ms5611;
 
double referencePressure;
 
void setup() 
{
  Serial.begin(9600);
 
  // Initialize MS5611 sensor
  Serial.println("Initialize MS5611 Sensor");
 
  while(!ms5611.begin())
  {
    Serial.println("Could not find a valid MS5611 sensor, check wiring!");
    delay(500);
  }
 
  // Get reference pressure for relative altitude
  referencePressure = ms5611.readPressure();
 
  // Check settings
  checkSettings();
}
 
void checkSettings()
{
  Serial.print("Oversampling: ");
  Serial.println(ms5611.getOversampling());
}
 
void loop()
{
  // Read raw values
  uint32_t rawTemp = ms5611.readRawTemperature();
  uint32_t rawPressure = ms5611.readRawPressure();
 
  // Read true temperature & Pressure
  double realTemperature = ms5611.readTemperature();
  long realPressure = ms5611.readPressure();
 
  // Calculate altitude
  float absoluteAltitude = ms5611.getAltitude(realPressure);
  float relativeAltitude = ms5611.getAltitude(realPressure, referencePressure);
 
  Serial.println("--");
 
  Serial.print(" rawTemp = ");
  Serial.print(rawTemp);
  Serial.print(", realTemp = ");
  Serial.print(realTemperature);
  Serial.println(" *C");
 
  Serial.print(" rawPressure = ");
  Serial.print(rawPressure);
  Serial.print(", realPressure = ");
  Serial.print(realPressure);
  Serial.println(" Pa");
 
  Serial.print(" absoluteAltitude = ");
  Serial.print(absoluteAltitude);
  Serial.print(" m, relativeAltitude = ");
  Serial.print(relativeAltitude);    
  Serial.println(" m");
 
  delay(1000);
}

 

Output

Open the serial monitor and you will see something like this

rawTemp = 8432348, realTemp = 24.08 *C
rawPressure = 8669780, realPressure = 101367 Pa
absoluteAltitude = -3.50 m, relativeAltitude = -0.17 m

rawTemp = 8476892, realTemp = 25.56 *C
rawPressure = 8654596, realPressure = 101383 Pa
absoluteAltitude = -4.83 m, relativeAltitude = -1.50 m

rawTemp = 8553596, realTemp = 28.06 *C
rawPressure = 8628308, realPressure = 101383 Pa
absoluteAltitude = -4.83 m, relativeAltitude = -1.50 m

rawTemp = 8595300, realTemp = 29.42 *C
rawPressure = 8613628, realPressure = 101374 Pa
absoluteAltitude = -4.08 m, relativeAltitude = -0.75 m

 

Link

GY-63 MS5611-01BA03 Precision MS5611 Atmospheric Pressure Sensor Module Height Sensor Module

OPT3001 Digital Ambient Light Sensor and Microbit example

The OPT3001 is a sensor that measures the intensity of visible light. The spectral response of the sensor tightly matches the photopic response of the human eye and includes significant infrared rejection.

The OPT3001 is a single-chip lux meter, measuring the intensity of light as visible by the human eye. The precision spectral response and strong IR rejection of the device enables the OPT3001 to accurately meter the intensity of light as seen by the human eye regardless of light source. The strong IR rejection also aids in maintaining high accuracy when industrial design calls for mounting the sensor under dark glass for aesthetics. The OPT3001 is designed for systems that create light-based experiences for humans, and an ideal preferred replacement for photodiodes, photoresistors, or other ambient light sensors with less human eye matching and IR rejection.

Measurements can be made from 0.01 lux up to 83k lux without manually selecting full-scale ranges by using the built-in, full-scale setting feature. This capability allows light measurement over a 23-bit effective dynamic range.

The digital operation is flexible for system integration. Measurements can be either continuous or single-shot. The control and interrupt system features autonomous operation, allowing the processor to sleep while the sensor searches for appropriate wake-up events to report via the interrupt pin. The digital output is reported over an I2C- and SMBus-compatible, two-wire serial interface.

The low power consumption and low power-supply voltage capability of the OPT3001 enhance the battery life of battery-powered systems.

Features

  • Precision Optical Filtering to Match Human Eye:
    • Rejects > 99% (typ) of IR
  • Automatic Full-Scale Setting Feature Simplifies Software and Ensures Proper Configuration
  • Measurements: 0.01 lux to 83 k lux
  • 23-Bit Effective Dynamic Range With
    Automatic Gain Ranging
  • 12 Binary-Weighted Full-Scale Range Settings:
    < 0.2% (typ) Matching Between Ranges
  • Low Operating Current: 1.8 µA (typ)
  • Operating Temperature Range: –40°C to +85°C
  • Wide Power-Supply Range: 1.6 V to 3.6 V
  • 5.5-V Tolerant I/O

Connection

Microbit CJMCU-3001
3.3v Vcc
Gnd Gnd
SDA – 20 SDA
SCL – 19 SCL

Code

This example uses the following library https://github.com/closedcube/ClosedCube_OPT3001_Arduino

/*
 
This is example for ClosedCube OPT3001 Digital Ambient Light Sensor breakout board 
 
Initial Date: 02-Dec-2015
 
Hardware connections for Arduino Uno:
VDD to 3.3V DC
SDA to A4
SCL to A5
GND to common ground
 
Written by AA for ClosedCube
 
MIT License
 
*/
 
#include <Wire.h>
#include <ClosedCube_OPT3001.h>
 
ClosedCube_OPT3001 opt3001;
 
#define OPT3001_ADDRESS 0x44
 
void setup()
{
	Serial.begin(9600);
	Serial.println("ClosedCube OPT3001 Arduino Test");
 
	opt3001.begin(OPT3001_ADDRESS);
	Serial.print("OPT3001 Manufacturer ID");
	Serial.println(opt3001.readManufacturerID());
	Serial.print("OPT3001 Device ID");
	Serial.println(opt3001.readDeviceID());
 
	configureSensor();
	printResult("High-Limit", opt3001.readHighLimit());
	printResult("Low-Limit", opt3001.readLowLimit());
	Serial.println("----");
}
 
void loop()
{
	OPT3001 result = opt3001.readResult();
	printResult("OPT3001", result);
	delay(500);
}
 
void configureSensor() {
	OPT3001_Config newConfig;
 
	newConfig.RangeNumber = B1100;	
	newConfig.ConvertionTime = B0;
	newConfig.Latch = B1;
	newConfig.ModeOfConversionOperation = B11;
 
	OPT3001_ErrorCode errorConfig = opt3001.writeConfig(newConfig);
	if (errorConfig != NO_ERROR)
		printError("OPT3001 configuration", errorConfig);
	else {
		OPT3001_Config sensorConfig = opt3001.readConfig();
		Serial.println("OPT3001 Current Config:");
		Serial.println("------------------------------");
 
		Serial.print("Conversion ready (R):");
		Serial.println(sensorConfig.ConversionReady,HEX);
 
		Serial.print("Conversion time (R/W):");
		Serial.println(sensorConfig.ConvertionTime, HEX);
 
		Serial.print("Fault count field (R/W):");
		Serial.println(sensorConfig.FaultCount, HEX);
 
		Serial.print("Flag high field (R-only):");
		Serial.println(sensorConfig.FlagHigh, HEX);
 
		Serial.print("Flag low field (R-only):");
		Serial.println(sensorConfig.FlagLow, HEX);
 
		Serial.print("Latch field (R/W):");
		Serial.println(sensorConfig.Latch, HEX);
 
		Serial.print("Mask exponent field (R/W):");
		Serial.println(sensorConfig.MaskExponent, HEX);
 
		Serial.print("Mode of conversion operation (R/W):");
		Serial.println(sensorConfig.ModeOfConversionOperation, HEX);
 
		Serial.print("Polarity field (R/W):");
		Serial.println(sensorConfig.Polarity, HEX);
 
		Serial.print("Overflow flag (R-only):");
		Serial.println(sensorConfig.OverflowFlag, HEX);
 
		Serial.print("Range number (R/W):");
		Serial.println(sensorConfig.RangeNumber, HEX);
 
		Serial.println("------------------------------");
	}
 
}
 
void printResult(String text, OPT3001 result) {
	if (result.error == NO_ERROR) {
		Serial.print(text);
		Serial.print(": ");
		Serial.print(result.lux);
		Serial.println(" lux");
	}
	else {
		printError(text,result.error);
	}
}
 
void printError(String text, OPT3001_ErrorCode error) {
	Serial.print(text);
	Serial.print(": [ERROR] Code #");
	Serial.println(error);
}

Output

Here is a sample of the output from the Serial Monitor

OPT3001: 7910.40 lux
OPT3001: 8017.92 lux
OPT3001: 7969.28 lux
OPT3001: 454.40 lux
OPT3001: 125.28 lux
OPT3001: 192.88 lux
OPT3001: 247.76 lux
OPT3001: 252.56 lux
OPT3001: 256.48 lux
OPT3001: 256.80 lux
OPT3001: 5.44 lux
OPT3001: 6.09 lux
OPT3001: 6.81 lux
OPT3001: 38.48 lux
OPT3001: 229.84 lux
OPT3001: 233.68 lux

 

Link

OPT3001 CJMCU-3001 ambient light sensor eye like measurement light intensity single chip illumination meter

Microbit and MAG3110 magnetic sensor example

The MAG3110 is a small, low-power digital 3D magnetic sensor with a wide dynamic range to allow operation in PCBs with high extraneous magnetic fields. The MAG3110:

  • Measures the components of the local magnetic field, the sum of the geomagnetic field and the magnetic field created by components on the circuit board
  • Can be used in conjunction with a 3-axis accelerometer so that orientation-independent accurate compass heading information may be achieved
  • Features a standard I²C serial interface and is capable of measuring local magnetic fields up to 10 Gauss with output data rates up to 80 Hz

 

Features

Layout

 

microbit and MAG3110

microbit and MAG3110

 

Code

 

#include <Wire.h>
#define MAG_ADDR 0x0E //7-bit address for the MAG3110, doesn't change
 
void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600); // start serial for output
  config(); // turn the MAG3110 on
}
 
void loop()
{
  print_values();
  delay(500);
}
 
void config(void)
{
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x11); // cntrl register2
  Wire.write(0x80); // send 0x80, enable auto resets
  Wire.endTransmission(); // stop transmitting
  delay(15);
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x10); // cntrl register1
  Wire.write(1); // send 0x01, active mode
  Wire.endTransmission(); // stop transmitting
}
 
void print_values(void)
{
  Serial.print("x=");
  Serial.print(readx());
  Serial.print(",");
  Serial.print("y=");
  Serial.print(ready());
  Serial.print(",");
  Serial.print("z=");
  Serial.println(readz());
}
 
int readx(void)
{
  int xl, xh; //define the MSB and LSB
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x01); // x MSB reg
  Wire.endTransmission(); // stop transmitting
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
    xh = Wire.read(); // receive the byte
  }
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x02); // x LSB reg
  Wire.endTransmission(); // stop transmitting
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
    xl = Wire.read(); // receive the byte
  }
  int xout = (xl|(xh << 8)); //concatenate the MSB and LSB
  return xout;
}
 
int ready(void)
{
  int yl, yh; //define the MSB and LSB
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x03); // y MSB reg
  Wire.endTransmission(); // stop transmitting
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
    yh = Wire.read(); // receive the byte
  }
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x04); // y LSB reg
  Wire.endTransmission(); // stop transmitting
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
    yl = Wire.read(); // receive the byte
  }
  int yout = (yl|(yh << 8)); //concatenate the MSB and LSB
  return yout;
}
 
int readz(void)
{
  int zl, zh; //define the MSB and LSB
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x05); // z MSB reg
  Wire.endTransmission(); // stop transmitting
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
    zh = Wire.read(); // receive the byte
  }
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x06); // z LSB reg
  Wire.endTransmission(); // stop transmitting
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
   zl = Wire.read(); // receive the byte
  }
  int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
  return zout;
}

 

Output

Open the serial monitor window

x=126,y=1552,z=72
x=256,y=1552,z=19
x=259,y=15,z=67
x=124,y=1544,z=67
x=128,y=1545,z=64
x=124,y=1544,z=68
x=0,y=1953,z=513
x=50,y=2082,z=88
x=81,y=1698,z=612
x=186,y=1961,z=65056
x=0,y=1188,z=13
x=258,y=1286,z=65041
x=112,y=1536,z=65113

 

Links

MAG3110 Triple 3 Axis Magnetometer Breakout Electronic Compass Sensor Module For Arduino