• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to control relay by mcu?

Status
Not open for further replies.

Muhammad Saad

New Member
1.png Hi
I am new to mcu. I want to make a project for controlling two leds. I am using PCW (Pic c compiler)Please help me to write a code to on/off relay through potentiometer reading(5v to A0 pin). I need to on the relay when voltage is under 1.5v and above 4.5v. here is my circuit.
Looking for help.
 

heydonms

Member
I would suggest that you break the task down in to sections. Breadboard your circuit and write code that turns the relay on and off, then write code that measures voltage, then combine them. When you run into a problem that you can't solve, post a detailed description of what you expected to happen and what actually happened and I'm sure someone will be able to assist you to troubleshoot it.

I suspect you will want some EMF suppression on that relay coil, or if you are genuinely only looking to drive LEDs then you could probably do away with the relay altogether.
 

Muhammad Saad

New Member
Hi heydonms
I am posting code here. the code compiles and generates hex file but on proteus it is not giving desired output. i want to on 1st led when voltage is below 1.5v and 2nd led on when voltage exeeds 4.5 v. remember voltage value is from 0-5v and variation is with potentiometer.



#include <main.h>
#fuses NOWDT
#fuses NOLvP
#fuses HS
#fuses NODEBUG
#include<stdio.h>
#include<STDLIB.H>
#use delay(clock=11059200)
#DEFINE relay_on 1
#define relay_off 0
#fuses HS,WDT
unsigned int8 relay,adcvalue, LOWERLIMIT, UPPERLIMIT;
void main(void)
{
setup_adc( ADC_CLOCK_INTERNAL);
setup_adc_ports( ALL_ANALOG );
( input(PIN_A0));
set_adc_channel(0);

relay = read_adc();
LOWERLIMIT = (5*relay*100/1023);
UPPERLIMIT = (5*relay*100/1023);

if (LOWERLIMIT < 2)
output_low(PIN_C0);
Else
output_high(PIN_C0);
if (UPPERLIMIT <= 4)
output_high(PIN_C0);
else
output_low(PIN_C0);
}
 

misterT

Well-Known Member
Most Helpful Member
LOWERLIMIT = (5*relay*100/1023);
UPPERLIMIT = (5*relay*100/1023);

What is the purpose of these variables if you give them exactly the same value?
It is not a good idea to assign 16 bit values to 8 bit variable. Change your variables to be 16 bit.
Your code has plenty of logical problems that you can solve on your own if you pay attention. Biggest mistake is that the program exits the main function (you do not have an infinite loop in your program).

Have you tried just toggling a single output port (PINC0 for example).. can you make that work?
 

JimB

Super Moderator
Most Helpful Member
Muhammad

Your schematic is lacking a few resistors, please see my modified version.
You need a resistor to limit the base current of the transistor.
You also need resistors to limit the current through the LED (unless the LEDs already have resistors built in).

JimB

Micro and LEDS Modified.JPG
 

Muhammad Saad

New Member
LOWERLIMIT = (5*relay*100/1023);
UPPERLIMIT = (5*relay*100/1023);

What is the purpose of these variables if you give them exactly the same value?
It is not a good idea to assign 16 bit values to 8 bit variable. Change your variables to be 16 bit.
Your code has plenty of logical problems that you can solve on your own if you pay attention. Biggest mistake is that the program exits the main function (you do not have an infinite loop in your program).

Have you tried just toggling a single output port (PINC0 for example).. can you make that work?

Thank you misterT for help. actually I am very new to mcu and c language. and also mention where is to put formula (adc). I wrote the code and now want to operate the relay on the following values,

1. first led on when voltage is below 1.5 volts.
2. first led off when voltage is greater than 1.5v.
3. second led on when voltage is greater than 4.5v.
Please rectify the errors.
 

misterT

Well-Known Member
Most Helpful Member
I don't know anything about your compiler or library functions, so I use functions from your code.. I just try to fix some basic mistakes:

C:
/* Calculate upper- and lower-limits */
#define LOWERLIMIT   ((15*1023)/50) /* 1.5 volt limit */
#define UPPERLIMIT   ((45*1023)/50) /* 4.5 volt limit */

/* Variable to hold adc-value.
   I assume 10-bit ADC which gives values from 0 to 1023 */
unsigned int adcvalue;

void main(void)
{
    /* Setup the hardware */
    input(PIN_A0);
    setup_adc(ADC_CLOCK_INTERNAL);
    setup_adc_ports( ALL_ANALOG );

    /* Infinite loop is the heart of every embedded program */
    while(1) {

        /* Read adc channel 0,
           I assume these functions do what the name suggests */
        set_adc_channel(0);
        adcvalue = read_adc();

        /* Control the relay */
        if (adcvalue < LOWERLIMIT) {
            /* Do whatever needs to be done when at low voltage */
            output_high(PIN_C0); /* Set pin C0 high, this should switch ON the relay */
        }
        else if (adcvalue > UPPERLIMIT) {
            /* Do whatever needs to be done when at high voltage */
            output_high(PIN_C0); /* Set pin C0 high, this should switch ON the relay */
        }
        else {
            /* Do whatever needs to be done when between low and high levels */
            output_low(PIN_C0); /* Set pin C0 low, this should switch OFF the relay */
        }

    } /* Go back to while(1) */
}
 
Last edited:

dr pepper

Well-Known Member
Most Helpful Member
You need a diode across the relay coil to protect the transistor from kickback, cathode to +
 

Muhammad Saad

New Member
HI misterT
thanks again the code you provided isn't working like mines but it is better than mine. I am using PIC c compiler. please look at the very first circuit diagram of this page. I need to operate rely for under and over voltage limitations.
lower limit is 1.5v
upper limit is 4.5 v.

I also attached the proteus file.
Looking forward for reply
 

Attachments

  • 21.9 KB Views: 114

misterT

Well-Known Member
Most Helpful Member
I need to operate rely for under and over voltage limitations.
lower limit is 1.5v
upper limit is 4.5 v.
I just wanted to help you with the general program "structure" or "idea". I don't know your compiler or libraries and I don't have time to learn them.. that is your job. I modified my code to include transistor control (setting pin C0 high). Google is your friend when learning. Google for tutorials and examples for your compiler.

Have you tested the code? is it working.. even compiling..? Don't forget to add the necessary #includes that I left out from my example.
 

Muhammad Saad

New Member
Thanks misterT.
I have tested the code it is compiling with no errors and generating hex file. but relay is only on from all adc values it is not switching off. Thanks for help
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top