A second solution is to use a different temperature sensor, LM35DZ (HCSENS0011). This plugs into the same socket but must face the opposite way and have jumper, J1, OPEN.
(If you get this wrong the chip will get very hot very quickly!) This a very quick acting sensor with analogue output connected to A4 in analogue mode.
• Calibrated directly in ˚Celsius (Centigrade)
• Linear + 10.0 mV/˚C scale factor
• 0.5˚C accuracy guaranteeable (at +25˚C)
• Range when powered by 5Volts and GND is +2˚C to 150˚C
The analogue pin A4 spreads 5Volts over 1024 equal divisions (10 bit); so each single step is 5/1024 Volts, ie 0.0048828 Volts or 0.0048828 * 100 ËšC = 0.48828 ËšC; just under half a degree.
This means that we should only rely on the reading to the nearest whole ËšC.
The previous DS18B20 is a 12 bit device; hence the extra accuracy.
Here is the code. The display is continuous, it updates quickly if you warm up the sensor with hot fingers but it is only accurate to the nearest 1ËšC.
Code: Select all
/* LM35to7Segment -- HobbyComponents Multifunction Shield HCARD0085 --
Tony Goodhew 20 Sept 2014
Temperature Sensor LM35DZ in U5 - right hand 3 machined sockets
==========> Flat face towards 7 Segment Display <========== VERY IMPORTANT !!! or it gets VERY HOT!!!
==========> J1 SHOULD NOT BE IN PLACE - TURN SIDEWAYS <==========
This is a faster but less accurate sensor than a DS18B20 and is displayed to the nearest 1ºC
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com and the author in your comments if you redistribute this code.
This software may not be used for the purpose of promoting or selling products
that directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS AND THE AUTHOR, Tony Goodhew, MAKE NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS, OR Tony Goodhew, SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/
// Import libraries
#include <OneWire.h> // http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <DallasTemperature.h> // https://github.com/milesburton/Arduino-Temperature-Control-Library
#define ONE_WIRE_BUS 18 // Connect to Pin A4
// Define shift register pins used for seven segment display
#define LATCH 4
#define CLK 7
#define DATA 8
// Global variables
int Number = 8888; // Initial LED check
int tempPin = 4; // Pin A4 Analogue
int data; // Data value from TMP35 (0 - 1023)
float convert; // Conversion factor from analog data to temperature in ºC
// LED block byte codes for symbols - Just sum unlit segment values: Top = 1, centre = 64, DP = 128
const byte SegCode[] = {192, 249, 164, 176, 153, 146, 130, 248, 128, 152, 255}; //0 - 9 + Blank
// Byte pointers to select blocks 1 to 4 - 1 is Most Significant Block
const byte Block[] = {241, 242, 244, 248};
void setup()
{
// Set pin modes
pinMode(LATCH,OUTPUT);
pinMode(CLK,OUTPUT);
pinMode(DATA,OUTPUT);
convert = 5.0 * 100.0 / 1024.0; // ºC per analog data step @ 10 milli volts / ºC
}
void loop()
{
data = analogRead(tempPin);
if (data == 0) {Number = 0;} // To prevent rounding errors in FP arithmetic
else Number = int(float(data) * convert + 0.5); // Temperature rounded to nearest Integer
for (int i = 0; i < 100; i++)
// Display temperature 100 times
{
show();
}
}
void show()
// Routine to update 7-segment display - suppresses leading zeros
{
if (Number > 999) {Send(0, Number / 1000);} // MS Block - left
if (Number > 99) {Send(1, (Number / 100) % 10);} // without DP
if (Number > 9 ) {Send(2, (Number / 10) % 10);}
if (Number >= 0) {Send(3, Number % 10);} // LS Block - right
}
void Send(byte BlockIndex, byte Value)
// Send a symbol to one of the 4 LED blocks of the display
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLK, MSBFIRST, SegCode[Value]);
shiftOut(DATA, CLK, MSBFIRST, Block[BlockIndex] );
digitalWrite(LATCH, HIGH);
}
If you leave this set up running you may find that as the temperature in the room slowly changes the “units†digit starts to flicker. This is not really a fault. It indicates that the voltage is right on a step up or down inside the Analogue to Digital converter.
I checked the temperature displayed against a certified mercury thermometer that I’ve had since back when colour photography needed a dark room, film, chemicals and accurate water temperatures. The readings were the same – reassuring!
This all goes to prove that programming these devices is all about compromise. You can go for simplicity, speed, accuracy or a steady display but you cannot have it all at once.
I see from the circuit diagram that digital pins 0 and 1, together with GRD and 5Volts are available on the connector to the left of the 7-segment display. I think I’ll be trying a radio link to another Arduino in the near future.
Tony Goodhew