Saturday, September 13, 2014

My First Arduino Project

I had this Basic Arduino Kit from the US (and got it to pass the Jordanian customs) and started learning it.

The first tutorial was about the thermal detector or temperature sensor.  It was a great achievement for me to get it to read the temperature and get it to reflect it on the serial monitor. I also read the first digital lesson regarding lightining up the LED. So now I had to use my basic knowledge about both in a project and thats what i did.

I combined what I learned about the thermal detection along with LED control to create my first project "The Temperature Detection With Basic Alarm".

The idea basically is simple; it will detect the temperature and if it is greater than or equal 22 it will light up the Red Alarm LED and if it is less than or equal 20 it will light up the Blue Alarm LED, otherwise both LEDs are off (which means that I would like to keep the temperature between 20 and 22 exclusive). When I finished, I thought why not adding an LCD to the combination to show up the temperature and that's what I did. Of course this could be connected to an AC controller where you can control the temperature as desired.

Below is the code along with pictures for the circuit.

// Detecting temperture and based on it will light up the red alarm LED
// if the temp grt or equal 22 and will light up the blue alarm LED
// if the is less or equal 20 while at the same time will show the temp on the LCD

#include <LiquidCrystal.h>
const int HOT_ALARM_LED = 7;
const int COLD_ALARM_LED = 6;
const int TEMPERATURE_PIN = A0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup ()
{
  Serial.begin (9600);
  
  pinMode (HOT_ALARM_LED, OUTPUT);
  pinMode (COLD_ALARM_LED, OUTPUT);
  
  lcd.begin(16, 2);
}

void loop ()
{
  while (float fTemp = ReadTemperature())
  {
    if (fTemp >= 22.0)
    {
      digitalWrite (COLD_ALARM_LED, LOW);
      digitalWrite (HOT_ALARM_LED, HIGH);
    }
    else if (fTemp <=20.0)
    {
      digitalWrite (HOT_ALARM_LED, LOW);
      digitalWrite (COLD_ALARM_LED, HIGH);
    }
    else
    {
      digitalWrite (HOT_ALARM_LED, LOW);
      digitalWrite (COLD_ALARM_LED, LOW);
    }
    delay (1000);
  }
}

float ReadTemperature ()
{
  lcd.clear();
  
  float fTemp = 0.0;
  
  fTemp = analogRead (TEMPERATURE_PIN);
  
  fTemp = (fTemp / 1023) * 500;
  
  Serial.print ("Temperature is: ");
  Serial.print (fTemp);
  Serial.println (" C");
  
  lcd.print (fTemp);
  
  return fTemp;
}

No comments: