Week 4 - Transistors, Program Control
Week 4 - Transistors, Program Control
WEEK 4
POWERING OUTPUTS
HIGH POWER OUTPUTS
• 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
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
• 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
• 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
• 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.