Continue to Site

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.

  • 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.

Transistor and a PIC

Status
Not open for further replies.

CoTang

Member
Hiya!

I had great help on here before and I'm needing it again.

I was playing around with a 16f84a interfacing the portb.0 bit to a 2n3904 npn transistor that activates a 12vdc 270ohm relay(with a diode of course). Now when I omitted the Rb (resistor from pic pin to base of transistor) it worked in a strange way. Within the code I set the port bit high and then a delay. The relay turns on. But once the delay finishes the relay turns off even though my code doesn't set it low.... Without the delay the relay doesn't even turn on.

Now if I put a Rb in place (calculated so that Ic have enough current), everything works fine. No issues.

So my question is, why does the relay turn on then off without Rb and why does it somewhat work when there is a delay?

Thanks.
 
This sounds like a RMW (Read/Modify/Write) problem. When you set or clear a port bit the processor reads the byte, modifies it and then writes it back. The read always reads the port pins - not the port latch. This normally isn't a problem unless a large load is on a pin (such as a transistor without a base resistor to limit the current).

For example, if your load is on PortA.0 and is turned on by bsf PORTA,0 and it draws a large enough current that the output only goes to 0.7V, then when it is read it will read as zero. Therefore bsf PORTA,3 will turn off your load. This is because the processor reads the port (=0 and not 1) sets bit 3 (=8) and writes it back. Writing 9 to the port will set bit 3 and leave bit 0 set.

Try measuring the voltage on your output pin when it is on.

HTH

Mike.
 
Simple solution, just keep Rb, it's supposed to be there, removing it is a silly idea.
 
Pommie said:
This sounds like a RMW (Read/Modify/Write) problem. When you set or clear a port bit the processor reads the byte, modifies it and then writes it back. The read always reads the port pins - not the port latch. This normally isn't a problem unless a large load is on a pin (such as a transistor without a base resistor to limit the current).

For example, if your load is on PortA.0 and is turned on by bsf PORTA,0 and it draws a large enough current that the output only goes to 0.7V, then when it is read it will read as zero. Therefore bsf PORTA,3 will turn off your load. This is because the processor reads the port (=0 and not 1) sets bit 3 (=8) and writes it back. Writing 9 to the port will set bit 3 and leave bit 0 set.

Try measuring the voltage on your output pin when it is on.

HTH

Mike.

That explains it, but still why does it work with a delay in it? Say I go

high Portb.0 (Which is activating the transistor)
delayms 1000 (add a delay of 1 sec)

the sucker actually turns on then off.

And yes the voltage I measured was 0V on the pin.
 
Last edited:
Hero999 said:
Simple solution, just keep Rb, it's supposed to be there, removing it is a silly idea.

I forgot to put it in or might have missed it, you know how the bread board can get cluttered up like that. But you are right it is suppose to be in there, but I would like to know why it doesn't work. I see it as a learning experience.
 
You're shorting the output of the PIC pin down to chassis via a diode junction, it's not going to help anything - doesn't matter what happens or why, just don't do it!.
 
Nigel Goodwin said:
You're shorting the output of the PIC pin down to chassis via a diode junction, it's not going to help anything - doesn't matter what happens or why, just don't do it!.

It does matter to me. I pretty much understand that its shorting out due to the previous posting, but it still doesn't make sense that if it is shorting out casuing the pin to read 0V and the Read-Modify-Write ends up writing zero, why in the world that if I put in delay it actually works. (Well in terms of the relay coming on then off).

It's the curiosity that make me a decent tech, but great tech is one who finds the answer to the question that he doesn't know.
 
It has to be some kind of reaction the high function is causing. Have you checked the output on a scope, does the pin go high for 1 second and then go low? What compiler are you using? Perhaps it checks for short circuits on I/O pins and turns then off for you but it was delayed because of the delayms code?
 
CoTang said:
It's the curiosity that make me a decent tech, but great tech is one who finds the answer to the question that he doesn't know.

You need the full internal circuit of the PIC which isn't something you're going to get - otherwise you're only guessing!.

