Categories

LM35 temperature sensor

In this example we will connect an LM35 temperature sensor to our Arduino

The LM35 series are precision integrated-circuit temperature sensors, whose output voltage is linearly proportional to the Celsius (Centigrade) temperature. The LM35 thus has an advantage over linear temperature sensors calibrated in Kelvin, as the user is not required to subtract a large constant voltage from its output to obtain convenient Centigrade scaling. The LM35 does not require any external calibration or trimming to provide typical accuracies of ±1/4°C at room temperature and ±3/4°C over a full -55 to +150°C  temperature range

Here is a picture of the pins, its important to get these correct or you can damage the sensor

LM35 temperature sensor pinout

LM35 temperature sensor pinout

I purchased a small module, which looks like this. This was clearly labelled and helps avoid any mistakes with wiring, I also prefer using cables to modules rather than breadboards

lm35 module

lm35 module

Schematics and Parts

You will need

Arduino Uno
LM35 sensor or module
Hook up wire (dupont cables)

Very simple to connect Vcc is 5v, Gnd is any Gnd and out goes to Arduino A0, you can see this below

Arduino and lm35 schematic

Arduino and lm35 schematic

Code

//initializes/defines the output pin of the LM35 temperature sensor
int outputpin= 0;
//this sets the ground pin to LOW and the input voltage pin to high
void setup()
{
Serial.begin(9600);
}
 
//main loop
void loop()
{
int rawvoltage= analogRead(outputpin);
float millivolts= (rawvoltage/1024.0) * 5000;
float celsius= millivolts/10;
Serial.print(celsius);
Serial.print(" degrees Celsius, ");
 
Serial.print((celsius * 9)/5 + 32);
Serial.println(" degrees Fahrenheit");
delay(1000);
 
}

Results

Here are the results via the serial monitor

arduino and lm35 serial output

arduino and lm35 serial output

 

Links

LM35 datasheet

 
5PCS LM35D Temperature Sensor TO92 Packing

Share

SHT21 humidity and temperature sensor example

The SHT21 is a low cost humidity and temperature sensor. Its an I2C device so again is very simple to connect to any arduino

This is the breakout for the sensor that I bought.

SHT21

SHT21

Here is a simple schematic, again be careful as Vcc is 3.3v with this device

Schematic

UNO and SHT21

UNO and SHT21

Lets look at the required libraries and a basic code example, there is not much to this to be honest most of the work is done in the library

Code

There are a couple of libraries to help you out

https://github.com/misenso/SHT2x-Arduino-Library
https://github.com/elechouse/SHT21_Arduino

I used the latter one

 

#include <Wire.h>
#include <SHT2x.h>
 
 
void setup()
{
  Wire.begin();
  Serial.begin(9600);
}
 
void loop()
{
  Serial.print("Humidity: ");
  Serial.println(SHT2x.GetHumidity());
  Serial.print("Temperature(C): ");
  Serial.println(SHT2x.GetTemperature());
  Serial.println();
  delay(1000);
}

Links

these come in at about $7 for a useful breakout board with this sensor

Humidity Sensor – SHT21 Breakout Board

Share

DHT11 sensor example

In this example we will show a basic example of connecting a DHT11 Temperature and Humidity Sensor to your Arduino. This is a very nice little, low cost sensor which requires only a pull up resistor to VDD. This can easily be constructed on a mini bread board and then joined up to your Arduino. using some connecting cables

The DHT sensors are made of two parts, a capacitive humidity sensor and a thermistor. There is a chip inside that does some a/d conversion and outputs a digital signal with the temperature and humidity.

I wont go into the technical details of the DHT11, there is a link at the bottom to a PDF datasheet which explains all you need to know.

Schematic

Arduino and DHT11 schematic

Arduino and DHT11 schematic

Connections

Here is the breadboard layout

Arduino and DHT11 breadboard

Arduino and DHT11 breadboard

Code

You will need the DHT11 library from the DHT library. You could roll your own code but this saves a whole lot of time.

Download, extract and copy this to your Arduino libraries folder location. This is a simple example which outputs the humidity and temperature, place your fingers on the DHT11 and you will see the readings fluctuate

#include <dht11.h>

dht11 DHT;
//Pin 4 of Arduino to Data of DHT11
#define DHT11_PIN 4

void setup()
{
Serial.begin(9600);
int check;
Serial.print(“DHT11 STATUS – \t”);
check = DHT.read(DHT11_PIN);
//check status
switch (check)
{
case DHTLIB_OK:
Serial.print(“OK\n”);
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print(“Checksum error\n”);
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print(“Timeout error\n”);
break;
default:
Serial.print(“Unknown error\n”);
break;
}
}

void loop()
{

//humidity and temperature output
Serial.print(“Humidity is “);
Serial.print(DHT.humidity,1);
Serial.print(“\n”);
Serial.println(“Temperature is “);
Serial.println(DHT.temperature,1);

delay(1000);
}

Output

Upload the sketch above and open the Serial Monitor window

arduino and dht11 serial output

arduino and dht11 serial output

 

Parts List

Label	Part Type
DHT1	DHT11 Humitidy and Temperature Sensor	
Part1	Arduino Uno (Rev3)
R1	4.7k Ω

 

Links

DHT11 Humidity and Temperature Sensor from Amazon UK

DHT11 Humidity and Temperature Sensor from Amazon US

DHT11 Datasheet in PDF format

Share

DS18B20 temperature sensor

The DS18B20 is a  temperature sensor that can be used in various simple projects.  This part uses the 1 wire (I2C) bus and you can connect multiple sensors up to your Arduino.

The part is also relatively low cost and only requires an additional 4k7 pull up resistor. In the example below we shall make a basic example that reads the temperature and outputs via serial and can be verified using the serial monitor in the Arduino IDE.

Lets look at the parts list

Parts List

Label Part Type
DS1 DS18B20 1-Wire Temperature Sensor
Part1 Arduino Uno (Rev3)
R1 4.7k Ω Resistor

Layout

As always be careful not to get the connections incorrect, you can refer to the pinout for the device below to help . The DS18B20 can be powered by between 3.0V and 5.5V so you can simply connect its GND pin to 0V and the VDD pin to +5V from the Arduino Duo.

ds18b20 pinout

ds18b20 pinout

 

Here is the connection diagram showing how to connect your arduino to the resistor and sensor.

ds1820 breadboard

ds1820 breadboard

Coding

You need to download 2 libraries and copy them into your Arduino libraries folder.

Dallas 18b20 Temperature Library
“1-Wire” Library

Now copy and paste this into a new sketch

#include <OneWire.h>
#include <DallasTemperature.h>

//the pin you connect the ds18b20 to
#define DS18B20 2

OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);

void setup()
{
Serial.begin(9600);
delay(1000);
//start reading
sensors.begin();
}

void loop()
{
//read temperature and output via serial
sensors.requestTemperatures();
Serial.print(sensors.getTempCByIndex(0));
Serial.println(” degrees C”);
}

When you upload the sketch, go to Tools -> Serial Monitor and you should see something like the following. Put your finger  on the DS18B20 and you should see the temperature vary.

 

ds18b20 output

ds18b20 output

Links

DS18B20 links

Buy DS18B20 from Amazon UK

Buy DS18B20 from Amazon US

Share