Arduino Tutorial: How to use Arduino to monitor light and temperature levels inside a room.

Scritto da Alessandra De Rosa on . Postato in Prodotti, Progetto Leonardo da Vinci, Servizi, Tutorials Arduino

For this project, we are going to show you how to mount or create the same type of scheme or project that we’ve done.
The project consists of a display of temperature and light levels on the room the sensors will be.

 

For this project we used:

-1 Arduino uno

-1 Breadborad

-1 Button

-1 lcd

-1 Potentiometer

-4 Leds

-6 Resistances

-1 Temperature sensor————————-LM35

-1 Light sensor (photo resistance)

-1 Relay ————————————SRD-05VDC-SL-C

 

 

First part of the Tutorial:

The first thing, if you are new to arduino, is working with digital ports.
The digital ports are used to control Leds, Display’s, Relay’s, and more…
This is the sketch of what you have to have to start working with digital entry’s:

arduino digital entry

So the code for this is not hard.
You must have the 2 main functions void setup() and void loop().
Before the two functions, you should create int variables to store the number of the pin you want to use, either digital or analogic, you just need to put in them the number of the pin.
The void setup is where you define what ports are going to be, for example on the sketch the digital port 2 is going to be on output mode.
You do it using the function pinMode(,INPUT/OUTPUT);
Also, in the setup you should use the function Serial.begin(9600); to be able to see the data on the arduino serial monitor.
After the setup of all the ports we are going to make the loop function, the one which is constantly running on arduino.

This first sketch uses the ports 2,3,4 to light up led’s.
So you should just use the function digitalWrite(,HIGH/LOW);
The high frequency means the led is on and the Low means off.

Example:

digitalWrite(digi1,HIGH);

If you only run this this on your loop function, the led will always stay on.

 

 Second part of the Tutorial:

This second sketch will show you how to work with analogic ports.
The analogic ports are used to receive data from the sensors or to send different types of signals instead of HIGH or LOW you can send different signals.

arduino analogic ports

A little warning towards the sensor usage, if the sensors are built in the board wrong they heat up and you can ruin a sensor.
So always make sure that you have the voltage and ground on the right positions, and the correct amount of voltage, for that you will need to use AnalogReference(INTERNAL) if you are using arduino UNO.
The 2 principal functions are always required in order for arduino to work, so it’s basically the same as before.
In this sketch we have a relay, which lets you create an on/off switch. It’s really useful for creating Emergency Stop buttons, in cases like the sensors getting too hot.
The relay is controlled by a digital port in OUTPUT mode, this is the same as the leds HIGH for on, Low for off.
We have 2 sensors here just to serve as example of what you can do with all the sensors….
In the loop function instead of now using digitalWrite or digitalRead or any other function related to digital ports, we are going to use analogRead(); because we want to receive the data from the sensors instead of sending data to the sensors.
In order to use the funtion analogRead() you need to store the data in a variable of the int type.

 

Example:

 = analogRead();

 

So after mounting the sensors on the board and arduino correctly, the code for this part is:

 

Example:

void loop()
{
 int data=0;

 data = analogRead(an1);
 
 Serial.println(an1);
 delay(5000);
}

The Serial.println(); function prints a line on the serial console of arduino that lets you read the data returned by the sensor.
Make sure to create a delay on your loop function to ensure that you have time to read the things displayed on the serial console.

 

Third part of the Tutorial:

The third step towards the final project is using a LCD to display the Data.
The Display requires a special library named , this library allows you to have variable LiquidCrystal that correspond to the display.
To use it on the first line you write : #include
In the sketch you can see how to connect the display with 16 pins to the arduino and the potentiometer, the potentiometer is needed to adjust the display settings (brightness and contrast).

arduino linked to a display

For this we are going to need to do a bit more of code.
The first big thing is defining where the display is connected, you need to create a variable of the type LiquidCrystal with the name that you want and initialize it, in this way:

 

Example:

 LiquidCrystal lcd(12,11,7,6,5,4);

 

The numbers are correspondent of the connected display on arduino from the highest pin to the lowest. This will allow the library to display correctly the things you want to display.
In void setup you also have to put the initialization of the display, the lines and columns that the display will have.

 

Example:

 lcd.begin(16, 2);<br />

The code for this is easy to understand.
To display things in the display you use the name of the variable that you have created a dot and the word print.

 

Example:

 lcd.print(""); <br />

or

 lcd.print();<br />

Another usefull comand is lcd.setCursor( ,
); basically allows you to choose where you want the text.

 

Final Tutorial:

Now it’s time to put it all together using all the things learned from the 3 other tutorials.
For this part we are going to use all the components showed on the beginning of the tutorial:

-1 Arduino uno
-1 Breadborad
-1 Button
-1 lcd
-1 Potentiometer
-4 Leds
-6 Resistances
-1 Temperature sensor————————-LM35
-1 Light sensor (photo resistance)
-1 Relay ————————————SRD-05VDC-SL-C

 

This part is going to use the sensors to read the data as soon as you press the button and after the loading process / progress bar on the leds and on the lcd, it will be displayed the temperature and the light level on a percentage and light up a led on a scale from bad to good temperature.
So to make this work here is our sketch:

Arduino Sensors Display

Arduino Stand-by Bar

Arduino Progress Bar

Arduino sensor and display

The code for this part is very “user friendly”, I mean it’s meant to be graphical and functional at the same time.

