Things used in this project

Hardware components:
Psoc 20analog 20coprocessor 20pioneer 20kit 20web
Cypress PSoC Analog Coprocessor Pioneer Kit
×1
11026 02
Jumper wires (generic)
×1
Mfr 25frf52 100r sml
Resistor 100 ohm
×2
Vishay semiconductor ild74 image 75px
Opto-Isolator
×2
12002 04
Breadboard (generic)
×1
Software apps and online services:
Creator bannerimage 2
Cypress PSoC Creator

Schematics

Fans schematic circuit.
Schematic1 9cwgwhrvur
LM35 schematic circuit.
Schematic2 tpxq6exyot

Code

PSoC code - Asthma and rhinitis habitat in a smart home.C/C++
Monitor humidity and temperature conditions in a bedroom.
/******************************************************************************
* Project Name      : Asthma and rhinitis habitat in a smart home 
* Developer         : Andres Sanchez
* Software Used     : PSoC Creator 4.0
* Related Hardware  : CY8CKIT-048 PSoC Analog Coprocessor Pioneer Kit 
*******************************************************************************/

/* Header File Includes */
#include <project.h>
#include <stdio.h>

//CONSTANTS
/* Constants used to calculate humidity */
/* This is the capacitance of the sensor at 55% RH with 0.1pF resolution */
#define CAPACITANCE_AT_55_RH        (1800)
/* Sensitivity numerator and denominator indicate sensitivity of the sensor */
#define SENSITIVITY_NUMERATOR       (31)
#define SENSITIVITY_DENOMINATOR     (100)
/* Value of reference capacitor.  Note that this value includes the pin capacitance
    and the physical 180pF reference capacitor */
#define CREF                        (1930)
/* Offset Capacitance */
#define COFFSET                     (150)
/* This is raw count equivalent to trace capacitance */
#define OFFSETCOUNT                 (1536)
#define BUFFERSIZE                  (8)
#define READ_WRITE_BOUNDARY         (0)
/* Nominal humidity 55% */
#define NOMINAL_HUMIDITY            (550)
#define HUMIDITY_0_PERCENT          (0)
#define HUMIDITY_100_PERCENT        (1000)
#define HUMIDITY_50                 (500)   

#define ADC_CHANNEL_VREF			(0u)
#define ADC_CHANNEL_VTH				(1u)
#define LED_ON						(0u)
#define LED_OFF						(1u)
#define TEMPERATURE_THRESHOLD_HIGH	(3000)
#define TEMPERATURE_THRESHOLD_LOW	(2500)
/* IIR Filter coefficients for each signal */
/* Cut off frequency = fs/(2 * pi * iir_filter_constant).  In this project fs ~= 1 ksps.
This results in a cut-off frequency of 4.97 Hz.  We are using IIR filter as FIR requires 
more order of filter to get the same cut-off frequency*/
#define FILTER_COEFFICIENT_TEMPERATURE	(32u)
                                
typedef struct __attribute__((packed))
{
	uint16 humidityRawCounts;	/* Raw count from CapSense Component for the humidity sensor */
	uint16 capacitance;			/* Capacitance of the humidity sensor */
	uint16 humidity;			/* Measured humidity */
	uint16 rawCountsRefCap;     /* Raw count from CapSense Component for the Reference capacitor */
}humidity_sensor_data;

typedef struct __attribute__((packed))
{
	int16 Vth;					/* Voltage across thermistor */
	uint16 Rth;					/* Thermistor resistance */
	int16 temperature;			/* Measured temperature */
}temperature_sensor_data;

/*************************************Function Prototypes**********************************************/
void Inicializar(void);
void Comunicacion(void);
void Lectura_Humedad(void);
void Lectura_Temperatura(void);
void Analisis(void);
__inline uint16 CalculateCapacitance(uint16 rawCounts, uint16 refSensorCounts);
__inline uint16 CalculateHumidity(uint16 capacitance);
//////////////////////////////////////////////////////////////////////////////////////////////////////////

humidity_sensor_data ihBuffer = {0, 0, 0, 0};//Humidity buffer.
temperature_sensor_data itBuffer = {0, 0, 0};//Temperature buffer.


/***********************************Global  Variables***************************************************/
uint32 ch;//Char recieved data

