Categories

Follow Us

Arduino and TMP102 digital sensor example

In this article we look at the TMP102 digital sensor and we will connect it up to an Arduino – lets start with some technical info about this sensor

The TMP102 device is a digital temperature sensor ideal for NTC/PTC thermistor replacement where high accuracy is required. The device offers an accuracy of ±0.5°C without requiring calibration or external component signal conditioning. Device temperature sensors are highly linear and do not require complex calculations or lookup tables to derive the temperature. The on-chip 12-bit ADC offers resolutions down to 0.0625°C.

The TMP102 device features SMBus™, two-wire and I2C interface compatibility, and allows up to four devices on one bus. The device also features an SMBus alert function. The device is specified to operate over supply voltages from 1.4 to 3.6 V with the maximum quiescent current of 10 µA over the full operating range.

The TMP102 device is ideal for extended temperature measurement in a variety of communication, computer, consumer, environmental, industrial, and instrumentation applications. The device is specified for operation over a temperature range of –40°C to 125°C.

 

Layout

 

arduino and tmp102

arduino and tmp102

 

Parts List

Name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
TMP102 breakout TMP102 Digital Temperature Sensor Breakout Board Module 12-bit 1.4V To 3.6VDC Sensor Module Temperature Module
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Code

This is from the Arduino site with a few tweaks

#include "Wire.h"
#define TMP102_I2C_ADDRESS 72 /* This is the I2C address for our chip. This value is correct if you tie the ADD0 pin to ground. See the datasheet for some other values. */
 
 
void setup() 
{
  Wire.begin(); // start the I2C library
  Serial.begin(115200); //Start serial communication at 115200 baud
}
 
 
void loop() 
{
  getTemp102();
  delay(5000); //wait 5 seconds before printing our next set of readings. 
}
 
void getTemp102()
{
  byte firstbyte, secondbyte; //these are the bytes we read from the TMP102 temperature registers
  int val; /* an int is capable of storing two bytes, this is where we "chuck" the two bytes together. */ 
  float convertedtemp; /* We then need to multiply our two bytes by a scaling factor, mentioned in the datasheet. */ 
  float correctedtemp; 
 
  /* Reset the register pointer (by default it is ready to read temperatures)
You can alter it to a writeable register and alter some of the configuration - 
the sensor is capable of alerting you if the temperature is above or below a specified threshold. */
 
  Wire.beginTransmission(TMP102_I2C_ADDRESS); //Say hi to the sensor. 
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.requestFrom(TMP102_I2C_ADDRESS, 2);
  Wire.endTransmission();
 
 
  firstbyte      = (Wire.read()); 
  /*read the TMP102 datasheet - here we read one byte from
   each of the temperature registers on the TMP102*/
  secondbyte     = (Wire.read()); 
  /*The first byte contains the most significant bits, and 
   the second the less significant */
    val = firstbyte;
    if ((firstbyte & 0x80) > 0) 
    {
      val |= 0x0F00;
    } 
    val <<= 4; 
 /* MSB */
    val |= (secondbyte >> 4);    
/* LSB is ORed into the second 4 bits of our byte.
Bitwise maths is a bit funky, but there's a good tutorial on the playground*/
    convertedtemp = val*0.0625;
    correctedtemp = convertedtemp - 5; 
    /* See the above note on overreading */
 
 
  Serial.print("firstbyte is ");
  Serial.print("\t");
  Serial.println(firstbyte, BIN);
  Serial.print("secondbyte is ");
  Serial.print("\t");
  Serial.println(secondbyte, BIN);
  Serial.print("Concatenated byte is ");
  Serial.print("\t");
  Serial.println(val, BIN);
  Serial.print("Converted temp is ");
  Serial.print("\t");
  Serial.println(val*0.0625);
  Serial.print("Corrected temp is ");
  Serial.print("\t");
  Serial.println(correctedtemp);
  Serial.println();
}

 

Output

Open the serial monitor window and you should see something like this

