summaryrefslogtreecommitdiff
path: root/tags/electronics/temperature-sensor/1.0/exerciser/exerciser.pde
blob: 5bec56e0e85da0e8895e270b50845d98adaa131d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

#include<math.h>

int a_InPin = 0;    // select the input pin for the potentiometer
     
int val;

double r0;
double r1;
double r2;
double t0;
double beta;
double vadc;
double vcc;
double vs;
double rs;
double k;

double celsius;

void setup()
{
  // begin the serial communication
  Serial.begin(9600);
 
  r0 = 10000.0;
  r1 = 680;
  r2 = 1600;
  t0 = 25.0 + 273.15;
  beta = 3500.0;
  vadc = 5.0;
  vcc = 5.0;
  vs = (r1 * vcc) / (r1 + r2);
  rs = (r1 * r2) / (r1 + r2);
//  k = r0 * pow(2.178, (-beta / t0)); // 1.07527403
  k = 1.07527403;
}

void loop()
{
  
  int celhold;
  
  val = analogRead(a_InPin);    // read the value from the analog pin
  celsius = to_temp(val);
  
  Serial.println((long)celsius);            // Write the value to the serial port
  delay(1000);                  // stop the program for 1 sec

}


double to_temp(int reading)
{
  float v; 
  double r;
       
       Serial.print("reading:");
       Serial.println(reading);
       v = (float)((reading * vadc) / 1024.0);          // convert the 10 bit ADC value to a voltage
       Serial.print("voltage:");
       Serial.println((long)(v * 1000));
       r = (rs * v) / (vs - v);     // resistance of thermistor
       Serial.print("resistance:");
       Serial.println((long)r);
       return ((beta / log(r / k)) - 273.15);        // temperature
}

/*
float to_adc(int temp)
{
         "Convert a temperature into a ADC value"
       r = self.r0 * exp(self.beta * (1 / (t + 273.15) - 1 / self.t0)) # resistance of the thermistor
       v = self.vs * r / (self.rs + r)     # the voltage at the potential divider
       return round(v / self.vadc * 1024)  # the ADC reading
}
*/