DHT 22 Automatic Irrigation System

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
MahdiFelah00
Posts: 1
Joined: 10 May 2022 22:56

DHT 22 Automatic Irrigation System

#1 Post by MahdiFelah00 » 10 May 2022 23:10

Hello Guys,

I'm a beginner in Microcontrollers Programming trying to make an automatic Irrigation System Using DHT22 For a project and i've been trying to set off a motor when Humidity levels reach 30% but i can't seem to make it work

If you can help me please post below and thank you in advance

Here is the full code:

/**************************************************************************************

Interfacing DHT22 (AM2302 - RHT03) sensor with PIC16F887 microcontroller.
C Code for mikroC PRO for PIC compiler
Internal oscillator used @ 8MHz
Configuration words: CONFIG1 = 0x2CD4
CONFIG2 = 0x0700
This is a free software with NO WARRANTY.
https://simple-circuit.com/

***************************************************************************************/


// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// end LCD module connections

// DHT22 sensor connection (here sensor data pin is connected to pin RC0)
sbit DHT22_PIN at RC0_bit;
sbit DHT22_PIN_Direction at TRISC0_bit;
// End DHT11 sensor connection

char temperature[] = "Temp = 00.0 C ";
char humidity[] = "00";
unsigned short T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
unsigned int Temp, RH;
int i,pluie,nb_panne=0;
int intrp_ouv,intrp_ferm,affich_panne;
char txt[20];
int NB;
int hum;

void interrupt() {
if(INTCON.T0IF==1){
INTCON.T0IF=0;
NB--;
if(NB==0){
PORTA.RA6=0;
INTCON.T0IE=0; //desactiver timer
}
}

if(INTCON.INTF==1){//Pluie
if(PORTB.RB0==1){
PORTA.RA2 = 0;
//PORTD.RD6 = 0;
pluie=1;
}
INTCON.INTF=0; //Fin Pluie
}else if(INTCON.RBIF==1){
if(PORTB.RB4==1)
{
PORTA.RA2=1; //Arrosage
intrp_ouv=1;
}
if(PORTB.RB4==0)
{
PORTA.RA2=0; //Arrosage
//PORTD.RD6=0; //Buzzer
intrp_ferm=1;
}
if(PORTB.RB5==1){
affich_panne=1;
}
INTCON.RBIF=0;
}
}


void Start_Signal(void) {
DHT22_PIN_Direction = 0; // Configure connection pin as output
DHT22_PIN = 0; // Connection pin output low
delay_ms(25); // Wait 25 ms
DHT22_PIN = 1; // Connection pin output high
delay_us(25); // Wait 25 us
DHT22_PIN_Direction = 1; // Configure connection pin as input
}

unsigned short Check_Response() {
TMR1H = 0; // Reset Timer1
TMR1L = 0;
TMR1ON_bit = 1; // Enable Timer1 module
while(!DHT22_PIN && TMR1L < 100); // Wait until DHT11_PIN becomes high (checking of 80µs low time response)
if(TMR1L > 99) // If response time > 99µS ==> Response error
return 0; // Return 0 (Device has a problem with response)
else { TMR1H = 0; // Reset Timer1
TMR1L = 0;
while(DHT22_PIN && TMR1L < 100); // Wait until DHT11_PIN becomes low (checking of 80µs high time response)
if(TMR1L > 99) // If response time > 99µS ==> Response error
return 0; // Return 0 (Device has a problem with response)
else
return 1; // Return 1 (response OK)
}
}

unsigned short Read_Data(unsigned short* dht_data) {
short i;
*dht_data = 0;
for(i = 0; i < 8; i++){
TMR1H = 0; // Reset Timer1
TMR1L = 0;
while(!DHT22_PIN) // Wait until DHT11_PIN becomes high
if(TMR1L > 100) { // If low time > 100 ==> Time out error (Normally it takes 50µs)
return 1;
}
TMR1H = 0; // Reset Timer1
TMR1L = 0;
while(DHT22_PIN) // Wait until DHT11_PIN becomes low
if(TMR1L > 100) { // If high time > 100 ==> Time out error (Normally it takes 26-28µs for 0 and 70µs for 1)
return 1; // Return 1 (timeout error)
}
if(TMR1L > 50) // If high time > 50 ==> Sensor sent 1
*dht_data |= (1 << (7 - i)); // Set bit (7 - i)
}
return 0; // Return 0 (data read OK)
}