We used all the functions explained above and a couple of interface tricks.
The Delays are essential for this part because they are the ones which time all the progress.

Here we used this code :

 

#include <LiquidCrystal.h>


int rele=12;//rele

int pdigital1=9; // 25%
int pdigital2=10;
int pdigital3=11;//75%
int pdigital5=13;//100%

int button=8; //button 

int an1= 5; // sensor Temp
int an2= 3; //sensor light


LiquidCrystal lcd(7,6,5,4,3,2);

float vrif = 1.1;

void setup()
{
  // Leds:
  pinMode(pdigital1,OUTPUT);
  pinMode(pdigital2,OUTPUT);
  pinMode(pdigital3,OUTPUT);

  pinMode(pdigital5,OUTPUT);
  // Sensors:
  pinMode(an1,INPUT);
  pinMode(an2,INPUT);
  //button
  pinMode(button,INPUT);
  //voltage of the sensors
  analogReference(INTERNAL);
  //lcd cols and rows
  lcd.begin(16, 2);
  
  Serial.begin(9600);
  
}

void loop()
{
  int x=0; 
  float val=0, light=0; 
  float TempAverage=0.0;
  
  digitalWrite(rele,LOW);	   			//Turning the switch off
  
  lcd.setCursor(0,0);					//Display Something on the screen, in our case the name of the company
  lcd.print("                ");
  lcd.setCursor(0,1);
  lcd.print("                ");
  lcd.print("HydraSolutions!");
   
  if(digitalRead(button)==LOW)				//If there is no button press it just waits
  {
    delay(500);
  }
  else							//Otherwise it will run all the program 
  {
    digitalWrite(rele,HIGH);				//Switch ON - energy to all the circuit
    val=analogRead(an1);				//Get the light and the temperature
    light=analogRead(an2);
    
    
     lcd.setCursor(0,0);					//Cleaning the display
     lcd.print("                ");
     lcd.setCursor(0,1);
     lcd.print("                ");
     
     lcd.setCursor(0,0); 			//the beggining of the progress bar on the led's and on the display
     lcd.print("Loading:25%");				//25%
     digitalWrite(pdigital1,HIGH);delay(2000); 		//2 second until the 50%
     
     lcd.setCursor(0,0);
     lcd.print("Loading:50%"); 				//50%
     digitalWrite(pdigital2,HIGH);delay(2000); 		//2 second until the 75%

     lcd.setCursor(0,0);
     lcd.print("Loading:75%");				//75%
     digitalWrite(pdigital3,HIGH);delay(2000);  		//2 second until the 100%
      
     lcd.setCursor(0,0);
     lcd.print("Loading:100%");				//100% = Complete
     digitalWrite(pdigital5,HIGH);delay(2000); 		//2 second until the display of the light and temperature conditions
     
     delay(2000);
     lcd.setCursor(0,0);					//Cleaning the display
     lcd.print("                ");
     lcd.setCursor(0,1);
     lcd.print("                ");
     digitalWrite(pdigital1,HIGH);digitalWrite(pdigital2,HIGH);digitalWrite(pdigital3,HIGH);digitalWrite(pdigital5,HIGH);							 //cleaning the leds
     delay(1000);
     digitalWrite(pdigital1,LOW);digitalWrite(pdigital2,LOW);digitalWrite(pdigital3,LOW);digitalWrite(pdigital5,LOW);
     
    
     
     
      TempAverage += (95.0 * vrif * val) / 1024.0;  	//the equation to make the signal returned from the sensor into temperature
     
     if(TempAverage>32) 		//from here we have the conditions to choose a led from the scale 
     {
       digitalWrite(pdigital1,HIGH);
     }
     if(TempAverage<32 && TempAverage>28)
     {
       digitalWrite(pdigital2,HIGH);
     }     
    
     if(TempAverage<28 && TempAverage>27)
     {
       digitalWrite(pdigital3,HIGH);
     } 
      if(TempAverage>=24 && TempAverage <=26)
     {
       digitalWrite(pdigital5,HIGH);
     } 
      if(TempAverage<18) 
     {
       digitalWrite(pdigital1,HIGH);
     }
     if(TempAverage<22 && TempAverage>18)
     {
       digitalWrite(pdigital2,HIGH);
     }     
      if(TempAverage<24 && TempAverage>22)
     {
       digitalWrite(pdigital3,HIGH);
     } 
     
     
      
      Serial.println(TempAverage);			//here we print on the serial console the temperature and the light level
      Serial.println(light);
     
     light*=100;						//determining the % of light on the room
     light/=1023;
     
     lcd.setCursor(0,0);					//Displaying the temperature and the light levels
     lcd.print("Temperatura:");lcd.print(TempAverage);
     lcd.setCursor(0,1);
     lcd.print("Light:");lcd.print(light); lcd.print("%");
     delay(10000);					//waiting 10 seconds
      
      lcd.setCursor(0,0);					//cleaning the display
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("                ");
      digitalWrite(rele,LOW);				//turning everything off 
      digitalWrite(pdigital1,LOW);
      digitalWrite(pdigital2,LOW);
      digitalWrite(pdigital3,LOW);
      digitalWrite(pdigital5,LOW);
      
    }
}

This was our tutorial on how to have a display with the temperature and light levels using a button press with arduino.

Alexandre Leitao
Gonçalo Neto

Google+