firstbyte is 10110
secondbyte is 11000000
Concatenated byte is 101101100
Converted temp is 22.75
Corrected temp is 17.75

firstbyte is 11100
secondbyte is 10110000
Concatenated byte is 111001011
Converted temp is 28.69
Corrected temp is 23.69

 

Link

http://www.ti.com/lit/gpn/tmp102

Share

Arduino and MPU-9255 sensor example

In this article we look at the MPU-9255 and an Arduino, lets look at some information about the sensor

MPU-9255 is a multi-chip module (MCM) consisting of two dies integrated into a single QFN package. One die houses the 3-Axis gyroscope and the 3-Axis accelerometer. The other die houses the AK8963 3-Axis magnetometer from Asahi Kasei Microdevices Corporation. Hence, the MPU-9255 is a 9-axis MotionTracking device that combines a 3-axis gyroscope, 3-axis accelerometer, 3-axis magnetometer and a Digital Motion Processor™ (DMP) all in a small 3x3x1mm package available as a pin-compatible upgrade from the MPU-6515.

With its dedicated I2C sensor bus, the MPU-9255 directly provides complete 9-axis MotionFusion™ output. The MPU-9255 MotionTracking device, with its 9-axis integration, on-chip MotionFusion™, and run-time calibration firmware, enables manufacturers to eliminate the costly and complex selection, qualification, and system level integration of discrete devices, guaranteeing optimal motion performance for consumers. MPU-9255 is also designed to interface with multiple non-inertial digital sensors, such as pressure sensors, on its auxiliary I2C port.

MPU-9255 features three 16-bit analog-to-digital converters (ADCs) for digitizing the gyroscope outputs, three 16-bit ADCs for digitizing the accelerometer outputs, and three 16-bit ADCs for digitizing the
magnetometer outputs. For precision tracking of both fast and slow motions, the parts feature a user programmable gyroscope full-scale range of ±250, ±500, ±1000, and ±2000°/sec (dps), a user programmable accelerometer full-scale range of ±2g, ±4g, ±8g, and ±16g, and a magnetometer full-scale range of ±4800µT.

Other industry-leading features include programmable digital filters, a precision clock with 1% drift from -40°C to 85°C, an embedded temperature sensor, and programmable interrupts. The device features I2C and SPI serial interfaces, a VDD operating range of 2.4V to 3.6V, and a separate digital IO supply, VDDIO from 1.71V to VDD.

Communication with all registers of the device is performed using either I2C at 400kHz or SPI at 1MHz. For applications requiring faster communications, the sensor and interrupt registers may be read using SPI at 20MHz.

 

Parts List

Name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
MPU9255 sensor 1Set IIC/I2C MPU-9255 MPU9255 Three-Axis Gyroscope+Accelerator+Magnetometer Sensor Board Module
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Layout

 

arduino and mpu

arduino and mpu

 

Code

You need to import the https://github.com/Bill2462/MPU9255-Arduino-Library

/*
Raw data example
This example reads raw readings from the magnetometer gyroscope and the accelerometer and then
displays them in serial monitor.
*/
 
#include <MPU9255.h>//include MPU9255 library
 
MPU9255 mpu;
 
void setup() {
  Serial.begin(115200);//initialize Serial port
 
  if(mpu.init())
  {
  Serial.println("initialization failed");
  }
  else
  {
  Serial.println("initialization succesful!");
  }
 
}
 
void loop() {
  mpu.read_acc();//get data from the accelerometer
  mpu.read_gyro();//get data from the gyroscope
  mpu.read_mag();//get data from the magnetometer
 
  //print all data in serial monitor
  Serial.print("AX: ");
  Serial.print(mpu.ax);
  Serial.print(" AY: ");
  Serial.print(mpu.ay);
  Serial.print(" AZ: ");
  Serial.print(mpu.az);
  Serial.print("    GX: ");
  Serial.print(mpu.gx);
  Serial.print(" GY: ");
  Serial.print(mpu.gy);
  Serial.print(" GZ: ");
  Serial.print(mpu.gz);
  Serial.print("    MX: ");
  Serial.print(mpu.mx);
  Serial.print(" MY: ");
  Serial.print(mpu.my);
  Serial.print(" MZ: ");
  Serial.println(mpu.mz);
  delay(100);
}

 

