|
|
| Author |
Message |
hakha4
Joined: 02 Jun 2009
Posts: 30
Location: Moholm Sweden
|
|
Posted: 07 April 2010, 17:52 PM Post subject: AN203 issue driving ULN2803 |
|
|
Hi
I've used a template from code described in AN203 for 74Hc595 driving 8 LED's and everything works as expected. I want to drive relay's using ULN2803 connected to the 74Hc595 but when when I in code set one bit,ALL output's are set in ULN2803. Tried to find out what's wrong by reading datasheet's but I'm stucked Is this a hardware or software problem? Any help would be appreciated
Regards Hucke
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 07 April 2010, 18:11 PM Post subject: Re: AN203 issue driving ULN2803 |
|
|
| hakha4 wrote: | | Is this a hardware or software problem? | If you would post the circuit and the code, someone may be able to suggest a solution. Please post the smallest possible program that demonstrates the problem and an excerpt from the circuit diagram that shows all connections to the devices that are involved in the example code.
|
|
| Back to top |
|
 |
hakha4
Joined: 02 Jun 2009
Posts: 30
Location: Moholm Sweden
|
|
Posted: 07 April 2010, 19:55 PM Post subject: |
|
|
Hi again
Attaching simple scematic and code to clerify what I'm doing. Maybe someone have an idea what's missing. As I said everything works ok if I omit ULN2803.
Regards Håkan
| Code: |
'#################################################################
'############## Output to relays / LED's ##### ###################
'#################################################################
Option TargetCPU ZX1281e
Private Const dataPin as Byte = 31
Private Const clockPin as Byte = 30
Private Const strobePin as Byte = 29
Public Sub IO_HC595(tmpData As Byte)
' initialize the I/O pins
Call PutPin(dataPin, zxOutputLow)
Call PutPin(clockPin, zxOutputLow)
Call PutPin(strobePin, zxOutputLow)
' output data to the shift register
Call OutputData(tmpData)
End Sub
Sub OutputData(ByVal dataVal as Byte)
Dim InvDataVal As Byte
dataVal = dataVal XOR &hFF
' send 8 bits of data to the shift register
Call ShiftOut(dataPin, clockPin, 8, dataVal)
' strobe the output latch to transfer the data to the register’s outputs
Call PulseOut(strobePin, 1, 1)
End Sub |
| Description: |
|
| Filesize: |
10.26 KB |
| Viewed: |
6630 Time(s) |

|
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 07 April 2010, 20:27 PM Post subject: |
|
|
I edited the post to make it more readable. Firstly, code tags begin with code and end with /code, both enclosed in square brackets. This in a widely used strategy although the enclosing delimiters are different in some scenarios. For example, HTML and XML use angle brackets instead of square brackets. If you click the Edit button for your post you can see what it looks like in plain text.
Secondly, I added indenting to make the code more readable. If you had indenting originally, it was removed when the post was first made without the proper code tags.
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 07 April 2010, 20:48 PM Post subject: |
|
|
By the way, the code that you posted is not a complete working example in that it is missing the Main() and the code that calls the two routines. Since we can't see what else is going on, it is difficult to speculate what might be causing a problem. Although it is a little more work to produce a complete simplified (minimized) example, going through the process will sometimes yield an epiphany in that you'll suddenly realize what you are doing to produce the results you're observing.
Note that the initialization of the pins only needs to be done once. I would suggest the following change: | Code: | Public Sub Init_HC595()
' initialize the I/O pins
Call PutPin(dataPin, zxOutputLow)
Call PutPin(clockPin, zxOutputLow)
Call PutPin(strobePin, zxOutputLow)
End Sub
Sub Public Sub IO_HC595(dataVal As Byte)
dataVal = dataVal XOR &hFF
' send 8 bits of data to the shift register
Call ShiftOut(dataPin, clockPin, 8, dataVal)
' strobe the output latch to transfer the data to the register’s outputs
Call PulseOut(strobePin, 1, 1)
End Sub |
It is not clear why you invert the data value before it's written to the '595. Without the inversion, in each bit position a '1' will cause the corresponding output of the ULN2803 to be low which is almost certainly what you'd want to energize a relay or turn on an LED connected in common anode configuration.
What value to you pass to the output routine when you see all outputs of the ULN2803 go low?
|
|
| Back to top |
|
 |
