1.15 NPN Transistor Switch
Now let’s explore transistors - the fundamental building blocks of all digital electronics! A transistor acts like an electronic switch that can be controlled by a small signal to switch much larger currents.
We’re using an S8050 NPN transistor. Think of it as a water valve: a small control signal at the “base” can turn on/off a much larger current flow between “collector” and “emitter”. This lets our tiny Pico control bigger devices like motors, lights, or fans!
Component List
Raspberry Pi Pico W x1
MicroUSB cable x1
830 Tie-Points Breadboard x1
Jumper Wire Several
Resistor 220Ω, 1KΩ, 10KΩ x1
LED x1
Transistor S8050 x1
Component knowledge
Transistor
How our transistor switch works:
Control Logic: - Button pressed → GP14 reads HIGH → GP15 outputs HIGH → Transistor turns ON → LED lights up - Button released → GP14 reads LOW → GP15 outputs LOW → Transistor turns OFF → LED goes dark
Why use a transistor? Instead of connecting the LED directly to the Pico, the transistor acts as a “relay” that can handle higher currents safely, protecting our microcontroller.
Connect
Code
Note
Open the
1.15_npn_transistor_switch.pyfile under the path ofUltimate-Starter-Kit-for-Pico-W\Python\1.Projector copy this code into Thonny, then click “Run Current Script” or simply press F5 to run it.Don’t forget to click on the “MicroPython (Raspberry Pi Pico)” interpreter in the bottom right corner.
After running the code, press the button to toggle the LED on and off. The serial monitor shows detailed transistor switching status, including press counts and whether the “high current device” (LED) is active or inactive. Perfect for learning transistor switching principles!
The following is the program code:
"""
Transistor Switch Control
Uses a button to control a transistor switch that can
drive higher current devices like motors or lights.
"""
import machine
import utime
# Pin definitions
BUTTON_PIN = 14 # button input pin
TRANSISTOR_PIN = 15 # transistor control pin
CHECK_DELAY = 50 # button check interval in milliseconds
# Variables for button state tracking
button_pressed = False
last_button_state = False
device_on = False
press_count = 0
# Initialize pins
button = machine.Pin(BUTTON_PIN, machine.Pin.IN)
transistor = machine.Pin(TRANSISTOR_PIN, machine.Pin.OUT)
def setup():
"""Initialize the system"""
global device_on
print("=== Transistor Switch Control ===")
print("Press button to toggle device ON/OFF")
print("Transistor acts as electronic switch")
print()
# Ensure device starts OFF
transistor.value(0)
device_on = False
print("Device: OFF (Ready)")
print()
def handle_button_control():
"""Handle button press and transistor control"""
global button_pressed, last_button_state, device_on, press_count
# Read current button state
button_pressed = bool(button.value())
# Detect button press (transition from LOW to HIGH)
if button_pressed and not last_button_state:
# Toggle device state
device_on = not device_on
press_count += 1
# Control transistor switch
transistor.value(1 if device_on else 0)
# Display status
print(f"Button pressed (#{press_count}) - Device: {'ON' if device_on else 'OFF'}")
if device_on:
print("Transistor conducting - High current device active")
else:
print("Transistor off - High current device inactive")
print()
# Update last button state for next comparison
last_button_state = button_pressed
def main():
"""Main function"""
setup()
try:
while True:
# Check button and control transistor
handle_button_control()
# Small delay for stable operation
utime.sleep_ms(CHECK_DELAY)
except KeyboardInterrupt:
print("\nTransistor control stopped.")
print(f"Total button presses: {press_count}")
# Turn off device safely
transistor.value(0)
print("Device turned OFF - System safe.")
if __name__ == "__main__":
main()