PDA

View Full Version : Can't keep a pin low



bartman
- 2nd January 2006, 06:33
I'm using a 12F629

I have set all my ports to either input or output and turned off the comparitor.

I have a PNP transistor on GPIO.4 and a LED for testing the state of the pin.

The program is set to start with GPIO.4 LOW which is seems to do - my LED is lit.

The problem crops up when I use a button to supply an input to any one of three other ports OR put another port HIGH to play a sound on a transducer.

None of the subroutines address GPIO.4 in any way yet it is being switched to HIGH and not staying in the low state I'm looking for.

Reading through the datasheet I didn't see anything on GPIO.4 that should suggest it should behave any way other than a general output with the configuration I have set up however I will admit to the datasheets being quite a bit over my head.

So what is causing the other ports to affect my pin from staying low?

Thanks.

Bart

Melanie
- 2nd January 2006, 08:24
Take your pick...

Bad code, or
Bad PIC, or
Bad Circuit.

Since all pins operate pretty much independently, you either have a short between it and an adjacent pin somewhere, or you have a dud PIC, or your code is flawed (which includes not initialising the PIC properly or resetting the PIC in some way into it's initial power-up state). Are you perhaps using some command which is not valid for the 12 series PICs?

bartman
- 2nd January 2006, 15:30
Hmmm, can there be a fourth choice?

I swapped PICs with no change
I've triple checked all the connections on the breadboard

That leaves a code problem.

Here is the first part of that:

@ DEVICE pic12F629, INTRC_OSC_NOCLKOUT ' Internal Oscillator
@ DEVICE pic12F629, WDT_ON ' Enable watch dog timer
@ DEVICE pic12F629, PWRT_ON ' Enable power up timer
@ DEVICE pic12F629, MCLR_ON ' Enable MCLR pin

Define OSCCAL_1K 1 ' Oscillator calibration

TRISIO = %11101011 ' turn all I/O to input except GPIO.2 and GPIO.4

CMCON = 7 ' Disable analog comparator

Which is working for all other input and output.

As far as I can tell there is no command that is not allowed. It is all basic stuff like driving a buzzer based on buttons pressed - really nothing fancy. A single input on any of the other lines that does nothing but make a beep seems to be flipping that pin - and that buzzer is on port 2.

I'll keep going through the code.

Thanks.

Bart

I just tried something simple and discovered that when my port 2 goes HIGH, port 4 is going HIGH at the same time, but it will not switch back when port 2 goes LOW again.

Does that suggest anything?

Luciano
- 2nd January 2006, 16:55
Hi,

Describe how do you drive the LED and the buzzer.
Post a small schematic with components type and values.

- Voltage on the VDD pin if the PIC.
- Voltage on the emitter of the transistor.
- Type of transistor.
- Type of buzzer.


Also post a complete small program showing the problem.

Best regards,

Luciano

bartman
- 2nd January 2006, 17:12
I can give you some of this, but since it's only something I "fiddle with" I don't have anyway right now to provide all info.

I'm running it on 3 volts
Don't have emitter voltage, but it can drive a sound board that runs on 3-4 volts
PNP common type 2N4403
Buzzer is your run of the mill transducer driven from PIN through cap

I don't have a "small" version of the program.

If the line says GPIO.4 = 0 the LED (or the sound board I have on it) works properly.
If I then use GPIO.2 = 1 or I use SOUND GPIO.2 [whatever value] then port 4 goes HIGH instantely.

Switching port 2 back to low does NOT reset port 4 to low so it just works one way.

The idea is that there are three buttons that play different combinations of the SOUND command depending on how they are pressed. With each press there is also a confirmation beep. At the same time there is going to be a sound module working on its own. The confirmation beeps should play and have no effect on the sound module.

The sound module power runs through the transistor which is acting just like a switch that can turn the sound module on or off depending on the value of port 4.

I removed SOUND command that would play the confirmation beep and tried it that way. Without that port 2 address happening the PIC would turn the sound module on or off with the button presses just as it should. That suggested to me that port 2 addressing was causing the problem so I then put a HIGH 2 command in place of the sound command and tried that. Sure enough, the second port 2 went high port 4 did as well.

I will work on a version of the program that is short that I can post here. I'll have to remove a lot to boil it down then I will post that.

Thanks.

Bart

Ingvar
- 2nd January 2006, 17:25
Measure the voltage on GPIO.4 when it's low. It MUST be a low voltage(below 1V), if not you will have problems just like this. My guess is that you have a short on the base resistor to the PNP or forgotten it completely.

bartman
- 2nd January 2006, 17:50
Success!

It was the resistor issue. I think this is the second time one of those damn things has screwed me up. I also had a 0 and a 1 reversed in part of my code which didn't show up until I got the resistor issue resolved.

So, now my question is this. I have a pre-programmed sound module which I know very little about and won't ever know anymore on. It runs on 3 to 4 volts and probably barely uses any amps at all. It is runs on 50 ma I'd be surprises. It drives a little 1.25" speaker.

I've been using just comman transistors and resistors on the breadboard in an effort to just turn the power on/off to the sound module. That problem now fixed thank God.

But, when I go to put this together for real I want to ensure I'm using suitable stuff that is over-engineered just enough to keep things working as future repair will be pretty much impossible.

So, is there a generic enough way to come up with a suitable transistor and resitor value to do this?

I had originally picked out a BCX5316CT-ND from Digikey as a transistor because it seemed to match the common through hole one I was using for testing. Resistor I was never quite sure on, but was thinking like 330 ohm.

Advice on this issue would be appreciated.

Thanks for enough questions and comments that I could troubleshoot this to a conclusion.

Bart

Melanie
- 2nd January 2006, 20:15
>> So, is there a generic enough way to come up with a suitable transistor and resitor value to do this?

How technical do you want to get?

Current in mA through the Collector divided by the gain of the Transistor (see Transistors Datasheet) should give you the current through the Base. Pick a suitable Resistor to fit (5v out from the PIC less 0.7v for the Transistors Emitter/Base junction divded by the previously calculated Base Current). Example 50mA Collector current with a Transistor gain of 150 should give you a Base current of around 333uA. 4.3v/333uA=12.9K Resistor... drop down to next lowest preferred value=12K.

Alternatively fit a 10K Resistor in the Base which should drive most decent small/medium Transistors on the planet.

bartman
- 2nd January 2006, 22:45
Probably 10K will do!

But, here are the specs from the transistor I picked.

I'm not sure which of these figures is gain, but I think it is HFE@1c/VCE

min: 100
max 250
mA -150
V -2

In case not, we have VCE(sat)
Max V -.5
@IC (mA) -500
@IB (mA) -50

Also, PIC will be running on 3.7V same as sound board.

Bart

bbarney
- 3rd January 2006, 01:33
check this one out Bart at Digi-key
SMBT2907AINCT-ND $1.99(can) for 10
and resistors 311-10.0KFCT-ND $1.11 for 10

bartman
- 3rd January 2006, 03:21
Okay, so looking at your suggestion it looks similar in specs to the one I listed. What is your reason for this one out of curiosity? I did notice your suggestion is a very small component and the one I listed was considerably bigger (I also picked that one so it would be a bit easier for me to work with).

But, I still want to know why one over the other when they are close.

I do have some SMD 10K resistors so I think I'm good there.

Thanks.

Bart

Luciano
- 3rd January 2006, 09:02
See this link.

Using Bipolar Transistors As Switches
http://www.rason.org/Projects/transwit/transwit.htm


Luciano

bbarney
- 3rd January 2006, 23:08
the one you picked out is a medium power transistor not a switching transistor,that's why there's a big differance in the price of the 2.As for the size sot-23 is the more common one to use and easy to solder.there's probably better and cheaper transistors that will work ok I was just looking for a swithing transistor close to the spec's you gave

bartman
- 6th January 2006, 04:00
I was reading the link that Luciano supplied regarding bipolar transistors. It mentions a second resistor tied to the positive side to ensure there is no negative voltage at the transistor. It also mentions this is not required, but it sounds like good practice.

Now, that link doesn't specifically talk about the transistor being driven by a PIC so I want to know if that second resistor is still good practice in that case or not?

I'm about finished designing my PCB and want to add anything I might need now and not try to fit it in later.

Thanks.

Bart

Luciano
- 6th January 2006, 10:08
Hi,

Upon power up, your PIC I/O is an input high-impedance (tri-state)
and then with your code you make it an output. Without R2 the
base of the transistor will be left "floating" when the I/O is
in high-impedance state.

* * *

Battery operated devices

Hypothetical scenario:
Only the PIC is switched off with a switch and the
emitter of the transistor is directly connected to the battery.

When the PIC is powered down what is the voltage on its I/O?
In this conditions, do you have still some current flow in R1?
If the transitor "switch" is not completely turned off, will the
sound module slowly discharge the battery?

* * *

How to measure the current in R1:

To do this measurement you will need a high-impedance voltmeter.
(Digital multimeters with LCD display are OK).

With the Digital multimeter set for "DC volt" measurements,
measure the voltage dropp across R1.

http://img8.picsplace.to/img8/5/shunt.jpg (http://picsplace.to/)


Example, your Digital Voltmeter measures 1V across R1:
(1 V is dropped across R1 and your R1 is 10000 ohm).

I = V/R


To calculate the current flow through R1:

1V / 10000 Ohm = 0.0001 A (0.1 mA)


Best regards,

Luciano

bartman
- 6th January 2006, 14:04
There is a master on/off switch on the battery to power the sound module and the PIC so when it is "off" it is really "off" to everything. I'm not worried about draining anything.

I just want to ensure when both circuits are active that when I want the pin low it stays low and when I want it high it stays high. I don't want to take the chance that it floats or changes state for no reason.

Since that diagram and article didn't mention connecting it to a PIC output I question that second resistor. I have a feeling it would keep everything too high all the time.

I also would like everything to start up low, but I think the PIC is fast enough that it reads my mute flag and shifts the pin before there is enough time to even notice a delay.

Bart