Newbee please help me understand these

Hi,

im a newbee to electronics :roll_eyes: . im a SE so i understand the programming logics but the problem i have is that
for example is i want and npn transister to work as a switch how can i find which transister to buy(the code)? and how is this codes made(is there any logic behind the code)?
same with the ICs , i understand that ic is a circuit of functions covered by its datasheet but how can i find the correct ic i need for my task

this is a project i did few days ago, basically its 2 3 segment displays i brought from ebay along with a ultrasonic sensor thinking to build a speed cam but the ultrasonic sensor was dead already(i think i broke it) because it gives 0cm all the time(i may have connected the gnd and vcc wrong first time). but i managed to hook up the display after reading about how 7segs work.
but it took all my pins (7segments+3+3) so if i needed to connect the ultrasonic sensor how can i do it? is there anyway to reduce the number of pins i used?
as you can see here

its a pretty lengthy way of doing it

please help me understand these.

thanks in advance

For transistors, it helps to have an idea of what you need it to do.
Then you can do a search at digikey.com and find a part.
For example, say you had 6 digits worth of common cathode 7-segment displays.
You could use 7-arduino output pins to drive the anode/segments, and an NPN transistor per/digit to multplex the digits (drive the anode for digit 1, turn on the NPN to pull the cathode low, after 5mS turn the cathode off and drive the anodes for digit 2, drive its cathode, etc).
You could use 6 more arduino pins to control those transistors.
Search for NPN-transistor, Single BJT, brings up a big list. Start filtering: in-stock, NPN, through hole
Sort by price.
Now start browsing the list.
Scroll down a few pages to get into the Qty 1 priced parts (unless you're planning on stocking up with 2,000!)
You're looking for a part that can handle 20mA/segment * 7 segments = 140mA thru the collector, and a low voltage from collector-emitter without taking tons of base current to get the current flow needed.
So a part like KSC815YTAFSCT-ND might do. It can take 200mA, needs 15mA base current (Ib) to control 150mA collector current (Ic) and only has 0.4V from collector to emitter. You'll need 6 IO pins for the 6 digits.

On the other hand, with some experience you'll see that there are shift registers that can free up IO pins and sink the current, all in one package.
A part like TPIC6B595 is a shift register, it has 8 open-drain outputs (meaning it has MOSFET outputs that only sink current), and each output can sink 150mA continuously. So you can use one of those vs 6 transistors and base current-limit resistors.

Shift registers typically need 3 control pins - data line, input clock line, output register clock line.
You could also use this shift register for the anodes - what I have done is use pullup resistors to drive the anodes high for segments that are to be on, and use TPIC6B595 to pull the anodes low for the segments that are to be off.
The two shift registers can be daisy chained, so now 3 IO pins can control the 6 digits and leave plenty of pins free for other stuff.
avnet.com has the best prices I've seen on these, about 78 cents each, I usually buy a tube of 20 or 25 to have on hand for projects.

tpic6b595.pdf (427 KB)