void main() {
INTCON.GIE = 1; //(Activer les interruptions)
INTCON.RBIE = 1; //(Activer les interruptions RB4-->RB7)
INTCON.INTE= 1; //(Activer les interruption RB0)
OPTION_REG.INTEDG= 1 ;
intrp_ouv=0;
pluie=0;
intrp_ferm=0;
nb_panne=0;

OSCCON = 0X70; // Set internal oscillator to 8 MHz
ANSELH = 0; // Configure all PORTB pins as digital
T1CON = 0x10; // Set Timer1 clock source to internal with 1:2 prescaler (Timer1 clock = 1 MHz)
TMR1H = 0; // Reset Timer1
TMR1L = 0;
Lcd_Init(); // Initialize LCD module
Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off
Lcd_Cmd(_LCD_CLEAR); // clear LCD
for(;;)
{
while(1) {

Start_Signal(); // Send start signal to the sensor
if(Check_Response()) { // Check if there is a response from sensor (If OK start reading humidity and temperature data)
// Read (and save) data from the DHT11 sensor and check time out errors
if(Read_Data(&RH_byte1) || Read_Data(&RH_byte2) || Read_Data(&T_byte1) || Read_Data(&T_byte2) || Read_Data(&Checksum)) {
Lcd_Cmd(_LCD_CLEAR); // clear LCD
lcd_out(1, 5, "Time out!"); // Display "Time out!"
}
else { // If there is no time out error
if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)) {
// If there is no checksum error
RH = RH_byte1;
RH = (RH << 8) | RH_byte2;
Temp = T_byte1;
Temp = (Temp << 8) | T_byte2;
if (Temp > 0X8000){
temperature[6] = '-';
Temp = Temp & 0X7FFF;
}
else
temperature[6] = ' ';
temperature[7] = (Temp / 100) % 10 + 48;
temperature[8] = (Temp / 10) % 10 + 48;
temperature[10] = Temp % 10 + 48;
if(RH == 1000) // If the relative humidity = 100.0 %
humidity[6] = 1 + 48; // Put 1 of hundreds
else
humidity[0] = (RH / 100) % 10 + 48;
humidity[1] = (RH / 10) % 10 + 48;
temperature[11] = 223;
hum = humidity; // Put degree symbol (°)
Lcd_out(1,1,hum);
if (hum == 30){PORTA.B2 = 1;}
}
// If there is a checksum error
else {
Lcd_Cmd(_LCD_CLEAR); // clear LCD
lcd_out(1, 1, "Checksum Error!");
}
}
}
// If there is a response (from the sensor) problem
else {
Lcd_Cmd(_LCD_CLEAR); // clear LCD
lcd_out(1, 3, "No response");
lcd_out(2, 1, "from the sensor");
}

if(intrp_ouv==1){
Lcd_cmd(_LCD_CLEAR);
Lcd_out(2,1,"Arrosage Gazon");
intrp_ouv=0;
}
if(intrp_ferm==1){
Lcd_cmd(_LCD_CLEAR);
Lcd_out(2,1,"Fin Arrosage Gazon");
intrp_ferm=0;
}
if(pluie==1){
Lcd_cmd(_LCD_CLEAR);
Lcd_out(2,1,"Pluie !!");
pluie=0;
delay_ms(2000);
Lcd_cmd(_LCD_CLEAR);
Lcd_out(2,1,"Smart Garden");
}






TMR1ON_bit = 0; // Disable Timer1 module
delay_ms(1000); // Wait 1 second

TRISA=0X00;

TRISB=0XFF;



}
}
}
// End of code

Post Reply

Return to “User Projects”