Skip to content

Latest commit

 

History

History
176 lines (113 loc) · 6.25 KB

File metadata and controls

176 lines (113 loc) · 6.25 KB

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 [] and join today!

5.6 Two Kinds of Transistors

This kit is equipped with two types of transistors, S8550 and S8050, the former is PNP and the latter is NPN. They look very similar, and we need to check carefully to see their labels. When a High level signal goes through an NPN transistor, it is energized. But a PNP one needs a Low level signal to manage it. Both types of transistor are frequently used for contactless switches, just like in this experiment.

Let's use LED and button to understand how to use transistor!

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 LINK
ESP32 Starter Kit 320+

You can also buy them separately from the links below.

COMPONENT INTRODUCTION PURCHASE LINK
cpn_esp32_wroom_32e
cpn_esp32_camera_extension -
cpn_breadboard
cpn_wires
cpn_resistor
cpn_led
cpn_button
cpn_transistor

Available Pins

  • Available Pins

    Here is a list of available pins on the ESP32 board for this project.

    For Input IO14, IO25, I35, I34, I39, I36, IO18, IO19, IO21, IO22, IO23
    For Output IO13, IO12, IO14, IO27, IO26, IO25, IO33, IO32, IO15, IO2, IO0, IO4, IO5, IO18, IO19, IO21, IO22, IO23
  • Conditional Usage Pins (Input)

    The following pins have built-in pull-up or pull-down resistors, so external resistors are not required when using them as input pins:

    Conditional Usage Pins Description
    IO13, IO15, IO2, IO4 Pulling up with a 47K resistor defaults the value to high.
    IO27, IO26, IO33 Pulling up with a 4.7K resistor defaults the value to high.
    IO32 Pulling down with a 1K resistor defaults the value to low.
  • Strapping Pins (Input)

    Strapping pins are a special set of pins that are used to determine specific boot modes during device startup (i.e., power-on reset).

    Strapping Pins IO5, IO0, IO2, IO12, IO15

    Generally, it is not recommended to use them as input pins. If you wish to use these pins, consider the potential impact on the booting process. For more details, please refer to the esp32_strapping section.

Way to connect NPN (S8050) transistor

image

In this circuit, when the button is pressed, IO14 is high.

By programming IO26 to output high, after a 1k current limiting resistor (to protect the transistor), the S8050 (NPN transistor) is allowed to conduct, thus allowing the LED to light up.

image

Way to connect PNP(S8550) transistor

image

In this circuit, IO14 is low by the default and will change to high when the button is pressed.

By programming IO26 to output low, after a 1k current limiting resistor (to protect the transistor), the S8550 (PNP transistor) is allowed to conduct, thus allowing the LED to light up.

The only difference you will notice between this circuit and the previous one is that in the previous circuit the cathode of the LED is connected to the collector of the S8050 (NPN transistor), while this one is connected to the emitter of the S8550 (PNP transistor).

image

Code

Note

  • Open the 5.6_transistor.py file located in the esp32-starter-kit-main\micropython\codes path, or copy and paste the code into Thonny. Then, click "Run Current Script" or press F5 to execute it.
  • Make sure to select the "MicroPython (ESP32).COMxx" interpreter in the bottom right corner.
import machine 

button = machine.Pin(14, machine.Pin.IN)   # Button
led = machine.Pin(26, machine.Pin.OUT)  # LED

# Start an infinite loop
while True:  
    # Read the current value of the 'button' object (0 or 1) and store it in the 'button_status' variable
    button_status = button.value() 
    # If the button is pressed (value is 1)
    if button_status == 1: 
        led.value(1) # Turn the LED on
    # If the button is not pressed (value is 0)
    else:       
        led.value(0)            # turn the LED off

Two types of transistors can be controlled using the same code. When we press the button, the ESP32 will send a high-level signal to the transistor; when we release it, it will send a low-level signal.

  • The circuit using the S8050 (NPN transistor) will light up when the button is pressed, indicating that it is in a high-level conduction state;
  • The circuit using the S8550 (PNP transistor) will light up when the button is released, indicating that it is in a low-level conduction state.