chiark / gitweb /
wiringPi Version 2 - First commit (of v2)
[wiringPi.git] / examples / Gertboard / temperature.c
1 /*
2  * temperature.c:
3  *      Demonstrate use of the Gertboard A to D converter to make
4  *      a simple thermometer using the LM35.
5  *
6  * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
7  ***********************************************************************
8  * This file is part of wiringPi:
9  *      https://projects.drogon.net/raspberry-pi/wiringpi/
10  *
11  *    wiringPi is free software: you can redistribute it and/or modify
12  *    it under the terms of the GNU Lesser General Public License as published by
13  *    the Free Software Foundation, either version 3 of the License, or
14  *    (at your option) any later version.
15  *
16  *    wiringPi is distributed in the hope that it will be useful,
17  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *    GNU Lesser General Public License for more details.
20  *
21  *    You should have received a copy of the GNU Lesser General Public License
22  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
23  ***********************************************************************
24  */
25
26 #include <stdio.h>
27
28 #include <wiringPi.h>
29 #include <gertboard.h>
30
31 int main ()
32 {
33   int x1, x2 ;
34   double v1, v2 ;
35
36   printf ("\n") ;
37   printf ("Gertboard demo: Simple Thermemeter\n") ;
38   printf ("==================================\n") ;
39
40 // Always initialise wiringPi. Use wiringPiSys() if you don't need
41 //      (or want) to run as root
42
43   wiringPiSetupSys () ;
44
45 // Initialise the Gertboard analog hardware at pin 100
46
47   gertboardAnalogSetup (100) ;
48
49   printf ("\n") ;
50   printf ("| Channel 0 | Channel 1 | Temperature 1 | Temperature 2 |\n") ;
51
52   for (;;)
53   {
54
55 // Read the 2 channels:
56
57     x1 = analogRead (100) ;
58     x2 = analogRead (101) ;
59
60 // Convert to a voltage:
61
62     v1 = (double)x1 / 1023.0 * 3.3 ;
63     v2 = (double)x2 / 1023.0 * 3.3 ;
64
65 // Print
66
67     printf ("|    %6.3f |    %6.3f |", v1, v2) ;
68
69 // Print Temperature of both channels by converting the LM35 reading
70 //      to a temperature. Fortunately these are easy: 0.01 volts per C.
71
72     printf ("          %4.1f |          %4.1f |\r", v1 * 100.0, v2 * 100.0) ;
73     fflush (stdout) ;
74   }
75
76   return 0 ;
77 }
78