Output

 

List

https://store.invensense.com/datasheets/invensense/PS-MPU-9255.pdf

Share

Controlling LEDs on the Rich Shield with a remote control

This Rich Shield for the Arduino has many useful components onboard such as LED’s, buttons and a buzzer. It is easy and great for learning arduino development and programming

This is the shield with a little remote control, we will show you an example to control the 4 LEDs the remote control

 

Parts List

You can get both of the required parts for about $12

Name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
Rich Shield for Arduino Rich Shield + IR Remote with Infrared Receiver LED Buzzer Button Light Sensor Temperature Sensor 24C02 EEPROM for Arduino UNO R3

Test Code

In this step we need to find the code that is associated with each key we wish to use, there are 4 LEDs on the board and we will use 4 buttons to switch an LED on and another 4 to switch the LEDs off, so in this example 8 buttons in total

You will need to download the library and add the zip file using the library manager

Click here to download the IRremote library

#include <IRremote.h>
 
int RECV_PIN = 2;
 
IRrecv irrecv(RECV_PIN);
 
decode_results results;
 
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Serial results");
}
 
void loop() 
{
  if (irrecv.decode(&results)) 
  {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}

 

You should see something like this, I have added the corresponding button as well

FF30CF – 1
FF18E7 – 2
FF7A85 – 3
FF10EF – 4
FF38C7 – 5
FF5AA5 – 6
FF42BD – 7
FF4AB5 – 8
FF52AD – 9
FF6897 – 0

In our example we used the windows calculator to convert from hex to decimal values, we could have done this in the code.

Complete Example

Now we need to implement the complete example

/*
My remote values
FF30CF - 1
FF18E7 - 2
FF7A85 - 3
FF10EF - 4
FF38C7 - 5
FF5AA5 - 6
FF42BD - 7
FF4AB5 - 8
FF52AD - 9
FF6897 - 0
I convert these to decimal
*/
#include <IRremote.h>
 
int IR_Recv = 2;   //IR Receiver Pin 2
int redPin = 4;
int greenPin = 5;
int bluePin = 6;
int yellowPin = 7;
 
IRrecv irrecv(IR_Recv);
decode_results results;
 
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Starts the receiver
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);    
  pinMode(yellowPin, OUTPUT);   
  pinMode(redPin, OUTPUT);      
}
 
void loop()
{
  //decodes the infrared input
  if (irrecv.decode(&results))
  {
    long int decCode = results.value;
    Serial.println(results.value);
    //switch case to use the selected button
    switch (results.value)
    {
      case 16724175: //when you press 1
        digitalWrite(yellowPin, HIGH);
        break;   
      case 16726215: //when you press 5
        digitalWrite(yellowPin, LOW);   
        break;
       case 16718055: //when you press 2
        digitalWrite(bluePin, HIGH);
        break;           
       case 16734885: //when you press 6
        digitalWrite(bluePin, LOW);
        break;       
       case 16743045: //when you press 3
        digitalWrite(greenPin, HIGH);
        break;       
       case 16728765: //when you press 7
        digitalWrite(greenPin, LOW);
        break;
     case 16716015: //when you press 4
        digitalWrite(redPin, HIGH);
        break;
     case 16730805: //when you press 8
        digitalWrite(redPin, LOW);
        break;
    }
    irrecv.resume();
  }
  delay(10);
}

 

Exercise(s)

Implement a button to switch on all LEDs and to switch off all LEDs

Share

Arduino and BME environmental sensor example

In this article we will connect a BME680 sensor to an Arduino

BME680 is an integrated environmental sensor developed specifically for mobile applications and wearables where size and low power consumption are key requirements. Expanding Bosch Sensortec’s existing family of environmental sensors, the BME680 integrates for the first time high-linearity and high-accuracy gas, pressure, humidity and temperature sensors. The gas sensor within the BME680 can detect a broad range of gases to measure air quality for personal well being.

