0% found this document useful (0 votes)
84 views14 pages

Arduino Analog I/O and Sensor Programming

The document describes an Arduino application that interfaces with sensors over analog, SPI, and I2C interfaces. It connects a temperature sensor over analog, a pressure and temperature sensor over SPI, and an accelerometer over I2C. It provides code snippets to initialize the interfaces, read sensor values, and calculate temperature in Celsius from the sensor readings. Functions are defined to write and read registers of the SPI and I2C devices. In a loop, it starts conversions, waits for completion, reads the result registers, and calculates and prints the temperature.

Uploaded by

Hamdi Gdhami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views14 pages

Arduino Analog I/O and Sensor Programming

The document describes an Arduino application that interfaces with sensors over analog, SPI, and I2C interfaces. It connects a temperature sensor over analog, a pressure and temperature sensor over SPI, and an accelerometer over I2C. It provides code snippets to initialize the interfaces, read sensor values, and calculate temperature in Celsius from the sensor readings. Functions are defined to write and read registers of the SPI and I2C devices. In a loop, it starts conversions, waits for completion, reads the result registers, and calculates and prints the temperature.

Uploaded by

Hamdi Gdhami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercices+SOL

Analog I/O: Arduino Programming

A SoC with:
• D12 conneceted to a LED.
• D3 connected to a Button (Pressed =LOW)
• A1 connected to an TMP36 (Temperature Sensor)
• ADC with 10 bits max resolution, Vref++5V, Vref-=0V

The Application must each second:


- Test if the Push Button is pressed/
 If yes:
- Convert Temperature and send conversion result to
serial port.
- Calculate Temperature and send value to Serial Port.
- If (Temp<16) or (Temp>28): Led ON

2
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino Programming

G = ( Vref - + (Nin * ΔQ) – VMES ) /slope + Gmes

Vref-= 0V
0,5
ΔQ = (Vref+ - Vref-) / 2N = 5000/1024

Δy = 1,2 – 0,5 V = 0,7 V = 700 mV, Δx = 75 – 0 = 75°C

Slope = 700mv /75 °C = 9,33 mV / °C


0

𝑁𝑖𝑛 ∗ 5000
( ) − 500
𝑇𝑒𝑚𝑝 𝑚𝑉 = 1024
9,33

3
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino Programming

𝑁𝑖𝑛 ∗ 5000
( ) − 500
𝑇𝑒𝑚𝑝 𝑚𝑉 = 1024
9,33

4
DAMERGI Emir – INSAT 2021
SPI: Arduino Programming - Example

The Sensor HTS221 (Temperature, pressure) is


connected to the SPI bus of an Arduino compatible
Bit 7 of CTRL_REG1 = 1
Board (see Arduino Numbers)
to power HTS

Bit 0 of CTRL_REG2 = 1
to start a Temperature
conversion

Bit 0 of STATUS_REG = 1
 Conversion Finished.
 New Values available.

• The Conversion result is 16 bits long and placed in the


2 *8bit registers; TEMP_OUT_L and TEMP_OUT_H.
• The Temperarue is calculated: conversion result*slope
+ Zero_Value.

DAMERGI Emir – INSAT 2021 5


SPI: Arduino Programming - Solution 1

#include « SPI.h »

//Registers Addresses
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define STATUS_REG 0x27
#define TEMP_OUT_L 0x2A
#define TEMP_OUT_H 0x2B

//Pin used to select Slave


#define SlaveSelect 5

//Constants
const int slope=…;
const int zero_val=…;

//variables
uint8_t Temperature_L, Tempertaure_H;
int Temperature;

DAMERGI Emir – INSAT 2021 6


SPI: Arduino Programming - Solution 2: SPI Read and Write Functions

void WriteToSPI (uint8_t RegAddr, uint8_t Value) uint8_t ReadFromToSPI (uint8_t RegAddr)
{ {
//Activate Slave uint8_t regvalue;
digitalWrite (SlaveSelect , LOW);
//Activate Slave
//send address digitalWrite (SlaveSelect , LOW);
SPI.transfer (RegAddr);
//send address
//send value SPI.transfer (RegAddr);
SPI.transfer (Value);
//read value
//Desactivate Slave regvalue = SPI.transfer (Value);
digitalWrite (SlaveSelect , HIGH);
} //Desactivate Slave
digitalWrite (SlaveSelect , HIGH);

return (regvalue);
}

DAMERGI Emir – INSAT 2021 7


SPI: Arduino Programming - Solution 3: Setup Function

