So I’ve recently been playing with some Arduino hardware and thought it might be worth documenting some stuff which might be useful to other Arduino newbies.

The LM35 DZ sensor is quite neat as it is very easy to read the temperature from the output. Unlike some other sensors which require you to remove a base voltage this one just has a linear output from 0 with every 10mV = 1°C. I’ve put one together here along with some code to get you started.

A circuit diagram showing an Arduino connected to temperature sensor (LN35DZ) and an LED

Connect the output of the temperature sensor to pin A0 of your Arduino. Optionally use an LED with suitable resistor to ‘flash’ on each temperature reading

As I mentioned temperature is really easy to calculate

float temperature = (valueIn * 5.0 *100) /1024.0;

Now to make things more interesting instead of just dumping the temperature to the serial output I’ve used the Ethernet shield and broadcasted the reading over the LAN with a UDP packet.

temperatureC = getTemperatureC(lm35DzPin);
Serial.println(temperatureC);
dtostrf(temperatureC, 6, 2, buffer);
Udp.beginPacket(IPAddress(255, 255, 255, 255), port);  
Udp.write(buffer);
Udp.endPacket();

Arduino with temperature sensor in action

Fire-up Wireshark and we see that the temperature in the room is 22°C (± typical error of 0.9°C).

Wireshark output

Quiet impressed how easy the entire thing to put together. Obviously this can’t be used in production without some modification as reading arbitrary data over the network without a signature is risky. In a hypothetical situation where you just display the temperature elsewhere one could trivially ‘hack’ the display to show all kinds of profanity.

##Resources
[LM35 data sheet]

##Full code listing

 1 #include <SPI.h>
 2 #include <Ethernet.h>
 3 #include <EthernetUdp.h>
 4 
 5 int lm35DzPin = 0;
 6 int dLedPin = 5;
 7 
 8 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 9 
10 char  buffer[7];      //
11 
12 unsigned int port = 9999;
13 EthernetUDP Udp;
14 
15 void startAndPrintIP()
16 {
17   // start the Ethernet and UDP:
18   Ethernet.begin(mac);
19   Udp.begin(port);
20 
21    // print your local IP address:
22   Serial.print("My IP address: ");
23   for (byte thisByte = 0; thisByte < 4; thisByte++) {
24     // print the value of each byte of the IP address:
25     Serial.print(Ethernet.localIP()[thisByte], DEC);
26     Serial.print("."); 
27   }
28   Serial.println();
29 }
30 
31 
32 void flashLED(int pin, int times)
33 {
34   int i =0; 
35   while(i<times)
36   {   
37       digitalWrite(dLedPin, HIGH);
38       delay(5);
39       digitalWrite(dLedPin, LOW);
40       delay(5);
41       i++;
42   }
43 }
44 
45 // Note pin must be set up for read first
46 // Forces a delay of 1 second
47 float getTemperatureC(int pin)
48 {
49    int valueIn = analogRead(pin);  
50    float temperature = (valueIn * 5.0 *100) /1024.0;
51    return temperature;
52 }
53 
54 void loop() {
55     float temperatureC;
56     temperatureC = getTemperatureC(lm35DzPin);
57     Serial.println(temperatureC);
58     
59     dtostrf(temperatureC, 6, 2, buffer);
60     
61     Udp.beginPacket(IPAddress(255, 255, 255, 255), port);
62     Udp.write(buffer);
63     Udp.endPacket();
64     
65     flashLED(dLedPin, 2);   // contains delay of 20
66     delay(980);
67 }
68 
69 void setup() {
70     pinMode(dLedPin, OUTPUT);
71     Serial.begin(9600);
72     Serial.println("Serial ready");
73     flashLED(dLedPin, 2);
74     startAndPrintIP();
75     flashLED(dLedPin, 2);
76 }