Monday, 7 April 2014

SY-HS 220 Humidity Sensor Interfacing with AVR Atmega (8/16/32)

SY HS220 is the Relative Humidity Sensor with near linear output reference to relative humidity.
Its opereating RH percentage is 30-90% with Output Voltage range 990-2970mV.

Here we've interfaced SY HS220 with AVR ATMEGA8 microcontroller and display %RH in LCD.

Circuit Diagram:

undefined
ElecDude: HS220 AVR Circuit
Code:
/* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Author: ElecDude
         admin@elecdude.com      

Copyright - 2014 - ElecDude

DISCLAIMER:

 THIS SOURCE FILE MAY BE USED AND DISTRIBUTED WITHOUT      
 RESTRICTION PROVIDED THAT THIS COPYRIGHT STATEMENT IS NOT 
 REMOVED FROM THE FILE AND THAT ANY DERIVATIVE WORK CONTAINS
 THE ORIGINAL COPYRIGHT NOTICE AND THE ASSOCIATED DISCLAIMER.

 This is provided without any  express or implied warranties,
 including, but not limited  to, the implied warranties of merchantability
 and fitnessfor a particular purpose. FOR EDUCATIONAL PURPOSE ONLY.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
                          SY-HS 220 Humidity Sensor
     Input voltage = 5.0V                    Operating Temperature = 0-60'C
    Operating Humidity = 30-90% RH        Output Voltage = 990-2970mV
    Std.output = 1.98V   at 25`C 60%RH    Accuracy = +/-5%RH  at 25'C & 60%RH

Normal Values
%RH   mV
 30      990
 40     1300
 50     1650
 60     1980
 70     2310
 80     2640
 90     2970

    V-RH RATIO = 0.30301    by linear slope
    %RH = RATIO * Vout

ADC    Vadc = Vref * adcval/1024

If Vref= Vcc= 4.96, then
    %RH= Vout * 0.30301
       = Vref * adcval/1024 * 0.30301
    %RH= 0.0001467 * adcval
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~*/


#define F_CPU 1000000UL
/*****************MACRO's DEFINITION*********************************/
#ifndef BIT
#define BIT(x)    _BV(x)
#endif
#ifndef SETBIT
#define SETBIT(x,b)     x|=_BV(b);
#endif
#ifndef CLEARBIT
#define CLEARBIT(x,b)     x&=~_BV(b);
#endif
#ifndef TOGGLEBIT
#define TOGGLEBIT(x,b)     x^=_BV(b);
#endif
#ifndef CHECKBIT
#define CHECKBIT(x,b)     (x & _BV(b))
#endif

#include <avr/io.h>
#include <util/delay.h>
void WaitMs(unsigned int ms) // waits (pauses) for ms milliseconds
{
    unsigned int m;
    for(m=0;m<=ms/10;m++)
    {
        _delay_ms(10);
    }
}

#include "ADCHEAD.H"
#include "lcd.c"

#define PRT PIND
#define sw1 0
#define sw2 1
#define sw3 2
#define TIME 100
register unsigned char i asm("r17");

#define R 0.148
unsigned char count=0x00,chng=1;
unsigned int val,th;
char d2[]="00.0%";
void UpdateVal()
{
    float rh;
    uint16_t x;
    rh=R*val; //xx.y
    x=rh*10; //xxy
    d2[3]= (x%10) | 0x30;//ones
    x/=10;
    d2[1]= (x%10) | 0x30;//tens
    x/=10;
    d2[0]= (x%10) | 0x30;//hund
}

int main()
{
      CLEARBIT(DDRC,4)//set as i/p for adc
    CLEARBIT(DDRC,5)
    DDRD=0xF8;//enable PD as i/p & en pull ups
    PORTD=0x07;
    SETBIT(DDRB,7)
    SETBIT(PORTB,7)
   
    _delay_ms(10);
    LCD_init(COFF);
    _delay_ms(100);
    LCD_putsPXY(3,0,"Welcome to");
    LCD_putsPXY(4,1,"ElecDude");
    ADC_init();
   
    WaitMs(800);//wait for some time to initialise
    LCD_clear();
    SetCH(0x05); //PC5=HS220's Vout
    val=0;i=0;
    LCD_putsPXY(0,0,"Relativ Humidity");
    while(1)
        {
            TOGGLEBIT(PORTB,7)
            val+=ADC_readcurch();
            i++;       
            if(i==4)
             {
                val=val/4;
                UpdateVal();
                LCD_putsXY(5,1,d2);
                i=0; val=0; //clear after update
             }// endof if(i==4)
         WaitMs(200);
        }//end of while
return 0;
}

ADCHEAD.H

#define ADC_ENABLE()         SETBIT(ADCSRA,ADEN)        //Macro to enable ADC Module.
#define ADC_DISABLE()         ADCSRA &= 0x7F            //Macro to disable ADC MOdule.
#define ADC_START_CONV()     SETBIT(ADCSRA,ADSC)        //Macro to start ADC conversion.
//#define ADC_STOP_CONV()        CLEARBIT(ADCSRA,ADSC)    //Macro to stop ADC conversion.
#define ADC_CLEAR_ADIF()     CLEARBIT(ADCSRA,ADIF)        //Macro to clear ADC Interrupt flag.

#define VREF 4.96        //Defines the VREF used.
/*            Value in Volts = ADC Value * (VREF/1024)
            ADC resolution = 4.96/1024 = 4.84mV            */
/*************************************************************************************
                    ACD initialization routine
*/

void ADC_init(void)    //Function used to initialise the ADC Module.
{
    ADMUX = 0x45;         //Select the channel ADC5, AVcc, ADC right adjust, chn=0-5
    ADCSRA = 0x83;     // 

    _delay_ms(150);     //Provide adequate delay to initialise the analog circuitry.

}


#define ADC_read() ADC
/* To read & return adc value from a channel (chn=> 0 to 5)    */
#define SetCH(chn) ADMUX=(ADMUX & 0xF0) | (chn & 0x07)
                                        // 07- because Max 5 channels for ATM8
int ADC_readch(unsigned char chn)       
{
    ADMUX= (ADMUX & 0xF0) | (chn & 0x0F);
    ADC_CLEAR_ADIF(); //Clear ADC Interrupt Flag
    ADC_START_CONV(); //Start ADC Conversion.
    while(!(CHECKBIT(ADCSRA,ADIF)));//wait for conversion complete
    return(ADC_read());//----read values & return
}

/* TO READ FROM CURRENTLY SET CHANNEL IN ADCMUX.
    Note: This function doesn't change the ADC channel & uses the value set in ADCMUX
          So set the required channel before calling this function   */

int ADC_readcurch()       
{
    ADC_CLEAR_ADIF(); //Clear ADC Interrupt Flag
    ADC_START_CONV(); //Start ADC Conversion.
    while(!(CHECKBIT(ADCSRA,ADIF)));//wait for conversion complete
    return(ADC_read());//----read values & return
}

/***/


Output:
Reactions:

0 comments:

Post a Comment

Search Here...