0% found this document useful (0 votes)
29 views24 pages

Week 4 - Transistors, Program Control

Uploaded by

pranti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views24 pages

Week 4 - Transistors, Program Control

Uploaded by

pranti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

HSI255

WEEK 4
POWERING OUTPUTS
HIGH POWER OUTPUTS

• Most logic IO is 3.3V or 5V.


• LabJack DAC is 0-5V.
• LabJack VS is 5V.
• LabJACK DO is high or low – 0V or 3.3V
• These voltages are typical for DAQs and microcontrollers.

• Low voltages are ok to drive small devices like LEDs, but what if we wanted to
turn on a motor? Or tea kettle? Or a very bright lamp?
TRANSISTORS AND RELAYS

• Both transistors and relays act like switches.


• Transistors can also be used as amplifiers.
• Both use an input signal to “turn on” a load circuit (with a separate voltage
source – could be AC or DC).
• We will discuss relays more next week.
TRANSISTORS
TRANSISTORS

• BJT – Bipolar Junction Transistors


• Two types: NPN and PNP
• While NPN is more commonly used, we will
be using both in this course for practice!

• Three terminal device:


• Base
• Collector
• Emitter
https://www.electricalclassroom.com/bjt-bipolar-junction-transistors/
• The arrow indicates the emitter.
HOW DOES IT WORK?

• A voltage (with a small current) is applied to the


base to “saturate” the NPN BJT and turn it on.
• When the BJT is saturated, there is a short circuit
between the collector and the emitter.
• When the BJT is “cut-off” (no current/voltage at
the base for NPN), there is no current flow
between the collector and emitter.
HOW DOES IT WORK?

https://www.build-electronic-circuits.com/how-transistors-work/
NPN VS PNP
NPN PNP

https://www.build-electronic-circuits.com/how-transistors-work/
NPN VS PNP

• NPN requires a POSITIVE voltage at the base to turn it on (negative-positive-negative).


• PNP requires a NEGATIVE voltage at the base to turn it on (positive-negative-positive).
• In reality, it’s not a negative voltage so much as it’s a 0V signal, but the above helps to remember.

Note the difference in circuit


NPN PNP building:
On an NPN BJT, the voltage
source is connected to the
collector. On a PNP BJT, the
IO pin IO pin voltage source is connected
to the emitter.
WHAT IT LOOKS LIKE

• NPN part numbers: 2N3904 or PN2222


• PNP part numbers: 2N3906 or 2N2907
• Both NPN and PNP transistors have the
same pin out, but the circuits are built
differently.
MORE LABJACK PROGRAMMING
PROGRAM CONTROL

• Program control refers to how a program makes decisions.


• Decision making (if/else)
• Selection (switch)
• Iteration (for, while, do while)
• Jump (break, continue, goto)
WHILE LOOPS

• Overwhelmingly, while/do while loops are used with DAQs/microcontrollers as


inputs are constantly scanned and outputs will vary based on information from
inputs.
• Consider your while/do while loops carefully – once the condition is met, the
program exits the while loop (and for many programs, ends the program).
EXIT STRATEGIES

• When developing your code, consider what the exit strategy is (most
programs will require one).
• In lab 3, we use a “kill switch”. Once the button was pressed, the program
ended. Therefore, we made the while condition: while(button not pressed)

• We can also have programs that run for a certain amount of time.
SLEEP AND COUNTER

• The Sleep() function will pause the program for however many
milliseconds are passed to it.
• For example, Sleep(500); will pause the program for half a second.
• Must #include Windows.h
• Declare and initialize a counter of type int. Upon each iteration of the while
loop, add one to the counter.
• When counter reaches the predetermined value, exit the while loop.
SLEEP AND COUNTER

• For example, we want the program to run for 20 seconds.


SLEEP AND COUNTER
SLEEP AND COUNTER

• For example, we want the program to run for 3 minutes.


SLEEP AND COUNTER
IF/ELSE

• Often used inside of while loops to check an input and set an output.
• We saw this in lab 3 – while the kill switch was not pressed, we checked the
charge of the capacitor and if it was over 3V, we turn on the LED. Else, we
turn off the LED.
• You don’t always need an else. Depends on the program and the algorithm
you develop.
A NOTE ABOUT YOUR PROGRAMS

• There is never only one way to write a program.


• You are welcome to experiment and try a different strategy to fulfill the
requirements of the labs.
• There may be some limits (e.g., you must use a LabJack U3 and associated
header and lib files, circuits must work as indicated, etc.).
• If you find a better way, I will ask lots of questions and be super duper
impressed.
LET’S TRY IT!

• No inputs.
• Two output circuits:
• NPN transistor with an LED connected to FIO4 (as DO).
• PNP transistor with an LED connected to FIO5 (as DO).
• Program description:
• Program will run for 20 seconds. When program first starts, one LED is on. The LEDs will
swap states every 4 seconds. At the end of the 20 seconds, both LEDs will turn off. Use
Sleep(100) in the program.
IN-CLASS ASSIGNMENT
You have a program that runs for 15 minutes. Your program outputs (prints) the
current temperature every 10 seconds. Choose a Sleep() parameter and a while
loop counter condition that satisfies these requirements. Show your work.

You might also like