void setup (void)


{
//Configure Slave Select Pin and
//Initialize to HIGH (SPI Slave off)
pinMode (SlaveSelect , OUTPUT);
digitalWrite (SlaveSelect , HIGH);

//Initialize SPI and Serial Periphs


SPI.begin();

//Power On HTS221
Bit 7 of CTRL_REG1 = 1
to power HTS SPI.WriteToSPI (CTRL_REG1 , 0x80);

Serial.begin(115200);
}
DAMERGI Emir – INSAT 2021 8
SPI: Arduino Programming - Solution 4: Loop

void loop (void)


{
Bit 0 of CTRL_REG2 = 1 //Start Conversion
to start a Temperature SPI.WriteToSPI (CTRL_REG2 , 0x01);
conversion
//Wait until Data is available
Bit 0 of STATUS_REG = 1
While (SPI.ReadFromSPI (STATUS_REG) & 0x01 !=0);
 Conversion Finished.
 New Values available.
//Read OUT_H & OUT_L registers

Temperature_L = SPI.ReadFromSPI (TEMP_OUT_L);

Temperature_H = SPI.ReadFromSPI (TEMP_OUT_H);

Temperature = Temperature_L | (Temperature_H <<8);

Temperature = (Temperature * slope) + zero_val;

Serial.print (Temperaure, 4);

}
DAMERGI Emir – INSAT 2021 9
I2C: Arduino Programming - Application with Accelerometer Lis3SDH (@0x6b)

SDA = 21 LIS3DSH is a 3 axis accelerometer MEMS sensor with +/- 6G Full Scale.
SCL = 22
• The results for each axis are 16 bits integer and are stored in 2
registers (2’s complement format).
• WHO AM I Register (Generally exists in each I2C device) and helps
identifying the device. For the LIS3SDH the value is 0x3F

DAMERGI Emir – INSAT 2021 10


I2C: Arduino Programming - Solution 1

#include « wire.h »

//Slave addr
#define Lis3SDH_Addr 0x6b

//Registers Addresses
#define WHO_AM_I 0x0F
#define CTRL_REG4 0x20
#define OUT_X_L 0x28
#define OUT_X_H 0x29

//Pin used to select Slave


#define SDA_Pin 21
#define SCL_Pin 22

//Constants
SDA = 21 TwoWire MyI2cBus = TwoWire(0);
SCL = 22

//variables
uint8_t Temperature_L, Tempertaure_H;
int Temperature;

DAMERGI Emir – INSAT 2021 11


I2C: Arduino Programming - Solution 2: I2C Read and Write Functions

void writeI2cReg (uint8_t RegAddr, uint8_t Value) uint8_t readI2cReg (uint8_t RegAddr)
{
MyI2cBus.beginTransmission(Lis3SDH_Addr);
{
MyI2cBus.write(RegAddr);
MyI2cBus.beginTransmission(Lis3SDH_Addr);
/ /Resart without Stop
MyI2cBus.write(RegAddr);
MyI2cBus.endTransmission(false)
MyI2cBus.write(Value);
//Read one byte
//end Transmission
MyI2cBus.requestFrom (Lis3SDH_Addr,0x01);
MyI2cBus.endTransmission(true);
uint8_t regvalue = MyI2cBusC.read() ;
}
MyI2cBus.endTransmission(true) ;
return (regvalue );
}

DAMERGI Emir – INSAT 2021 12


I2C: Arduino Programming - Solution 3: Setup Function

void setup ()
{
Serial.begin(115200);
MyI2cBus.begin(SDA_Pin, SCL_Pin, 400000);
// Configure LSI3SDH
//Continuous update, Enable X, Y and Z axis , 100Hz
// CTRL_REG4  0x67

writeI2cReg ( CTRL_REG4, 0x67);


}

DAMERGI Emir – INSAT 2021 13


I2C: Arduino Programming - Solution 4: Loop

void loop()
{
//Read Accelerations conversion result
uint8_t outXL = (readI2cReg(OUT_X_L) ;
uint8_t outXH = (readI2cReg(OUT_X_H) ;

int outX = OutXL | (OutXH <<8);


LIS3DSH is a 3 axis accelerometer MEMS sensor with +/- 6G Full Scale.
• The results for each axis are 16 bits integer and are stored in 2
registers (2’s complement format). int accelX = (outX * 6) / 2^15 ;

Serial.print (accelX ,4);

delay( …);
}
DAMERGI Emir – INSAT 2021 14

You might also like