0% found this document useful (0 votes)
7 views5 pages

Arduino TMP36 Temperature Sensor Code

The document outlines a final project for a Biomedical Instrumentation course, focusing on temperature data collection using a TMP36 sensor with Arduino. It details the code structure for reading and analyzing temperature data to identify critical points related to respiratory cycles. The project includes methods for averaging periods and calculating frequency of breaths based on the collected data.

Uploaded by

api-375199340
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)
7 views5 pages

Arduino TMP36 Temperature Sensor Code

The document outlines a final project for a Biomedical Instrumentation course, focusing on temperature data collection using a TMP36 sensor with Arduino. It details the code structure for reading and analyzing temperature data to identify critical points related to respiratory cycles. The project includes methods for averaging periods and calculating frequency of breaths based on the collected data.

Uploaded by

api-375199340
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

/ Trevor Nong

I r e c e i v e d a s s i s t a n c e from August D a i l e y i n d e s i g n i n g t h e program .



Group P a r t n e r s :
Maddie Mosscrop
Brandi StocktonF r e s s o

BE201 Biomedical Instrumentation
Final Project
Due : 5/17/2017
Final R e v i s i o n : 5/16/2017




A p o r t i o n o f t h i s code was p r o v i d e d p u b l i c domain a t
h t t p s : / / l e a r n . a d a f r u i t . com/tmp36temperature s e n s o r ? embeds=a l l o w&view=a l l
#u s i n g atemps e n s o r
/

// TMP36 Pin V a r i a b l e s
int sensorPin = 0;
// t h e a n a l o g p i n t h e TMP36 s Vout ( s e n s e ) p i n i s c o n n e c t e d t o
// t h e r e s o l u t i o n i s 10 mV / d e g r e e c e n t i g r a d e with a 500 mV o f f s e t t o
allow f o r negative temperatures

/
s e t u p ( ) t h i s f u n c t i o n r u n s once when you t u r n your Arduino on
We i n i t i a l i z e t h e s e r i a l c o n n e c t i o n with t h e computer
/

void setup ( )
{

// S t a r t t h e s e r i a l c o n n e c t i o n with t h e computer t o view t h e r e s u l t open t h e


s e r i a l monitor

S e r i a l . begin (9600) ;
}

// run o v e r and o v e r a g a i n

void loop ( )

// I wanted t o a n a l y z e t h e data t h a t was b e i n g c o l l e c t e d ,


// s o I i n i t i a l i z e d an a r r a y t o s t o r e t h e t e m p e r a t u r e r e a d i n g s from t h e
sensor .

1
f l o a t tempArray [ 2 0 0 ] ;
int i = 0;

f o r ( i = 0 ; i < 2 0 0 ; i ++) {

// g e t t i n g t h e v o l t a g e r e a d i n g from t h e t e m p e r a t u r e s e n s o r
i n t r e a d i n g = analogRead (A0) ;

// c o n v e r t i n g t h a t r e a d i n g t o v o l t a g e
f l o a t voltage = reading 5 . 0 ;
v o l t a g e /= 1 0 2 4 . 0 ;

// p r i n t out t h e v o l t a g e
Serial . print ( voltage ) ; Serial . println ( volts ) ;

// now p r i n t out t h e t e m p e r a t u r e
// c o n v e r t i n g from 10 mv p e r d e g r e e with 500 mV o f f s e t t o d e g r e e s ( ( v o l t a g e
500mV) t i m e s 1 0 0 )
f l o a t temperatureC = ( v o l t a g e 0 . 5 ) 100 ;
// S e r i a l . p r i n t ( temperatureC ) ; S e r i a l . p r i n t l n ( d e g r e e s C ) ;

// now c o n v e r t t o F a h r e n h e i t
f l o a t temperatureF = ( temperatureC 9 . 0 / 5 . 0 ) + 3 2 . 0 ;
S e r i a l . p r i n t ( temperatureF ) ; S e r i a l . p r i n t l n ( d e g r e e s F ) ;

tempArray [ i ] = temperatureF ;
// w a i t i n g . 1 s e c o n d s
delay (100) ;

// S t o r e s p o i n t s o f i n t e r e s t i n t h e data t h a t can be thought o f a s pseudo


c r i t i c a l points

int criticalPoint [ 7 ] ;

// S e t a c o n d i t o n t h a t d e s c r i b e s t h e b e h a v i o r o f t h e data

boolean i s I n c r e a s i n g ;

// An a l t e r n a t e i n d e x f o r t h e c r i t i c a l p o i n t a r r a y

int k ;

// The t e m p e r a t u r e i n a i r f l o w p a s t t h e t e m p e r a t u r e s e n s o r c o r r e s p o n d s t o t h e
respiratory cycle .
// For each i n h a l e , a i r w i l l r u s h o v e r t h e s e n s o r , c o o l i n g i t o f f , w h i l e f o r
each e x h a l e , a i r

