# microcontroller to drive a relay after testing input voltage

#### khara

Joined Jul 6, 2011
9
hello,

since am really new to all this , i really need your help. i am student in the University and am working on a project , where a solar panel is used to charge some batteries , that are needed to power a computer board and some other appliances. my task is to test the current or the voltage coming from the batteries or charge controller, since the appliances are connected to the charge controller, to see if it is capable of powering the appliances. i need at least 12 V .. the whole thing will need max. 25-30 W .. so the current should be a max of 2.5 A. I have to use a microcontroller (to measure and test the voltage) and a relay, to switch the relay On when the batteries are capable , or to switch it off when the batteries dont supply enough power. my question is , which microcontroller and relay are suitable for such thing? they have to have low power consumption. i hope i made it clear.

thank you

#### ErnieM

Joined Apr 24, 2011
8,020
Your description is clear (even if it sounds like a homework assignment <grin>).

As I understand, you have 1 input, the analog voltage to sense, and one digital output to control a relay. The relay needs to handle 2.5A.

Those are simple requirements, just about any micro controller with an analog to digital converter could handle it. Microchip (the company I use) even has a tiny SOT-5 that could handle it. The smallest devices to serve would be anything like:

PIC10F200, PIC10F202, , PIC10F204, PIC10F206, PIC10F220, PIC10F222, PIC12F508, PIC12F509, PIC12F510, PIC12F519. The most expensive part there is $0.49 (in quantity). I am also fond of their PIC12HV616 which has a built-in shunt voltage regulator which can save you a part or two. Something like a BSS123 MOSFET can be driven direct off one of the pins to switch the relay. One suitable relay would be a Panasonic CP1A-12V which is good for 12 V control and can handle 20 amps Thread Starter #### khara Joined Jul 6, 2011 9 thank you for your reply. It should be simple , it is nothing complicated, but since i dont have any kind of experience in this field, i have no idea what should or shouldnt do or use. is there any kind of code , that is already written ?? i also need a suitable compiler, i have heard of MPLab, but i dont know if i can program PICs with this one. i already have uvision from KEIL , but not really sure if it supports microcontrollers from Microchip. about the program, i do understand it should be something simple, reading the input voltage and then sending it to the comparator to be compared with predefined values, and then according to the result , the output port pin should be high or low. about driving the relay, sometimes the output voltage of the pin is not enough to turn the relay on and off, so a transistor is used. in some cases another transistor should be used to protect the other transistor. again thank u for ur help Thread Starter #### khara Joined Jul 6, 2011 9 i also found this "http://www.dnatechindia.com/index.php/Tutorials/8051-Tutorial/Relay-Interfacing.html" , it explains how a relay is driven using a microcontroller. the question is do i always need the second transistor to protect the first one. or is it just required for high voltage relays? #### ErnieM Joined Apr 24, 2011 8,020 First, since the M in MPLAB stands for "Microchip" then yes, you can use it for Microchip devices. <evil grin> MPLAB is an excellent programming enviroment. You create your code there inside a project that can collect all the files a project will need. You also get to pick which tools you are using, be they an assembler (comes built into MPLAB), a C (or other) compiler, programmer, in-circuit debugger, and maybe some other stuff I don't use. MPLAB also has a code simulator so you can actually run the code in your PC to see how things are working. MPLAB is a free download and comes with an assembler. I'm not sure if they have a free C compiler that works with the baseline devices, but there is at least one (free) out there that lets you build small projects like this. You will need a programmer such as a PicKit2 or 3. As far as driving that relay, the schematic in Fig 1 is fine. The LED is just there to show you what the relay is doing and may be eliminated with R1. A PIC has a complete totem-pole output meaning it can drive or sink 25 mA so R3 is also not needed, and that leaves R2: something in the area of 1K is fine. His picks for D1 and Q1 are also good. Thread Starter #### khara Joined Jul 6, 2011 9 what does the PicKit do , i mean is it the tool which converts for example the C code to the machine language? dont i need a serial port to connect the the PicKit to the microcontroller? i checked the data sheets of the PICs that you mentioned. there is no UART ports mentioned in these PICs...or did i miss something? i hope its ok , me asking stupid questions , i really appreciate your help. #### ErnieM Joined Apr 24, 2011 8,020 A C compiler converts code to something the PIC can run by making a ".hex" file from it. An assembler does the same. You can use both from MPLAB which is an "environment" program for building programs. PicKits are hardware, has a USB input and a 6 terminal output. They can power small PIC projects themselves, then use the hex file to program the PIC. With most PICs they can even do debugging, actually stop the program running inside your PIC and let you look at all the settings or step thru your code line by line. You can control a PicKit direct from MPLAB. PicKit2 doesn't include some of the very newer devices like the PIC32's, I believe the PicKit 3 can do those too. You didn't mention a UART so I didn't include it when I was searching. Microchip actually has a very nice search by feature page but it is hard to find and is only searchable by product families. I didn't find a UART till I went up to Enhanced Mid-Range Devices where the search engine found "30 of 39 devices." Thread Starter #### khara Joined Jul 6, 2011 9 I was asking about the UART (RS232) because my supervisor told me that we need this port to connect to the microcontroller and download the software. i also remember him telling me about JTAG toolkit .. i guess this kit is for another type of microcontrollers ,maybe ARM uCs !! in my case , if i use PICs and the PicKit , then i dont need a RS232 port?? i think i can connect the PicKit directly to the uC , not sure though. Thread Starter #### khara Joined Jul 6, 2011 9 i actaully forgot to mention that the microcontroller have to be powered by long life batteries. i was now reading about connecting the PIC to the batteries, and the problem of connecting it with higher voltage batteries, in case they supply more than 5 V , which the PIC runs at, then i have a problem. #### ErnieM Joined Apr 24, 2011 8,020 in my case , if i use PICs and the PicKit , then i dont need a RS232 port?? i think i can connect the PicKit directly to the uC , not sure though. Yep, that's how it works. While it is possible to make a PIC program itself you need a more expensive PIC for that. Best to use the "standard" way to program them thru the interface Microchip puts on them; you'll see it when you start picking devices and looking at data sheets. PicKits work with that interface. JTAG is only available on their high end ($) devices.

