How to Creating a Thermometer Blinks the Temperature using Arduino

Quick-Read Thermometer That Blinks the Temperature

In this project, I will show you a Guide How to Creating a Thermometer Blinks the Temperature using Arduino step by step complete process.

Now that you know how to build custom functions, we’ll make a  thermometer using the TMP36 temperature sensor in Complete Guide How to Creating a Quick-Read Thermometer using Arduino and the Arduino’s built-in LED. If the temperature is under 20 degrees Celsius, the LED will blink twice and then pause if the temperature drops within 20 and 26 degrees, the LED will blink four times and then pause, and if the temperature is over 26 degrees, the LED will blink six times.

We’ll make our sketch more modular by breaking it up into different functions that will give the sketch easier to follow, and the functions will be reusable.  the thermometer will make two main tasks: measure and categorize the temperature, and blink the LED a certain number of times.


≡Hardware


For  Creating a Thermometer Blinks the Temperature using Arduino project, you have to need this component :

  1. TMP36 temperature sensor
  2.  breadboard
  3. Various connecting wires
  4. Arduino and USB cable

≡ Schematic


Thermometer Blinks Temperature Arduino circuit is very simple, as shown in Figure 1.

Quick-Read Thermometer That Blinks the Temperature
Quick-Read Thermometer That Blinks the Temperature

≡ Sketch Thermometer Blinks Temperature Arduino


We’ll require to build two functions for the sketch. The first one will read the value from the TMP36, change it to Celsius, and then return a value of 2, 4, or 6, corresponding to the number of times the LED should blink. We’ll alter the sketch.

For our second function, we’ll apply blinkLed().  the void loop will name the functions in order and then pause for 2 seconds before restarting.

#define LED 13

int blinks = 0;

void setup()
{
  pinMode(LED, OUTPUT);
}

int checkTemp()
{
  float voltage  = 0;
  float celsius  = 0;
  float hotTemp  = 26;
  float coldTemp = 20;
  float sensor   = 0;
  int result;
  // read the temperature sensor and convert the result to degrees Celsius

  sensor = analogRead(0);
  voltage = (sensor * 5000) / 1024; // convert raw sensor value to millivolts
  voltage = voltage - 500;          // remove voltage offset
  celsius = voltage / 10;           // convert millivolts to Celsius

  // act on temperature range
  if (celsius < coldTemp)
  {
    result = 2;
  }
else if (celsius >= coldTemp && celsius <= hotTemp)
  {
    result = 4;
  }
  else
  {
    result = 6;    // (celsius > hotTemp)
  }
  return result;
}

void blinkLED(int cycles, int del)
{
  for ( int z = 0 ; z < cycles ; z++ )
  {
    digitalWrite(LED, HIGH);
    delay(del);
    digitalWrite(LED, LOW);
    delay(del);
  }
}

void loop()
{
  blinks = checkTemp();
  blinkLED(blinks, 500);
  delay(2000);
}

we apply custom functions, all we have to do in void_loop() is call them and set the delay. The function checkTemp() returns a value to the integer variable blinks, and then blinkLED() will blink the LED blinks times with a delay of 500 milliseconds. The sketch then pauses for 2 seconds before repeating.

Upload the sketch and watch the LED to see this thermometer in action.


≡ Displaying Data from the Arduino in the Serial Monitor


So far, we have sent sketches to the Arduino and applied the LEDs to show us the output. Blinking LEDs build it simple to get feedback from the Arduino, but blinking lights can tell us only so much. In this part, you’ll learn how to utilize the Arduino’s cable connection and the IDE’s Serial Monitor window to present data from the Arduino and send data to the Arduino from the computer keyboard.

The Serial Monitor

To open the Serial Monitor, start the IDE and click the Serial Monitor icon button on the toolbar, shown in Figure 2. The Serial Monitor should open and look similar to Figure 3.

Serial Monitor icon button on the IDE tool bar
Figure 2. Serial Monitor icon button on the IDE tool bar
 Serial Monitor
Figure 3. Serial Monitor

As you can see in Figure 3, the Serial Monitor presents an input field at the top, consisting of a single row and a Send button, and an output window below it, where data from the Arduino is displayed. If the Autoscroll box is checked, the most recent output is displayed, and once the screen is full, older data rolls off the screen as newer output is taken. If you uncheck Autoscroll, you can manually examine the data using a vertical scroll bar.

Starting the Serial Monitor