char shum[10] ; //Humidity String. 
char stem[10] ; //Temperature String.
char slm[10] ;  //LM35 String.

int16 adcResultVREF, adcResultVTH, adcResultLM35; //Variables to hold the the ADC readings.
 
int16 filterOutputVref=0, filterOutputVth=0, filterOutputlm=0; //Filter input and output variables for Vref, Vth and LM35 measurements.

uint8 interruptState = 0;
 
int automatico = 0; //Variable to enable/disable automatic program.

int fan1=0, fan2=0; //Variables to enable/disable fans.

int16 thermistorResistance, temperature; //Variables to hold calculated resistance and temperature.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


/*******************************************************************************
* Function Name: main
*********************************************************************************/
int main()
{  
    CyGlobalIntEnable;//Enable global interrupts.
    Inicializar();//Initialize hardware resources.
    
    for(;;)      //Infinite Loop.
    {
        Lectura_Humedad();//Humidity reading.
        Lectura_Temperatura();//Temperature Thermistor and LM35 reading.
        Comunicacion();//UART communication.
        Analisis();//Take decisions.
    }
}
void Inicializar(void)
{
    //Humidity.
    CSD_Start();//Start the CapSense component.
    
    //Comunication.
    UART_Start();//Start the UART component.
    
    //Temperature.
    ADC_Start();//Start the Scanning SAR ADC Component.
    ADC_StartConvert();//start conversion of the ADC measurement.
    VrefBuffer_Start();//Start Reference buffer
    PVref_Start();//Start Programmable Voltage Reference
    PVref_Enable();//Enable Programmable Voltage Reference
}
//Calculate the capacitance
__inline uint16 CalculateCapacitance(uint16 rawCounts, uint16 refsensorCounts)
{
    return (uint16)((float)(rawCounts - OFFSETCOUNT) * (CREF - COFFSET) / (float)(refsensorCounts - OFFSETCOUNT));
}
//Calculate the humidity
__inline uint16 CalculateHumidity(uint16 capacitance)
{
    int16 humidity;
    int16 delta;
    
    /* Find capacitance difference from nominal capacitance at 55% RH */
    delta = capacitance - CAPACITANCE_AT_55_RH;
    
    /* Calculate humidity from capacitance difference and sensor sensitivity */
    humidity = ((delta * SENSITIVITY_DENOMINATOR) / SENSITIVITY_NUMERATOR) + NOMINAL_HUMIDITY;
    
    /* If humidity is less than zero, limit it to 0; If humidity is greater than 1000 (100%), limit to 1000 */
    humidity = (humidity < HUMIDITY_0_PERCENT) ? HUMIDITY_0_PERCENT : (humidity > HUMIDITY_100_PERCENT) ? HUMIDITY_100_PERCENT : humidity;

    /* Return Humidity value */
    return humidity;
}
void Lectura_Humedad(void) //Humidity reading.
{
    if(!(CSD_IsBusy()))//Check if the capSense is Avialiable to be read.
        {       
           
            interruptState = CyEnterCriticalSection();
            
                ihBuffer.humidityRawCounts = CSD_BUTTON0_SNS0_RAW0_VALUE;
                ihBuffer.rawCountsRefCap = CSD_BUTTON0_SNS1_RAW0_VALUE;
                /* Convert raw counts to capacitance */
                ihBuffer.capacitance = CalculateCapacitance(ihBuffer.humidityRawCounts, ihBuffer.rawCountsRefCap);
                /* Calculate humidity */
                ihBuffer.humidity = CalculateHumidity(ihBuffer.capacitance);                
   
            CyExitCriticalSection(interruptState);
            /* Scan sensors */
            CSD_ScanAllWidgets();
        }          
      
}
void Lectura_Temperatura(void)//Temperature reading
{
      if(ADC_IsEndConversion(ADC_RETURN_STATUS))
        {
            /* Read the ADC result for reference and thermistor voltages */
            adcResultVREF = ADC_GetResult16(ADC_CHANNEL_VREF);
            adcResultVTH = ADC_GetResult16(ADC_CHANNEL_VTH);
            adcResultLM35 = ADC_GetResult16(2);
            /* Low pass filter the measured ADC counts of Vref */            
            filterOutputVref = (adcResultVREF + (FILTER_COEFFICIENT_TEMPERATURE - 1) * filterOutputVref) / FILTER_COEFFICIENT_TEMPERATURE;
            filterOutputlm= (adcResultLM35 + (FILTER_COEFFICIENT_TEMPERATURE - 1) * filterOutputlm) / FILTER_COEFFICIENT_TEMPERATURE;
            /* Low pass filter the measured ADC counts of Vth */         
            filterOutputVth = (adcResultVTH + (FILTER_COEFFICIENT_TEMPERATURE - 1) * filterOutputVth) / FILTER_COEFFICIENT_TEMPERATURE;
                        
            /* Calculate thermistor resistance */
            thermistorResistance = Thermistor_GetResistance(filterOutputVref, filterOutputVth);           
            
            /* Calculate temperature in degree Celsius using the Component API */
            temperature = Thermistor_GetTemperature(thermistorResistance);
                      
			/* Enter critical section to check if UART bus is busy or not */
            interruptState = CyEnterCriticalSection();
            
            itBuffer.Rth = thermistorResistance;
            itBuffer.temperature = temperature;
            itBuffer.Vth = filterOutputVth;
          
			/* Exit critical section */
            CyExitCriticalSection(interruptState);
        }
}
void Comunicacion(void) //Code for communication
{
    ch = UART_UartGetChar();
    
        if (ch=='a') {automatico=1;}//Send 'a' to Enable automatic mode.
        if (ch=='b') {automatico=0;}//Send 'b' to Disable automatic mode.
        
        if (ch=='w') {fan2=0; fan1=1;}//Send 'w' to enable just Fan1 (Air inlet).
        if (ch=='x') {fan1=0; fan2=1;}//Send 'x' to enable just Fan2 (Air outlet). 
        if (ch=='y') {fan1=1; fan2=1;}//Send 'y' to enable both Fans.
        if (ch=='z') {fan1=0; fan2=0;}//Send 'z' to disable both Fans.
        
        sprintf(stem,"%u",itBuffer.temperature); //Convert to String Temperature value
        sprintf(shum,"%u",ihBuffer.humidity); //Convert to String Humidity value
        sprintf(slm,"%u",filterOutputlm); //Convert to String LM35 Value.
       
        if (ch=='p') {//Send 'p' to get the environment measurement.
        
        UART_UartPutString("Start\r\n"); // Start indicator.
        UART_UartPutString("Temperatura:\r\n");
        UART_UartPutString(stem); // Send Thermistor convrted value.
        UART_UartPutString("Humedad:\r\n" );
        UART_UartPutString(shum); // Send Humidity converted value.
        UART_UartPutString("LM35:\r\n");
        UART_UartPutString(slm); // Send LM35 converted value.
        UART_UartPutString("\r\n");
        UART_UartPutString("Stop\r\n"); // Stop indicator.
        }
}
void Analisis(void) //Code to actuate fans
{
    if (automatico==1) //Code for automatic mode
    {
        if(ihBuffer.humidity > HUMIDITY_50 & filterOutputlm < 1000)
        {
            inair_Write(1); outair_Write(1);//Air inlet and outlet.
        }
        if(ihBuffer.humidity > HUMIDITY_50 & filterOutputlm > 1000)
        {
            inair_Write(0); outair_Write(1);//Just air outlet.
        }
        if(ihBuffer.humidity < HUMIDITY_50 & filterOutputlm < 1000)
        {
            inair_Write(0); outair_Write(0);//No fan activation.
        }
       if(ihBuffer.humidity < HUMIDITY_50 & filterOutputVth < 2500)
        {
            inair_Write(1); outair_Write(0);//Just air inlet.
        }
    }
    if (automatico==0) //Code for user mode.
    {
       if (fan1 == 1)  inair_Write(255);  //Air inlet fan enabled.
       if (fan2 == 1)  outair_Write(255); //Air outlet fan enabled.
       if (fan1 == 0)  inair_Write(0);    //Air inlet fan disabled.
       if (fan2 == 0)  outair_Write(0);   //Air inlet fan disabled.
    }
}
/* [] END OF FILE */

Credits

15492331 351560078550761 73659207954869482 n
Sánchez Andrés

I'm a Control Engineering student!

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Feedback