Arduino Due and TMP102 digital temperature sensor example

In this article we look at the TMP102 digital sensor and we will connect it up to an Arduino Due

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

Another easy device to connect to our Arduino Due as you can see below

 

arduino due and TMP102

arduino due and TMP102

 

Parts List

 

Name Link
Arduino Due Black Due R3 Board DUE-CH340 ATSAM3X8E ARM Main Control Board with 50cm USB Cable CH340G for arduino
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 10100
secondbyte is 1110000
Concatenated byte is 101000111
Converted temp is 20.44
Corrected temp is 15.44

firstbyte is 10100
secondbyte is 1110000
Concatenated byte is 101000111
Converted temp is 20.44
Corrected temp is 15.44

firstbyte is 10100
secondbyte is 1100000
Concatenated byte is 101000110
Converted temp is 20.37
Corrected temp is 15.38

 

Link

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

Share

Arduino Due and Si1145 sensor example

In this exampl;e we look at the Si1145 sensor, we connect it to an Arduino Due and use the Arduino IDE for development

First the sensor in a little more detail

The Si1145/46/47 is a low-power, reflectance-based, infrared proximity, ultraviolet (UV) index, and ambient light sensor with I2C digital interface and programmable event interrupt output. This touchless sensor IC includes an analog-to-digital converter, integrated high-sensitivity visible and infrared photodiodes, digital signal processor, and one, two, or three integrated infrared LED drivers with fifteen selectable drive levels. The Si1145/46/47 offers excellent performance under a wide dynamic range and a variety of light sources including direct sunlight. The Si1145/46/47 can also work under dark glass covers.

The photodiode response and associated digital conversion circuitry provide excellent immunity to artificial light flicker noise and natural light flutter noise. With two or more LEDs, the Si1146/47 is capable of supporting multiple-axis proximity motion detection. The Si1145/46/47 devices are provided in a 10-lead 2×2 mm QFN package and are capable of operation from 1.71 to 3.6 V over the –40 to +85 °C temperature range.

 

Parts List

name Link
Arduino Due Black Due R3 Board DUE-CH340 ATSAM3X8E ARM Main Control Board with 50cm USB Cable CH340G for arduino
Si1145 breakout SI1145 UV IR Visible Sensor I2C GY1145 Light Breakout Board Module
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Layout

Simple to connect being an I2C device – I used 3v3 on my sesnor

 

arduino due and si1145

arduino due and si1145

Code

This example requires the Adafruit Si1145 library, you can add this via the library manager or download from https://github.com/adafruit/Adafruit_SI1145_Library

#include <Wire.h>
#include "Adafruit_SI1145.h"
 
Adafruit_SI1145 uv = Adafruit_SI1145();
 
void setup() {
  Serial.begin(9600);
 
  Serial.println("Adafruit SI1145 test");
 
  if (! uv.begin()) {
    Serial.println("Didn't find Si1145");
    while (1);
  }
 
  Serial.println("OK!");
}
 
void loop() {
  Serial.println("===================");
  Serial.print("Vis: "); Serial.println(uv.readVisible());
  Serial.print("IR: "); Serial.println(uv.readIR());
 
  // Uncomment if you have an IR LED attached to LED pin!
  //Serial.print("Prox: "); Serial.println(uv.readProx());
 
  float UVindex = uv.readUV();
  // the index is multiplied by 100 so to get the
  // integer index, divide by 100!
  UVindex /= 100.0;  
  Serial.print("UV: ");  Serial.println(UVindex);
 
  delay(1000);
}

 

Output

Open the serial monitor window – these readings were taken indoors

Adafruit SI1145 test
OK!
===================
Vis: 256
IR: 281
UV: 0.02
===================
Vis: 263
IR: 280
UV: 0.03
===================
Vis: 261
IR: 279
UV: 0.02
===================

Link

https://www.silabs.com/documents/public/data-sheets/Si1145-46-47.pdf

Share

Flashing the onboard LEDs on a PyBoard

The Pyboard comes with 4 on board LEDs, in this example we will flash them on and off, the example was written in uPyCraft

 

Pinouts

 

Parts List

Name Link
Elecrow PyBoard Elecrow Core Board For MicroPython Crow Pyboard Development Board STM32F405RG for Pyboard Python Learning Module Microcontroller

Code

First of all the various methods from the official docs

LED.intensity([value])
Get or set the LED intensity. Intensity ranges between 0 (off) and 255 (full on). If no argument is given, return the LED intensity. If an argument is given, set the LED intensity and return None.

Note: Only LED(3) and LED(4) can have a smoothly varying intensity, and they use timer PWM to implement it. LED(3) uses Timer(2) and LED(4) uses Timer(3). These timers are only configured for PWM if the intensity of the relevant LED is set to a value between 1 and 254. Otherwise the timers are free for general purpose use.

LED.off()
Turn the LED off.
LED.on()
Turn the LED on, to maximum intensity.
LED.toggle()
Toggle the LED between on (maximum intensity) and off. If the LED is at non-zero intensity then it is considered “on” and toggle will turn it off.

Now for the example showing this

from pyb import LED
import time
 
led1 = LED(1) # 1=red, 2=green, 3=yellow, 4=blue
led1.toggle()
led1.on()
time.sleep(1)
led1.off()
time.sleep(1)
 
led2 = LED(2) # 1=red, 2=green, 3=yellow, 4=blue
led2.toggle()
led2.on()
time.sleep(1)
led2.off()
time.sleep(1)
 
led3 = LED(3) # 1=red, 2=green, 3=yellow, 4=blue
led3.toggle()
led3.on()
time.sleep(1)
led3.off()
time.sleep(1)
 
