Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
2.15 Two Types of Transistors: NPN and PNP
In this lesson, we’ll explore two types of transistors: the S8050 (NPN) and the S8550 (PNP). Transistors are commonly used as electronic switches, and we’ll see how both types can be used to control an LED with a button.
NPN (S8050): This type of transistor allows current to flow from the collector to the emitter when a high signal is applied to the base.
PNP (S8550): For PNP transistors, current flows from the emitter to the collector when a low signal is applied to the base.
While both transistors serve similar purposes, they behave oppositely when it comes to signal control. Let’s use these transistors to control an LED based on button input.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
PURCHASE LINK |
---|---|---|
Pico 2 W Starter Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT INTRODUCTION |
QUANTITY |
PURCHASE LINK |
---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
3(220Ω, 1KΩ, 10KΩ) |
||
6 |
1 |
||
7 |
1 |
||
8 |
1(S8050/S8550) |
Way to connect NPN (S8050) transistor
In this circuit, pressing the button sends a high signal to the GP14 pin. When GP15 outputs a high signal, the NPN transistor conducts, allowing current to flow through the LED, lighting it up.
Wiring the PNP (S8550) Transistor
For the PNP transistor circuit, the button starts with a low signal on GP14 and changes to high when pressed. When GP15 outputs a low signal, the PNP transistor conducts, allowing current to flow and lighting up the LED.
Writing the Code
Note
You can open the file
2.15_transistor.ino
under the path ofpico-2w-kit-main/arduino/2.15_transistor
.Or copy this code into Arduino IDE.
Don’t forget to select the board(Raspberry Pi Pico) and the correct port before clicking the Upload button.
// Define the pins
const int buttonPin = 14; // Button connected to GP14
const int transistorPin = 15; // Transistor base connected to GP15
int buttonState = 0; // Variable to hold the button state
void setup() {
pinMode(buttonPin, INPUT);
pinMode(transistorPin, OUTPUT);
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// control the transistor
digitalWrite(transistorPin, buttonState);
delay(10); // Small delay for debouncing
}
Results
For NPN Transistor (S8050):
When you press the button, the LED should turn on. When you release the button, the LED should turn off.
For PNP Transistor (S8550):
When you press the button, the LED should turn off. When you release the button, the LED should turn on.
Understanding the Code
Reading the Button State:
Reads the current state of the button.
buttonState = digitalRead(buttonPin);
Controlling the Transistor:
For NPN Transistor: When the button is pressed (
buttonState
is HIGH), the transistor is turned on, allowing current to flow and lighting up the LED.For PNP Transistor: When the button is pressed (
buttonState
is HIGH), the transistor is turned off (LOW), and when the button is not pressed, the transistor is turned on.
digitalWrite(transistorPin, buttonState);
Further Exploration
Control Larger Loads:
Use transistors to control devices that require more current than the Pico can provide directly, such as motors or relays.
Transistor as an Amplifier:
Explore how transistors can be used to amplify signals.
Experiment with Darlington Pair:
Use two transistors to create a Darlington pair for higher current gain.
Conclusion
In this lesson, you’ve learned how to use both NPN and PNP transistors to control an LED using a Raspberry Pi Pico and a button. Understanding the differences between NPN and PNP transistors is crucial for designing circuits that require switching or amplification.