hakha4
Joined: 02 Jun 2009
Posts: 30
Location: Moholm Sweden
|
|
Posted: 08 April 2010, 16:43 PM Post subject: |
|
|
Hi Don!
Sorry for producing such sloppy code. I've now put everything down to minimum and when I change bit 0-7 the LED lights up one by one as supposed when omitting the ULN 2803. When I put the ULN2803 between 74hc595 and LED's the output is wrong. When setting bit 0 LED 1-3 lights up bright and the rest light's up dimmed,setting bit 7 turn LED 1 and 8 on bright and the rest dimmed and so on ! I've read schematic's how to connect ULN 2803 and I think it's correct. The Question is if you can't connect the ULN 2803 to be driven directly from 74hc595 or if there is a error in programming
Regards Hucke
| Code: | Public RelayStatus As Byte
Sub main()
Dim x As Byte
Do
For x = 1 To 8
Dim togglestatus As Byte
togglestatus = zxoutputhigh
Call TestSetRelay(x, togglestatus)
Call delay(1#)
togglestatus = zxoutputlow
Call TestSetRelay(x, togglestatus)
Call delay(1#)
Next
Loop
End Sub
Sub TestSetRelay(x As Byte, pinstatus As Byte)
Dim tmpMsg As String
Call PutBit(RelayStatus, x - 1, pinstatus)
tmpMsg = "Relay : " & x & " set to : " & pinstatus
Debug.Print tmpMsg
Call IO_HC595(RelayStatus)
End Sub
'#################################################################
'############## Output to relays / LED's ###################
'#################################################################
Private Const dataPin As Byte = 31
Private Const clockPin As Byte = 30
Private Const strobePin As Byte = 29
Public Sub IO_HC595(tmpData As Byte)
' initialize the I/O pins
Call PutPin(dataPin, zxOutputLow)
Call PutPin(clockPin, zxOutputLow)
Call PutPin(strobePin, zxOutputLow)
' output data to the shift register
Call OutputData(tmpData)
End Sub
Sub OutputData(ByVal dataVal As Byte)
Dim InvDataVal As Byte
dataVal = dataVal Xor &HFF
' send 8 bits of data to the shift register
Call ShiftOut(dataPin, clockPin, 8, dataVal)
' strobe the output latch to transfer the data to the register’s outputs
Call PulseOut(strobePin, 1, 1)
End Sub |
|
|
| Back to top |
|
 |
hakha4
Joined: 02 Jun 2009
Posts: 30
Location: Moholm Sweden
|
|
Posted: 08 April 2010, 16:51 PM Post subject: |
|
|
Oops!
I've connected a cable wrong! But a all LED's turn on on setting a bit with the ULN2803 (i've tested with a brand new chip)
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 08 April 2010, 17:26 PM Post subject: |
|
|
| hakha4 wrote: | | When I put the ULN2803 between 74hc595 and LED's the output is wrong. | Are you aware that the ULN2803 introduces an inversion? This means that if an LED is on when connected to an output of the '595 it will be off when you add the ULN 2803 between the '595 and the LED.
|
|
| Back to top |
|
 |
hakha4
Joined: 02 Jun 2009
Posts: 30
Location: Moholm Sweden
|
|
Posted: 08 April 2010, 18:07 PM Post subject: |
|
|
Changing in code not to inverse 'datava'l now light's up the right LED bright but all the others also lights up but dimmed !? Any idea why
Regards Hucke
| Code: |
Sub OutputData(ByVal dataVal As Byte)
'dataVal = dataVal Xor &HFF
' send 8 bits of data to the shift register
Call ShiftOut(dataPin, clockPin, 8, dataVal)
' strobe the output latch to transfer the data to the register’s outputs
Call PulseOut(strobePin, 1, 1)
End Sub
|
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 08 April 2010, 19:58 PM Post subject: |
|
|
| hakha4 wrote: | [A]ll the others also lights up but dimmed !? Any idea why
| The ULN2803 is a high gain device meaning that very little input current is required to cause a moderate collector current to flow (thus dimly illuminating the LEDs). I would suggest re-checking the wiring, making sure that you have a good, common ground between the '595 and the ULN2803. Then, measure the voltage on the '595 output pins. The pins with a logic 0 level should be well below 0.5 volts; closer to 0.1V would be better. Also, measure the current flow in the LED that is brightly illuminated and one that dimly lit.
Are you using a separate supply for the LEDs?
|
|
| Back to top |
|
 |
G Herzog
Joined: 26 Mar 2006
Posts: 47
Location: Kaohsiung
|
|
Posted: 20 April 2010, 17:31 PM Post subject: |
|
|
Have you considered that a ULN2803 is a Darlington pair and it causes two diode drops in voltage (about 1.4volts)? You may have too much resistance in line for the LEDs to ever achieve full on. The LEDs are really being driven at something less than +5, maybe +3.6 volts.
As mentioned above, the ULN2308 also inverts the logic.
Also, the inputs to ULN2308 might require a pullup resistor of 5K or so to fully shut off the outputs. I am uncertain that relying on the BZ-24 (or 74HC595 in your case) to provide steady +5 is enough.
I seem to be having similar difficulties on a ULN2803 project (mine is directly driven from a ZX-24) and am consider using a 8x 5K ohm or higher strip soldered to the ULN2803 input pins to have all solidly pulled to +5. Fortunately, this is an easy fix.
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 20 April 2010, 17:59 PM Post subject: |
|
|
| G Herzog wrote: | | [A] ULN2803 is a Darlington pair and it causes two diode drops in voltage (about 1.4)? | That is true but it won't have any effect on the collector-to-emitter voltage when the output transistor is turned on.
| G Herzog wrote: | | Also, the inputs to ULN2308 might require a pullup resistor of 5K or so to fully shut off the outputs. | Remember, a high voltage on the input of the ULN2803 turns on the transistor, activating its load. This should be kept in mind because when the processor is in reset, the ULN2803 outputs will be driving their loads. In most cases, this is probably not what is desired. To get the ULN2303 to be idle when the processor is in reset you either need to add pull-down resistors or add pull-ups and an inverter between the processor and the ULN2803.
|
|
| Back to top |
|
 |
twesthoff
Joined: 17 Mar 2006
Posts: 191
Location: Fredericksburg, VA
|
|
Posted: 20 April 2010, 21:51 PM Post subject: |
|
|
[quote="dkinzer"] | G Herzog wrote: | | [A] ULN2803 is a Darlington pair and it causes two diode drops in voltage (about 1.4)? | That is true but it won't have any effect on the collector-to-emitter voltage when the output transistor is turned on.
The Vce(sat) of a darlington will never be less than about 0.9V (unlike a non-darlington which could be as low as 0.2V), so you do not have much current flowing through the LED. The voltage drop across the LED depends on the color. What color are you using?
If you are using Red the voltage is about 1.2V. Plus the Vce(sat) of 0.9V = 2.1V. 5V - 2.1V = 2.9V across the 390 ohm resistor. This gives you about 7.4ma through the LED which may not be very bright.
If you are using a different color LED you will have even less current and it will be even dimmer.
|
|
| Back to top |
|
 |
G Herzog
Joined: 26 Mar 2006
Posts: 47
Location: Kaohsiung
|
|
Posted: 21 April 2010, 1:42 AM Post subject: |
|
|
Thanks for detailed clarification about voltage drops and starting with low inputs. Everyone recommends the ULN2803, but very few sites offer the pragmatic details. The result is that after building something, one has to run down a lot of problems.
I am also concerned with what to do with unused inputs. I want to drive only 4 relays. Can I let these float, or must they be tied to either ground or +5?
It still looks to me that the LEDs have too much resistance when connected to a ULN2803 outputs that are driven by merely 5 volts to light brightly.
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 21 April 2010, 2:06 AM Post subject: |
|
|
| G Herzog wrote: | | I am also concerned with what to do with unused inputs. I want to drive only 4 relays. | If you connect the unused inputs to ground, the transistors will be certain to be off.
|
|
| Back to top |
|
 |
|