led4 = LED(4) # 1=red, 2=green, 3=yellow, 4=blue
led4.toggle()
led4.on()
time.sleep(1)
led4.off()
time.sleep(1)
 
# LEDs 3 and 4 support PWM intensity (0-255)
LED(4).intensity()    # get intensity
LED(4).intensity(25) # set intensity to 25
Share

Pyboard Micropython I2C Scanner

This is a simple very short code example in which we show how to create a basic I2CScanner in Micropython using the uPyCraft IDE for a Pyboard

The arduino equivalent is one that I use frequently and is one of the most useful sketches for figuring out why a sensor may not be working correctly with a library or code – usually because the default I2C address differs from the sensor address that you buy

Layout

An example connecting a sensor to a Pyboard

 

Code

from pyb import I2C
 
i2c = I2C(1, I2C.MASTER, baudrate=100000)
print('Scan i2c bus...')
devices = i2c.scan()
 
if len(devices) == 0:
  print("No i2c device !")
else:
  print('i2c devices found:',len(devices))
 
for device in devices:  
  print("Decimal address: ",device)
  print("Hex address: ",hex(device))

 

 

Output

All going well you should output like the following in uPyCraft – this is a TMP175 connected to a PyBoard (not a BMP180 as the layout earlier)

Ready to download this file,please wait!

download ok
exec(open(‘i2cscanner.py’).read(),globals())
Scan i2c bus…
i2c devices found: 1
Decimal address: 55
Hex address: 0x37
>>>

Share

A look at the STM32F4DISCOVERY with an Arduino based example

In this example we look at the STM32F4DISCOVERY and in particular the STM32F407G-DISC1

Lets look at some key features

The STM32F4DISCOVERY kit leverages the capabilities of the STM32F407 high performance microcontrollers, to allow users to easily develop applications featuring audio.

It includes an ST-LINK embedded debug tool, one ST-MEMS digital accelerometer, a digital microphone, one audio DAC with integrated class D speaker driver, LEDs, push-buttons and an USB OTG micro-AB connector.

With the latest board enhancement, the new order code STM32F407G-DISC1 has replaced the old reference STM32F4DISCOVERY.

Key Features

  • STM32F407VGT6 microcontroller featuring 32-bit ARM®Cortex®-M4 with FPU core, 1-Mbyte Flash memory, 192-Kbyte RAM in an LQFP100 package
  • On-board ST-LINK/V2 on STM32F4DISCOVERY (old reference) or ST-LINK/V2-A on STM32F407G-DISC1 (new order code)
  • USB ST-LINK with re-enumeration capability and three different interfaces:
    • Debug port
    • Virtual Com port (with new order code only)
    • Mass storage (with new order code only)
  • Board power supply: through USB bus or from an external 5 V supply voltage
  • External application power supply: 3 V and 5 V
  • LIS302DL or LIS3DSH ST MEMS 3-axis accelerometer
  • MP45DT02 ST-MEMS audio sensor omni-directional digital microphone
  • CS43L22 audio DAC with integrated class D speaker driver
  • Eight LEDs:
    • LD1 (red/green) for USB communication
    • LD2 (red) for 3.3 V power on
    • Four user LEDs, LD3 (orange), LD4 (green), LD5 (red) and LD6 (blue)
    • 2 USB OTG LEDs LD7 (green) VBUS and LD8 (red) over-current
  • Two push-buttons (user and reset)
  • USB OTG FS with micro-AB connector
  • Extension header for all LQFP100 I/Os for quick connection to prototyping board and easy probing
  • Comprehensive free software including a variety of examples, part of STM32CubeF4 package or STSW-STM32068 to use legacy standard libraries.

 

There are 4 coloured LEDs on the STM32F407G-DISC1 development board as stated above, these are locate dunder the microcontroller

PIN NAME COLOUR POSITION
PD13 LD3 ORANGE UP
PD14 LD5 RED RIGHT
PD15 LD6 BLUE DOWN
PD12 LD4 GREEN LEFT

 

Parts List

STM32F4DISCOVERY Embeded ST-LINK/V2 STM32 Evaluation Development Board,Free shipping

Code

We used Atom IDE with Platform.IO installed and added support for the STM32 boards. This is an Arduino example (confused). You could add STM32 support to the Arduino IDE by adding stm32duino support but I like the look of Platform IDE so wanted to play about with the IDE

This is a fairly basic example to flash the LEDS off and on repeatedly

#include <Arduino.h>
 
void setup() {
  // put your setup code here, to run once:
 
  // Set the MCU's pin data direction.
  pinMode(PD13, OUTPUT);
  pinMode(PD14, OUTPUT);
  pinMode(PD15, OUTPUT);
  pinMode(PD12, OUTPUT);
 
  // Set all outputs LOW to have all LED's initially turned off.
  digitalWrite(PD13, LOW);
  digitalWrite(PD14, LOW);
  digitalWrite(PD15, LOW);
  digitalWrite(PD12, LOW);
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
  // Turn off the GREEN LED, turn on the ORANGE LED and wait for 1000ms.
  digitalWrite(PD12, HIGH);
  digitalWrite(PD13, HIGH);
  digitalWrite(PD14, HIGH);
  digitalWrite(PD15, HIGH);
  delay(1000);
 
  digitalWrite(PD12, LOW);
  digitalWrite(PD13, LOW);
  digitalWrite(PD14, LOW);
  digitalWrite(PD15, LOW);
  delay(1000);
 
  // The code in the "loop()" statement will automatically repeat.
}

 

Link

https://www.st.com/en/evaluation-tools/stm32f4discovery.html

Share