2
// t h a t has been warmed by t h e body w i l l f l o w o v e r t h e s e n s o r , c a u s i n g i t t o
warm up . We would
// e x p e c t t h i s data t o a c t r o u g h l y l i k e a s i n e o r c o s i n e wave . So , i n o r d e r
t o a n a l y z e t h e amount
// o f b r e a t h taken i n a g i v e n p e r i o d o f time , I thought t h a t i t would be
c l e v e r t o f i n d t h e peaks
// and t r o u g h s o f each o f t h e s i n e waves c a t a l o g u e t h e i n d e x a t where t h i s
data p o i n t o c c u r s .
// Then , I would c o n s i d e r e v e r y o t h e r p o i n t a t which t h i s o c c u r s s o a s t o
o n l y c o n s i d e r peaks o r
// t r o u g h s , two o f which c o n s t i t u t e a p e r i o d , o r a f u l l b r e a t h c y c l e .
Because t h e data were
// taken a r e g u l a r i n t e r v a l s , i t i s e a s y t o c o n v e r t t h e d i f f e r e n c e i n i n d i c e s
to a d i f f e r e n c e
// i n time . This i s t h e s e c t i o n o f code t h a t I had h e l p with . I d i d not
know any C++ u n t i l
// about a week b e f o r e t h i s p r o j e c t and I hadn t had t o do any i n t e n s e
programming i n about a y e a r .

// Here , I s e t a c o n d i t i o n t o f i n d i f t h e s i n e wave was i n c r e a s i n g , and i f


i t was , t o break
// out o f t h e l o o p and p r o c e e d t o t h e next s t e p

while ( true )
{
i f ( tempArray [ i ] < tempArray [ i + 1 ] ) {
isIncreasing = true ;
break ;
}
i f ( tempArray [ i ] > tempArray [ i + 1 ] ) {
isIncreasing = false ;
break ;
}
i f ( tempArray [ i ] == tempArray [ i + 1 ] ) {
i = i + 1;
}
}

// Here , I s t a r t e d a f o r l o o p t o p r o c e e d through each data p o i n t and


// d e c i d e whether o r not i t was a c r i t i c a l p o i n t

f o r ( i n t i = 1 ; i < 2 0 0 ; i ++)
{

// An i n i t i a l approach t h a t d i d not prove f r u i t f u l .


// i f ( i = 1 )
// {
//
// i f ( tempArray [ i 1 ] < tempArray [ i ] ) {

3
// i s In c r e as i n g = true ;}
//
// i f ( tempArray [ i 1 ] > tempArray [ i ] ) {
// isIncreasing = false ;}
//
// i f ( tempArray [ i + 1 ] < tempArray [ i ] ) {
// isIncreasing = false ;}
//
// i f ( tempArray [ i + 1 ] > tempArray [ i ] ) {
// i s In c r e as i n g = true ;
// }
// }

// Here s t h e good s t u f f .
// This p r o c e s s t u r n e d out t o be a b i t s i m p l e r than my f i r s t attempt above .

// So , i n o r d e r t o f i n d a c r i t i c a l p o i n t , t h e r e a r e two c o n d i t i o n s t h a t
must apply
// I s t h e data i n c r e a s i n g / d e c r e a s i n g ?
// and
// Do we e x p e c t t h e data t o i n c r e a s e / d e c r e a s e ?
// For example , i f t h e s i n e wave i n i n c r e a s i n g , and we e x p e c t t h e next
data p o i n t
// t o d e c r e a s e , then we have found one o f our p o i n t s o f i n t e r e s t .
if ( i != 0 )

i f ( i s I n c r e a s i n g = t r u e && tempArray [ i ] < tempArray [ i + 1 ] ) {


criticalPoint [k] = i ;
k = k + 1;
isIncreasing = false ;
Serial . println (k) ;
}

i f ( i s I n c r e a s i n g = f a l s e && tempArray [ i ] > tempArray [ i + 1 ] ) {


criticalPoint [k] = i ;
k = k + 1;
i s In c r ea s i ng = true ;
Serial . println (k) ;
}

// I d e c i d e d t h a t I a l s o wanted t o smooth t h e data I was g e t t i n g , t o an


extent .
// So I d e c i d e d t o c o l l e c t 7 p o i n t s o f i n t e r e s t s o a s t o g e t 3 p e r i o d s , and
// a v e r a g e t h e 3 p e r i o d s . This h o p e f u l l y i n s u r e s t h a t we g e t a c c u r a t e data
// and c u t s down on i r r e g u l a r i t y i n one c y c l e o f b r e a t h .
// Though , I t may be i n t h e u s e r i n t e r e s t t o f i n d i r r e g u l a r i t y i n b r e a t h .

i f ( k >= 7 )

4
// i f ( c r i t i c a l P o i n t . s i z e == 7 )
break ;
}
}
// Averaging the p e r i o d s .
float period1 = criticalPoint [3] criticalPoint [1];
float period2 = criticalPoint [5] criticalPoint [3];
float period3 = criticalPoint [7] criticalPoint [5];

// I used t h e a b s o l u t e v a l u e f u n c t i o n b e c a u s e I kept g e t t i n g n e g a t i v e
numbers f o r some r e a s o n

f l o a t a v g P e r i o d = ( abs ( p e r i o d 1 ) + abs ( p e r i o d 2 ) + abs ( p e r i o d 3 ) ) / 3 . 0 ;


S e r i a l . p r i n t l n ( avgPeriod ) ;

// C o n v e r t i n g t o time !
f l o a t timePeriod = avgPeriod . 1 ;
S e r i a l . p r i n t l n ( timePeriod ) ;

// C o n v e r t i n g p e r i o d t o f r e q u e n c y !
f l o a t frequency = ( 1 . 0 / timePeriod ) 1000;

// Report f r e q u e n c y t o u s e r !
S e r i a l . p r i n t ( frequency , 10) ; S e r i a l . p r i n t l n ( breaths per second ) ;

// Aaaaaand we r e done .
// Again s p e c i a l thanks t o August D a i l e y f o r h e l p i n g out with t h e l o g i c .
return ;
}

You might also like