Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Heater Element Controller

Newbie level 2
Joined
Dec 13, 2023
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
30
Hey there!

I need a code for this:
I have a PIC12F683, an IRFZ44N MOSFET, a green and a red LED, a LM35 temperature sensor, and a heating element(20 ohm). I would like to build a temperature control system. The sensor will get temperature data and if the temperature is lower than 35 celsius, heating will start and if the temperature is above 40 celsius, heating will stop. I want PIC12F683 to read temperature data from LM35 and trigger the gate of the MOSFET IRFZ44N with the conditions I mentioned above. IDE is MPLAB X.

Thanks
 
You can proceed with the code like this.

Code:
#include <xc.h>
#include <stdint.h>

#define _XTAL_FREQ 4000000 // Assuming a 4MHz crystal oscillator

// Configuration bits
#pragma config FOSC = INTRCIO   // Internal oscillator
#pragma config WDTE = OFF       // Watchdog Timer disabled
#pragma config PWRTE = OFF      // Power-up Timer disabled
#pragma config MCLRE = OFF      // MCLR pin is digital I/O
#pragma config CP = OFF         // Code protection disabled
#pragma config CPD = OFF        // Data Code Protection disabled
#pragma config BOREN = OFF      // Brown-out Reset disabled
#pragma config IESO = OFF       // Internal/External Switchover disabled
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor disabled

// Define connections
#define LM35_PIN    GP0         // LM35 sensor connected to GP0 (AN0)
#define MOSFET_PIN  GP2         // IRFZ44N gate pin connected to GP2

// Function to initialize ADC
void ADC_Init() {
    ADCON0 = 0b00000001;    // Select channel AN0 and enable ADC
    ADCON1 = 0b00000000;    // Use VDD and VSS as reference voltage
    TRISIO |= (1 << LM35_PIN); // Set LM35 pin as input
}

// Function to read temperature from LM35
uint16_t Read_Temperature() {
    GO_nDONE = 1;           // Start ADC conversion
    while(GO_nDONE);        // Wait for conversion to complete
    return (ADRESH << 8) | ADRESL; // Combine ADRESH and ADRESL to get temperature value
}

// Function to control heating element
void Control_Heating(uint16_t temp) {
    if (temp < 35) {
        MOSFET_PIN = 1; // Turn on heating
    } else if (temp > 40) {
        MOSFET_PIN = 0; // Turn off heating
    }
}

void main() {
    TRISIO = 0b00000000;    // Set all pins as outputs initially
    CMCON = 0x07;           // Disable comparators

    ADC_Init();             // Initialize ADC

    while(1) {
        uint16_t temperature = Read_Temperature(); // Read temperature
        Control_Heating(temperature);             // Control heating based on temperature
    }
}
 
Oh wow... Thank you guys for lightning fast reply and being so helpful!!!

Actually, I want to learn how to program PIC MCUs but I have so limited time... I read a lot and also get help from chatGPT (not so useful or I cant understand)...
I also used the DHT11 temp and humidity sensor instead of LM35 because I don't know how to process analog data yet. Sorry for confusion.

Anyway, I worked on it for a while and this came out:

Code:
/*
 * File:   test.c
 */

#include <xc.h>
#include <pic12f683.h>

// PIC12F683 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Detect (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 4000000 // Assuming 4MHz oscillator frequency
#define DHT11_PIN GP0      // Pin connected to DHT11 sensor
#define LED_GREEN GP1      // Green LED pin
#define LED_RED GP2        // Red LED pin
#define HEATING_ELEMENT GP5 // Pin connected to heating element
#define TEMP_LOWER_LIMIT 35
#define TEMP_UPPER_LIMIT 40
// Function prototypes
void dht11_start();
uint8_t dht11_read();
void readDHT11(uint8_t *temp, uint8_t *hum);
void controlHeating(uint8_t temperature);


void main(void) {
    
    uint8_t temperature, humidity;
    
    TRISIO = 0x01; // Set GP0 as input (Temperature Sensor Data), others as output
    GPIO = 0x00; // All output pins low initially
    
    while (1) {
        readDHT11(&temperature, &humidity);
        controlHeating(temperature);
        // Implement LED control logic here based on temperature if needed
    }
}
void dht11_start() {
    TRISIO &= ~(1 << DHT11_PIN); // Set DHT11_PIN as output
    GPIO &= ~(1 << DHT11_PIN); // Pull the data line low
    __delay_ms(18); // Hold the line low for at least 18ms
    GPIO |= (1 << DHT11_PIN); // Release the line
    __delay_us(30); // Wait for DHT11 response
    TRISIO |= (1 << DHT11_PIN); // Set DHT11_PIN as input to read sensor response
}

uint8_t dht11_read() {
    uint8_t data = 0;
    for (int i = 0; i < 8; i++) {
        while (!(GPIO & (1 << DHT11_PIN))); // Wait for the data line to go high (start of data)
        __delay_us(30); // Wait for 30us to ensure it's a valid bit
        if (!(GPIO & (1 << DHT11_PIN))) {
            // If the line is still low after waiting, it's a '0' bit
            data <<= 1; // Shift left by 1 bit
        } else {
            // If the line is high, it's a '1' bit
            data |= 0x01; // OR operation with 0x01
            data <<= 1; // Shift left by 1 bit
            while (GPIO & (1 << DHT11_PIN)); // Wait for the line to go low (end of data)
        }
    }
    return data;
}

