• No results found

Temperature sensor

In document Arduino Development Cookbook (Page 97-101)

Almost all sensors use the same analog interface. Here, we explore a very useful and fun sensor that uses the same. Temperature sensors are useful for obtaining data from the environment. They come in a variety of shapes, sizes, and specifications. We can mount one at the end of a robotic hand and measure the temperature in dangerous liquids. Or we can just build a thermometer.

Here, we will build a small thermometer using the classic LM35 and a bunch of LEDs.

Getting ready

The following are the ingredients required for this recipe: f An Arduino board connected to a computer via USB f A LM35 temperature sensor

f A breadboard and jumper wires

f A bunch of LEDs, different colors for a better effect f Some resistors between 220–1,000 ohm

How to do it…

The following are the steps to connect a button without a resistor: 1. Connect the LEDs next to each other on the breadboard.

2. Connect all LED negative terminals—the cathodes—together and then connect them to the Arduino GND.

3. Connect a resistor to each positive terminal of the LED. Then, connect each of the remaining resistor terminals to a digital pin on the Arduino. Here, we used pins 2 to 6. 4. Plug the LM35 in the breadboard and connect its ground to the GND line. The GND

pin is the one on the right, when looking at the flat face.

6. Lastly, use a jumper wire to connect the center LM35 pin to an analog input on the Arduino. Here we used the A0 analog pin.

Schematic

This is one possible implementation using the pin A0 for analog input and pins 2 to 6 for the LEDs:

Code

The following code will read the temperature from the LM35 sensor, write it on the serial, and light up the LEDs to create a thermometer effect:

// Declare the LEDs in an array

int LED [5] = {2, 3, 4, 5, 6};

int sensorPin = A0; // Declare the used sensor pin

void setup(){

// Start the Serial connection Serial.begin(9600);

// Set all LEDs as OUTPUTS for (int i = 0; i < 5; i++){

pinMode(LED[i], OUTPUT);

} }

void loop(){

// Read the value of the sensor int val = analogRead(sensorPin);

Serial.println(val); // Print it to the Serial

// On the LM35 each degree Celsius equals 10 mV

// 20C is represented by 200 mV which means 0.2 V / 5 V * 1023 = 41 // Each degree is represented by an analogue value change of approximately 2

// Set all LEDs off

for (int i = 0; i < 5; i++){

digitalWrite(LED[i], LOW);

}

if (val > 40 && val < 45){ // 20 - 22 C digitalWrite( LED[0], HIGH);

} else if (val > 45 && val < 49){ // 22 - 24 C digitalWrite( LED[0], HIGH);

digitalWrite( LED[1], HIGH);

} else if (val > 49 && val < 53){ // 24 - 26 C digitalWrite( LED[0], HIGH);

digitalWrite( LED[1], HIGH);

digitalWrite( LED[2], HIGH);

} else if (val > 53 && val < 57){ // 26 - 28 C digitalWrite( LED[0], HIGH);

digitalWrite( LED[1], HIGH);

digitalWrite( LED[2], HIGH);

digitalWrite( LED[3], HIGH);

} else if (val > 57){ // Over 28 C digitalWrite( LED[0], HIGH);

digitalWrite( LED[1], HIGH);

digitalWrite( LED[2], HIGH);

digitalWrite( LED[3], HIGH);

digitalWrite( LED[4], HIGH);

}

delay(100); // Small delay for the Serial to send

}

Blow into the temperature sensor to observe how the temperature goes up or down.

How it works…

The LM35 is a very simple and reliable sensor. It outputs an analog voltage on the center pin that is proportional to the temperature. More exactly, it outputs 10 mV for each degree Celsius. For a common value of 25 degrees, it will output 250 mV, or 0.25 V. We use the ADC inside the Arduino to read that voltage and light up LEDs accordingly.

If it's hot, we light up more of them, if not, less. If the LEDs are in order, we will get a nice

thermometer effect. Code breakdown

First, we declare the used LED pins and the analog input to which we connected the sensor. We have five LEDs to declare so, rather than defining five variables, we can store all five pin

numbers in an array with 5 elements:

int LED [5] = {2, 3, 4, 5, 6}; int sensorPin = A0;

We use the same array trick to simplify setting each pin as an output in the setup() function. Rather than using the pinMode() function five times, we have a for loop that will do it for us. It will iterate through each value in the LED[i] array and set each pin as output:

void setup(){

Serial.begin(9600);

for (int i = 0; i < 5; i++){ pinMode(LED[i], OUTPUT); }

In the loop() function, we continuously read the value of the sensor using the analogRead() function; then we print it on the serial:

int val = analogRead(sensorPin); Serial.println(val);

At last, we create our thermometer effect. For each degree Celsius, the LM35 returns 10 mV more. We can convert this to our analogRead() value in this way: 5V returns 1023, so a value of 0.20 V, corresponding to 20 degrees Celsius, will return 0.20 V/5 V * 1023, which will be equal to around 41.

We have five different temperature areas; we'll use standard if and else casuals to determine

which region we are in. Then we light the required LEDs.

There's more…

Almost all analog sensors use this method to return a value. They bring a proportional voltage to the value they read that we can read using the analogRead() function. Here are just a few of the sensor types we can use with this interface:

Temperature Humidity Pressure Altitude Depth Liquid level Distance Radiation Interference Current Voltage Inductance Resistance Capacitance Acceleration Orientation Angular

velocity Magnetism Compass Infrared Flexing Weight Force Alcohol Methane and

other gases Light Sound Pulse Unique ID such as fingerprint

Ghost!

In document Arduino Development Cookbook (Page 97-101)