Friday, 23 November 2018

Arduino - VI

We continue the discussion on Arduino. In the sixth post on Arduino, we add the time element to a temperature sensor reading and see the output in serial monitor. We will use the same environment as in the first post of this series

In the last post, by importing a library called TimeLib.h we added the time element to Arduino. For the temperature reading, we will add LM35 sensor. Details about the sensor is here.

The code is shown below:

#include <TimeLib.h>

int tempPin = 0;     // initialize the number of temperature sensor output data pin
int temp_reading;    // declare variable for temperature reading
float temp_celcius;  // declare variable for temperature reading

void setup() {
  Serial.begin(9600);  // initialize serial port
  setTime(1542982538); //The argument to setTime must be Unix time 
}

void loop() {
  temp_reading = analogRead(tempPin);
  temp_celcius = (temp_reading/1024.0)*500; 
  digitalClockDisplay();
  Serial.print(" : ");
  Serial.println(temp_celcius); //temperature in Celcius
  delay(5000);
}

void digitalClockDisplay(){
  // ref https://github.com/PaulStoffregen/Time
  // digital clock display of the time
  Serial.print(day());
  Serial.print("/");
  Serial.print(month());
  Serial.print("/");
  Serial.print(year()); 
  Serial.print(" ");
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
}

void printDigits(int digits){
  // ref https://github.com/PaulStoffregen/Time
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

The circuit is shown below:




















Upload above program into Arduino. The serial monitor output is shown below:



















This concludes the post on adding time element to a temperature reading in Arduino