void readDHT11(uint8_t *temp, uint8_t *hum) {
    dht11_start(); // Initialize communication
    if (!(GPIO & (1 << DHT11_PIN))) {
        __delay_us(80);
        if (GPIO & (1 << DHT11_PIN)) {
            __delay_us(80);
            *hum = dht11_read(); // Read humidity
            *temp = dht11_read(); // Read temperature
            uint8_t checksum = dht11_read(); // Read checksum
            if (checksum != (*hum + *temp)) {
                // Checksum doesn't match data
                // Handle error or retry reading
            }
        }
    }
}


void controlHeating(uint8_t temperature) {
    if (temperature < TEMP_LOWER_LIMIT) {
        // Turn on heating element
        GPIO |= (1 << HEATING_ELEMENT);
    } else if (temperature > TEMP_UPPER_LIMIT) {
        // Turn off heating element
        GPIO &= ~(1 << HEATING_ELEMENT);
    }
}

I compiled the code both on MPLAB IDE X and Proteus 8 Professional, with no errors. Also proteus simulation works with no errors. I know no errors do not mean it is gonna function as I wish but... After getting errors for days, it is a thing :) And I am a rookie on Proteus.

For now, I didn't add LEDs both in the code and Proteus, I also attach my Proteus file.

THANK YOU FOR SPENDING TIME!

NOTE: I can't attach pdsprj file, zipping it.
--- Updated ---

Oh wow... Thank you guys for lightning fast reply and being so helpful!!!

Actually, I want to learn how to program PIC MCUs but I have so limited time... I read a lot and also get help from chatGPT (not so useful or I cant understand)...
I also used the DHT11 temp and humidity sensor instead of LM35 because I don't know how to process analog data yet. Sorry for confusion.

Anyway, I worked on it for a while and this came out:

Code:
/*
 * File:   test.c
 */

#include <xc.h>
#include <pic12f683.h>

// PIC12F683 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Detect (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 4000000 // Assuming 4MHz oscillator frequency
#define DHT11_PIN GP0      // Pin connected to DHT11 sensor
#define LED_GREEN GP1      // Green LED pin
#define LED_RED GP2        // Red LED pin
#define HEATING_ELEMENT GP5 // Pin connected to heating element
#define TEMP_LOWER_LIMIT 35
#define TEMP_UPPER_LIMIT 40
// Function prototypes
void dht11_start();
uint8_t dht11_read();
void readDHT11(uint8_t *temp, uint8_t *hum);
void controlHeating(uint8_t temperature);


void main(void) {
   
    uint8_t temperature, humidity;
   
    TRISIO = 0x01; // Set GP0 as input (Temperature Sensor Data), others as output
    GPIO = 0x00; // All output pins low initially
   
    while (1) {
        readDHT11(&temperature, &humidity);
        controlHeating(temperature);
        // Implement LED control logic here based on temperature if needed
    }
}
void dht11_start() {
    TRISIO &= ~(1 << DHT11_PIN); // Set DHT11_PIN as output
    GPIO &= ~(1 << DHT11_PIN); // Pull the data line low
    __delay_ms(18); // Hold the line low for at least 18ms
    GPIO |= (1 << DHT11_PIN); // Release the line
    __delay_us(30); // Wait for DHT11 response
    TRISIO |= (1 << DHT11_PIN); // Set DHT11_PIN as input to read sensor response
}

uint8_t dht11_read() {
    uint8_t data = 0;
    for (int i = 0; i < 8; i++) {
        while (!(GPIO & (1 << DHT11_PIN))); // Wait for the data line to go high (start of data)
        __delay_us(30); // Wait for 30us to ensure it's a valid bit
        if (!(GPIO & (1 << DHT11_PIN))) {
            // If the line is still low after waiting, it's a '0' bit
            data <<= 1; // Shift left by 1 bit
        } else {
            // If the line is high, it's a '1' bit
            data |= 0x01; // OR operation with 0x01
            data <<= 1; // Shift left by 1 bit
            while (GPIO & (1 << DHT11_PIN)); // Wait for the line to go low (end of data)
        }
    }
    return data;
}

void readDHT11(uint8_t *temp, uint8_t *hum) {
    dht11_start(); // Initialize communication
    if (!(GPIO & (1 << DHT11_PIN))) {
        __delay_us(80);
        if (GPIO & (1 << DHT11_PIN)) {
            __delay_us(80);
            *hum = dht11_read(); // Read humidity
            *temp = dht11_read(); // Read temperature
            uint8_t checksum = dht11_read(); // Read checksum
            if (checksum != (*hum + *temp)) {
                // Checksum doesn't match data
                // Handle error or retry reading
            }
        }
    }
}


void controlHeating(uint8_t temperature) {
    if (temperature < TEMP_LOWER_LIMIT) {
        // Turn on heating element
        GPIO |= (1 << HEATING_ELEMENT);
    } else if (temperature > TEMP_UPPER_LIMIT) {
        // Turn off heating element
        GPIO &= ~(1 << HEATING_ELEMENT);
    }
}

I compiled the code both on MPLAB IDE X and Proteus 8 Professional, with no errors. Also proteus simulation works with no errors. I know no errors do not mean it is gonna function as I wish but... After getting errors for days, it is a thing :) And I am a rookie on Proteus.

For now, I didn't add LEDs both in the code and Proteus, I also attach my Proteus file.

THANK YOU FOR SPENDING TIME!

NOTE: I can't attach pdsprj file, zipping it.
Screenshot 2023-12-13 184941.png
 

Attachments

  • test.rar
    22.1 KB · Views: 106
Last edited:

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top