The last time, we used the Arduino as a Web server. In the next few entries, we will aim to expand the Web server by building a Stevenson screen. While there are thermometers and hydrometers in a Stevenson screen, this time, we will be building a Stevenson screen for indoor use, using optical and ultrasonic sensors used previously, so that besides temperature and humidity, it is capable of sensing the indoor environment.
We aim to be able to store the data collected by the Stevenson screen in an SD card, and integrate it with a server-side program through the Web server.
In this article, we will se how to operate the temperature sensor with Arduino. We will combine it with the Web server light sensor used previously.
Today’s Electronics Recipe
Estimated time to complete: 45 minutes
Parts needed:
- Arduino (Arduino Uno R3) http://ift.tt/19qjkOK
- Breadboard http://ift.tt/1z7Tzip
- Temperature sensor (LM35DZ)
- 7-segment LED NAR161-B (high intensity red, 7-segment LED display, common anode)
- Resistor (220Ω)
How to use the temperature sensor
While there are many types of temperature sensors, we will be using the common LM35DZ.
Temperature sensor LM35DZ
Compared to other sensors, temperature sensors are easy to use. The LM35DZ is able to measure temperatures from 0℃ to 100℃, giving an output of 0V when the temperature is 0℃, 10mv at 1℃, and 100mV at 10℃.
As Arduino can receive analog inputs with values from 0 to 1023, it is possible to measure temperature using Arduino with the following formula.
Temperature = (([Base voltage(5V)] * [input value from analog pin]) / 1024) * 100;
Let’s connect this up to the Arduino.
The temperature sensor is connected as per the picture below.
With the flat part on top, from the left: +Vs, Vout, GND
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// // Program to display the temperature sensor value on serial monitor // int sensorPin = A0; // using analog pin 0 int sensorValue = 0; void setup() { Serial.begin(9600); // setting to display on serial monitor } void loop() { sensorValue = analogRead(sensorPin); // getting input from analog pin 0 float temp = modTemp(sensorValue); // converting input values from the temperature sensor Serial.println(temp); // displaying results on serial monitor delay(500); // 0.5 second delay } // converting analog input value to ℃ float modTemp( int analog_val){ float v = 5; // base voltage (V) float tempC = ((v * analog_val) / 1024) * 100; // conversion to Celsius return tempC; |
Display on serial monitor
Using the serial monitor ([Tool]-[Serial monitor]), also used in the article about the light sensor, to display the current temperature on the screen!
Looking at the program, besides the setup() and loop() functions, the modTemp() function also appears in the program. The process can be to some degree consolidated into functions.
Structure of Functions
[return value type] [function name]([parameters]){
〜define the processing here〜
return [value to return];
Example:
1
2
3
4
5
6
7
8
|
loop(){ int result01 = testFunc(1,2); int result02 = testFunc(3,4); } int testFunc( int A, int B){ int C = A + B; return C; } |
Process flow and results: Calling of function “testFunc” within “loop”, and returning the results of the processing by “testFunc” in “result01” and “result02” based on the parameters passed. Value in “result01” is 3 and “result02” is 7.
As the program gets longer, using functions will allow to consolidate the process, avoid the code repetitions, and make the change needed directly in the functions. So, let’s try to use functions when there is similar process to be carried out.
This is all we need to us the temperature sensor. Easy, right?
Installation on the Web server
Let’s install the temperature sensor on the Web server that we’ve built previously.
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
|
// // Program to display temperature sensor and light sensor through the Web server // #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // specifying the MAC address on the Ethernet shield IPAddress ip(192,168,0,177); // specifying the IP address assigned to the Arduino server EthernetServer server(80); // specifying the server port (no issue to keep as 80 as we are using HTTP) void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } Ethernet.begin(mac, ip); // testing connection with Ethernet shield server.begin(); Serial.print( "server is at " ); Serial.println(Ethernet.localIP()); } void loop() { EthernetClient client = server.available(); // monitor for external connections to the server if (client) { Serial.println( "new client" ); // an http request ends with a blank line boolean currentLineIsBlank = true ; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println( "HTTP/1.1 200 OK" ); client.println( "Content-Type: text/html" ); client.println( "Connection: close" ); // the connection will be closed after completion of the response client.println(); client.println( "<!DOCTYPE HTML>" ); client.println( "<html>" ); // A) input from each sensor // input from optical sensor int sensorReading = analogRead(0); // analog pin 0 client.print( "Light:" ); client.print(sensorReading); client.println( "<br />" ); delay(50); // input from temperature sensor sensorReading = analogRead(1); // analog pin 1 client.print( "Temp:" ); client.print(modTemp(sensorReading)); client.println( "<br />" ); client.println( "</html>" ); break ; } if (c == '\n' ) { // you're starting a new line currentLineIsBlank = true ; } else if (c != '\r' ) { // you've gotten a character on the current line currentLineIsBlank = false ; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println( "client disonnected" ); } } // B) converting analog input value to ℃ float modTemp( int analog_val){ float v = 5; // base voltage (V) float tempC = ((v * analog_val) / 1024) * 100; // conversion to Celsius return tempC; } |
The parts in red are what has changed from the program previously used for the Web server. Using part A to get the actual analog value (previously used for “for” statement), and part B to define the function to convert the temperature value, it is possible to display the values from the two sensors.
Once you are ready with the program, try displaying it in your browser.
We are one step closer to our Stevenson screen!
Display of Arduino Data
After using Arduino, you may have realized that there are many methods for displaying data. You can use Arduino and display the data through parts such as LED, on a PC through serial monitors, via the Ethernet shield through a network, and etc. There are many display methods, but in this example, as we build the Stevenson screen, what would be an interesting way to display data obtained? After all the trouble to set up a Web server with an Ethernet shield, it would be interesting to be able to display the data on a smart phone.
Whether it is interesting or boring depends on the display method. Let’s try displaying the input values using an electrical component known as a 7-segment LED.
* There will be a detailed explanation on how to use a 7-segment LED next time.
Attempt to display the temperature sensor value on 7-segment LED
When operating Arduino using power adapters, you cannot see the serial monitor. For those who want to see the sensor values immediately, try setting up a 7-segment LED. It can display the analog input value as a numerical value.
7-segment LED
Testing a 7-segment LED using a test count-up program
How to use a 7-segment LED
Let’s try using the 7-segment LED to display the value of the temperature sensor. Trying to use the 7-segment LED as it is led to the value inside the circuit overflowing, making the Arduino into something like a bomb
Displaying the value of the temperature sensor using a 7-segment LED
Final installation of temperature sensor and optical sensor
After installing the temperature sensor and the light sensor, the setup looks more complete. In the next article, we will be installing another sensor, and then we will be having a look at the case that will house the Arduino.
View more at: http://yoursmart.mobi
Post a Comment