I would suggest a 'great tech' wouldn't waste his time trying to answer pointless questions that won't do him any good even if he found out! :p
 
Nigel Goodwin said:
You need the full internal circuit of the PIC which isn't something you're going to get - otherwise you're only guessing!.

I would suggest a 'great tech' wouldn't waste his time trying to answer pointless questions that won't do him any good even if he found out! :p

I hate you :)

Beside, no answer is ever pointless, its just how people see it to be! :p

Oh well, at least I got 90% of the question answered. I guess it'll have to do.
 
Without seeing your code and your circuit it is impossible to say.

Hardware wise, the only explanation I can think off is that you are drawing too much current from your supply and the voltage drops low enough that the brown out circuit resets the pic. This may take a while if you have a large smoothing capacitor. This would account for the delay. Try monitoring your supply voltage.

Code wise, removing the delay may move page sensitive code into an area where it works correctly or malfunctions. This would most likely cause a reset. Try changing CALLs to NOPs so the code doesn't move around.

HTH

Mike.
 
CoTang said:
Oh well, at least I got 90% of the question answered. I guess it'll have to do.
I thought that Pommie explained it completely.

My understanding of what he said is:-

bsf porta, 0; sets RA0
You then insert a delay and then
bsf porta, 3; sets RA3 high.

But because of the read, modify, write operation, the PIC reads the pins and finds RA0 low, so it sets porta to

0000 1000

Hence RA0 is set low.

If there was no delay, then the relay would not have time to operate.
 
Last edited:
ljcox said:
If there was no delay, then the relay would not have time to operate.

Len,

Having reread the original question, I think your explanation is the most likely cause.

I should try and remember the old saying. When you see hoof prints, think horses, not zebras. IE, the simplest explanation is normally the correct one.

Mike.
 
Mike,
I thought that my explanation was simply a summary of your's.
 
Last edited:
I meant about why inserting a delay made a difference.

BTW, if the transistor is replaced with a 2N7000 then all problems would be solved. No base resistor required and it even has the protection diode built in.

Mike.
 
Pommie said:
I meant about why inserting a delay made a difference..
Oh, I see.
Pommie said:
BTW, if the transistor is replaced with a 2N7000 then all problems would be solved. No base resistor required and it even has the protection diode built in.

Mike.
I normally put a 100 Ohm resistor in series with the gate as I read somewhere that MOSFETs can sometimes oscillate if this is not present.

The relay still needs an external diode. There was a thread on this recently. I don't recall who wrote it, but they pointed out that the internal diode is not sufficient. Besides, its pointing in the wrong direction to suppress the back EMF. So it would have to be a zener to give protection.
 
ljcox said:
The relay still needs an external diode. There was a thread on this recently. I don't recall who wrote it, but they pointed out that the internal diode is not sufficient. Besides, its pointing in the wrong direction to suppress the back EMF. So it would have to be a zener to give protection.

There are 2 ways to handle back EMF, short it out with a reverse diode across the relay or conduct it back to the power supply with a reverse diode across the switching device. The 2N7000 uses the second way. As for the internal diode being sufficient, that depends on the load but it can take 800mA pulsed.

Mike.
 
But Mike, as I wrote, the diode across the MOSFET is in the wrong direction.

There is +12V behind the relay and a 2N7000 connected between it and gnd.

So the internal diode would have its cathode connected to the drain and anode to the source and thus to gnd.

The current through the relay (conventional flow) is going into the drain. When the FET is turned off, the inductance wants to keep the same current flowing. Therefore the anode of the diode would have to be connectd to the drain with the cathode to gnd.

If this was the case, the relay would be permanently operated.
 
Len,

You are of course correct. I was confusing a relay and a motor. If the load had been a motor then the diode would do it's job correctly. It's a long time since I did my DC Machines class.

In a motor, the back EMF is so called because it opposes the applied EMF due to the motor acting as a generator. This is not the case with an inductor. So, is back EMF still the correct term?

Mike.:confused:
 
Yes, its also called back EMF.

I would be dubious about switching a motor without protection from the inductive component also.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top