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.

img_NPN&PNP

  • 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.

What Youā€™ll Need

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

LINK

Newton Lab Kit

450+

Newton Lab Kit

You can also buy them separately from the links below.

SN

COMPONENT

QUANTITY

LINK

1

Raspberry Pi Pico 2

1

BUY

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

Resistor

3(220Ī©, 1KĪ©, 10KĪ©)

BUY

6

LED

1

BUY

7

Button

1

BUY

8

Transistor

1(S8050/S8550)

BUY

Wiring the NPN (S8050) Transistor

sch_s8050

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_s8050

Wiring the PNP (S8550) Transistor

sch_s8550

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.

wiring_s8550

Writing the Code

Note

  • You can open the file 2.15_transistor.ino from newton-lab-kit/arduino/2.15_transistor.

  • Or copy this code into Arduino IDE.

  • Select the Raspberry Pi Pico 2 board and the correct port, then click ā€œUploadā€.

// 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

  1. Reading the Button State:

    Reads the current state of the button.

    buttonState = digitalRead(buttonPin);
    
  2. 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.