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 Uno and HY-SRF05 ultrasonic sensor example

The HY-SRF05 is an ultrasonic emitter/receiver used to measure distance with a precision of ~0.3cm. It sends out a 40 KHz square wave signal that reflects on objects in front of the sensor. This signal is then read back by the sensor and the duration of the received signal is reflected on the ECHO pin.

Features

Supply voltage: 4.5V to 5.5V
Supply current: 10 to 40mA
Trigger pin format: 10 uS digital pulse
Sound frequency: 40 KHz
Echo pin output: 0V-VCC
Echo pin format: digital

How to use

Send a 10Us wide pulse (low to high) to the Trigger pin.
Monitor the ECHO pin.
When the ECHO pin goes HIGH, start a timer.
When the ECHO pin goes LOW, stop the timer and save the elapsed time.
Use the elapsed time in the following formula to get the distance in cm:

Distance (in cm) = (elapsed time * sound velocity (340 m/s)) / 100 / 2

We will see a code example later

 

Parts List

Name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
HY-SRF05 HY-SRF05 SRF05 Ultrasonic Ranging Module Ultrasonic Sensor
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Layout

 

arduino and HY-SRF05

arduino and HY-SRF05

 

Code

/*
VCC to +5V
GND to ground
TRIG to digital pin 12
ECHO to digital pin 13
*/
 
const int TRIG_PIN = 12;
const int ECHO_PIN = 13;
 
void setup() 
{
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(TRIG_PIN,OUTPUT);
  pinMode(ECHO_PIN,INPUT);
}
 
void loop()
{
  long duration, distanceCm, distanceIn;
 
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN,HIGH);
 
  // convert the time into a distance
  distanceCm = duration / 29.1 / 2 ;
  distanceIn = duration / 74 / 2;
 
  if (distanceCm <= 0)
  {
    Serial.println("Out of range");
  }
  else 
  {
    Serial.print(distanceIn);
    Serial.print("in: ");
    Serial.print(distanceCm);
    Serial.print("cm");
    Serial.println();
  }
  delay(1000);
}

 

Output

Open the serial monitor and you will something like this depending how close the module is to an object

I was moving the sensor away from an object

4in, 10cm
3in, 9cm
4in, 10cm
4in, 10cm
4in, 10cm
4in, 10cm
4in, 12cm
4in, 12cm
4in, 12cm

Share