I would add something like a low dropout regulator between the battery and the PIC to let it run off higher voltages.

#### khara

Joined Jul 6, 2011
9
thanks alot for your help , so far a lot of things are now clear to me. I have been reading and trying to learn how can the data sheet be useful , and how can i program the PIC with C code. choosing which microcontroller is not that easy , since i dont have any experience in this field. Can any of these microcontrollers have a battery (long life) to supply it?? do i need ADC if i am using a comparator? i read that the comparator is an analog part , so it shows which voltage is greater between the Positive and the negative inputs. depending on the polarity the output will be high or low.

i am a bit confused here. if i am using a comparator , why do i need the ADC ?? I mean, what i need is to know if my input voltage is higher than a certain value which is 12V, and then set a pin to high or low.

another question (plz remember that i am totally new to this) can one add any parts on a microcontroller?? or is the microcontroller added to a board where i can then add any part, for example a resistor or a voltage divider, in case i needed it, or a transistor (between the output pin and the relay)?

when programming the microcontroller , do you program just the thing you need , i mean , like reading the votlage at the IN pins and then doing the comparison and thats it , and then setting the out pin high or low ?? , what about all the other things in the data sheet and the features of the PIC (watchdog timer, delays, etc.) , is that already programmed?? do i need to define everything?

#### ErnieM

Joined Apr 24, 2011
8,020
Can any of these microcontrollers have a battery (long life) to supply it??
A Micro is just a digital circuit using MOS technology, meaning it needs more current as you run it faster. One great feature is you can put it to "sleep" so it isn't really running and consumes very tiny currents, micro to nano amps.

do i need ADC if i am using a comparator?
Either one will do. The ADC is less parts but harder to program. The comparator is simpler to program but takes more parts (a resistor dividor) and more current (the resistor current, which WILL be more then the micro itself takes).

another question (plz remember that i am totally new to this) can one add any parts on a microcontroller??
Of course you can. They come as any other IC and you add on the parts your specific application needs.

For example, I have 4 products here, one is a relay timer, one is a DC to DC converter, one is a FET switch controller that also monitors the FET current, and one is a LED dimming control. What do they have in common? They ALL use the same PIC chip inside, connected to different hardware running different programs.

when programming the microcontroller , do you program just the thing you need , i mean , like reading the votlage at the IN pins and then doing the comparison and thats it , and then setting the out pin high or low ?? , what about all the other things in the data sheet and the features of the PIC (watchdog timer, delays, etc.) , is that already programmed?? do i need to define everything?
One big question. All good questions too as you can see some of the things you will need.

You will need some sort of programmer, I would recommend a PicKit 2 or 3. I have several of the 2's, they work great. 3 is newer and I have not tried it yet but I hear it is good too. The PicKit not only programs the device, but it can power small project, and most importantly it can do debugging on your part while it is in the circuit. That is very importaint for those times when you say "it does the wrong thing when I do this" you can actually stop the program at that point and watch the code run step by step on your PC.

There is a \$69 dollar kit that combines the Pickit3 with a sample board. I suggest you get one as it has some very good lessons with programs to try, a great bargain! My only hesitation is it is an advanced device to start with but they no longer sell the one I started with years back (and I trust Microchip enough to recommend it anyway).

The demo board has a switch, a pot, some LEDs, and the tutorials will walk you thru how to use them all (including that nasty ADC). It will show you haw to use "all the other things."

Work thru their demo programs and you will be well grounded in these devices!

Once you get that done you will be able to switch to the real part you should be using, and we can help you pick one out.

Can you solder for breadboards? You'll need to build some hardware somewhere along the way.

#### khara

Joined Jul 6, 2011
9

I just took a look at this Link. There is a lot of options. which one do you recommend?? i want to be able to program PIC uCs, with C and not assembly. i find it alot easier for me.

the problem with the soldering , is that i do have a time limit. my supervisor is not the most active person. so I've been waiting for 2 weeks now for an answer, when he can get me a uC , or the uC that i need to work on. I've done alot of research until now, and i think i have a good idea of what i have to do. The only thing, that is not that clear , is choosing which uC since i dont know how to know which is better for my application. Since it should be low power uC , i understand it should not have that complicated features, so it wouldn't consume that much power, since it is going to be powered by a battery. I read your comment (about the sleep mode), and thats good.

When I read a data sheet, what is the most important thing that I am looking for. i mean for this specific application? I think I dont need that much pins :S !!? since I only need a pin for the input voltage coming from the charger controller, and an output pin connected to the relay. I will try and draw a sketch and post it here , maybe it will make things clearer.

#### ErnieM

Joined Apr 24, 2011
8,020
Oh cool, you found the "Part Number: DV164120 - PICkit 2 Starter Kit"

I was looking for that one last week, thought they dropped it. That is a very good tool to start with. You get a board with a prototypiong area, a programmer/debugger, and some nice tutorials that work with that board to teach you how to work it.

You can probably reuse that board in your application. It's not the exact best PIC part for your application, but it is a good choice.