Before we can apply the Serial Monitor, we require to activate it by adding this function to our sketch in void setup():

Serial.begin(9600);
The value 9600 is the speed at which the data will travel between the computer and the Arduino, also known as baud. This value must match the speed setting at the bottom right of the Serial Monitor.

Sending Text to the Serial Monitor
To send a text to the Serial Monitor to be displayed in the output window, you can use Serial. print:

Serial.print(“Arduino for Everyone!”);
This sends the text between the quotation marks to the Serial Monitor’s output window.

You can also use Serial.println to display text and then force any following text to start on the next line:

Serial.println(“Arduino for Everyone!”);
Displaying the Contents of Variables
You can also display the contents of variables on the Serial Monitor. For example, this would display the contents of the variable results:

Serial.println(results);
If the variable is afloat, the display will default to two decimal places. You can specify the number of decimal places used as a number between 0 and 6 by entering a second parameter after the variable name. For example, to display the float variable results to four decimal places, you would enter the following:

Serial.print(results,4);


≡ Displaying the Temperature in the Serial Monitor


we’ll present temperature data in Celsius and Fahrenheit in the Serial Monitor window. To do this, we’ll create one function to determine the temperature values and another to display them in the Serial Monitor.

Enter this code into the IDE:

float celsius    = 0;
  float fahrenheit = 0;

  void setup()
  {
    Serial.begin(9600);
  }

1 void findTemps()
  {
    float voltage = 0;
    float sensor  = 0;

    // read the temperature sensor and convert the result to degrees C and F
    sensor  = analogRead(0);
    voltage = (sensor * 5000) / 1024;  // convert the raw sensor value to millivolts
    voltage = voltage - 500;           // remove the voltage offset
    celsius = voltage / 10;            // convert millivolts to Celsius
    fahrenheit = (1.8 * celsius) + 32; // convert Celsius to Fahrenheit
  }

2 void displayTemps()
  {
     Serial.print("Temperature is ");
     Serial.print(celsius, 2);
     Serial.print(" deg. C / ");
     Serial.print(fahrenheit, 2);
     Serial.println(" deg. F");
  // use .println here so the next reading starts on a new line
  }

  void loop()
  {
    findTemps();
    displayTemps();
    delay(1000);
  }

A lot is occurring in this sketch, but we’ve built two functions, findTemps() at 1 and displayTemps() at 2, to clarify things. These functions are called in void loop(), which is quite easy. Thus you understand one reason to make your own functions: to make your sketches easier to understand and the code more modular and possibly reusable.

After uploading the sketch, wait a few seconds, and then display the Serial Monitor. The temperature in your area should be presented in a similar manner to that shown in Figure 4.

Debugging with the Serial Monitor
The Serial Monitor can be utilized to help debug (locate and fix errors) your sketch. For example, if you insert Serial.println(); statements in your sketch containing brief notes about the location in the sketch, then you can see when the Arduino passes each statement. For example, you might use the line

Serial.println(“now in findTemps()”);
inside the function findTemps() to let you know when the Arduino is running that particular function.

Making Decisions with while Statements
You can use while() statements in a sketch to repeat instructions, as long as (while) a given condition is true. The condition is always tested before the code in the while() statement is executed. For example, while ( temperature > 30 ) will test to determine if the value of temperature is greater than 30. You can use any comparison operator within the parentheses to build the condition.

In the following listing, the Arduino will count up to 10 seconds and then continue with its program:

int a = 0; // an integer
while ( a < 10 )
{
a = a + 1;
delay(1000);
}
This sketch starts with the variable a set to 0. It then adds 1 to the value of a (which starts at 0), waits 1 second (delay(1000)), and then repeats the process until a has a value of 10 (while ( a < 10 )). Once a is equal to 10, the comparison in the while statement is false; therefore, the Arduino will continue on with the sketch after the while loop brackets.

do-while
In contrast to while, the do-while() structure places the test after the code within the do-while statement is executed. Here’s an example:

int a = 0; // an integer
do
{
delay(1000);
a = a + 1;
} while ( a < 100 );
In this case, the code between the curly brackets will execute before the conditions of the test (while ( a < 100 )) have been checked. As a result, even if the conditions are not met, the loop will run once. You’ll decide whether to use a while or a do-while function when designing your particular project.

All Arduino tutorial available Click here

 ARDUINO TUTORIAL 

 29 total views,  1 views today