Gases that can be detected by the BME680 include Volatile Organic Compounds (VOC) from paints (such as formaldehyde), lacquers, paint strippers, cleaning supplies, furnishings, office equipment, glues, adhesives and alcohol.

Parameter Technical data
Package dimensions 8-Pin LGA with metal
3.0 x 3.0 x 0.93 mm³
Operation range (full accuracy) Pressure: 300…1100 hPa
Humidity 0…100%
Temperature: -40…85°C
Supply voltage VDDIO
Supply voltage VDD
1.2 … 3.6 V
1.71 … 3.6 V
Interface I²C and SPI
Average current consumption
(1Hz data refresh rate)
2.1 µA at 1 Hz humidity and temperature
3.1 µA at 1 Hz pressure and temperature
3.7 µA at 1 Hz humidity, pressure and temperature 0.09‒12 mA for p/h/T/gas depending on operation mode
Average current consumption in sleep mode 0.15 μA
Gas sensor
Response time (τ 33-63%)
Sensor-to-sensor deviation
Power consumption
Output data processing
< 1 s (for new sensors)
+/- 15% +/- 15
< 0.1 mA in ultra-low power mode
direct output of IAQ: Index for Air Quality
Humidity sensor
Response time (τ0-63%)
Accuracy tolerance
Hysteresis
8 s
± 3 % relative humidity
≤ 1.5 % relative humidity
Pressure sensor
RMS Noise
Sensitivity Error
Temperature coefficient offset
0.12 Pa (equiv. to 1.7 cm)
± 0.25 % (equiv. to 1 m at 400 m height change)
±1.3 Pa/K (equiv. to ±10.9 cm at 1°C temperature change)

 

Parts List

Name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
BME680 BME680 Sensor Module Temperature and Humidity Air Pressure Air Quality IAQ MCU680 Module
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic

We use the I2C connection for the sensor

arduino and bme680

arduino and bme680

 

Code

You will need to import the adafruit sensor and bme680 libraries – you can add these using the library manager

My particular sensor used address 0x76, the default is 0x77 so you may have to change this line from if (!bme.begin(0x76)) to if (!bme.begin())

 

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
 
 
#define SEALEVELPRESSURE_HPA (1013.25)
 
Adafruit_BME680 bme; // I2C
 
void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println(F("BME680 test"));
 
  if (!bme.begin(0x76)) 
  {
    Serial.println("Could not find a valid BME680 sensor, check wiring!");
    while (1);
  }
 
  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320*C for 150 ms
}
 
void loop() 
{
  if (! bme.performReading()) 
  {
    Serial.println("Failed to perform reading :(");
    return;
  }
  Serial.print("Temperature = ");
  Serial.print(bme.temperature);
  Serial.println(" *C");
 
  Serial.print("Pressure = ");
  Serial.print(bme.pressure / 100.0);
  Serial.println(" hPa");
 
  Serial.print("Humidity = ");
  Serial.print(bme.humidity);
  Serial.println(" %");
 
  Serial.print("Gas = ");
  Serial.print(bme.gas_resistance / 1000.0);
  Serial.println(" KOhms");
 
  Serial.print("Approx. Altitude = ");
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");
 
  Serial.println();
  delay(2000);
}

 

Output

Open the serial monitor and you will see something like this

BME680 test
Temperature = 21.52 *C
Pressure = 971.46 hPa
Humidity = 42.24 %
Gas = 0.00 KOhms
Approx. Altitude = 353.89 m

Temperature = 22.38 *C
Pressure = 971.44 hPa
Humidity = 42.71 %
Gas = 144.27 KOhms
Approx. Altitude = 354.06 m

Temperature = 24.83 *C
Pressure = 971.46 hPa
Humidity = 43.88 %
Gas = 139.47 KOhms
Approx. Altitude = 353.72 m

Share