Arduino Basics: NPN Transistor Sketch (P2N2222AG) using PWM

12 December 2011

NPN Transistor Sketch (P2N2222AG) using PWM

We are now going to build a simple circuit to test out the P2N2222AG transistor in the sparkfun inventor's kit. You can also buy this transistor from RS-online.

Here are the components that you will need.
  • 1 x Arduino UNO
  • 1 x Breadboard
  • 1 x P2N2222AG transistor
  • 3 x LEDs  (1 x Red LED, 2 x Yellow LEDs)
  • 3 x 330 ohm Resistors
  • Wires to connect it all together.
Here is the Fritzing sketch:

We will use Pulse width modulation (PWM) to fade the LEDs in and out. 
Load the "Fading" example into the Arduino. (File>Examples>Analog>Fading)

I used this sketch to help me understand how electricity flowed through the transistor by disconnecting a wire here and there. I am not sure if this is advisable, so do this at your own risk. If you are an electrical engineer, feel free to comment. Please let me know if this is "risky" business.
I have had no formal training in electronics, so don't blame me if your arduino, or transistor blows up !
But from what I understand, I cannot see any harm in disconnecting wires with this particular circuit.
Please note, that I would disconnect the power to the arduino before modifying the wires.


  1. Hey Scott, these are some great tutorials. I found your Neural Network series to be extremely informative and well written.

    Do you have any interest in further developing the neural network for Arduino? Drop me a line!

    1. I am knee deep in university study at the moment, and need to focus on that at the moment. Which is why I have slowed down my Arduino posts.
      When I manage to get a bit of a break, I will let you know.
      What did you have in mind ?

  2. Very helpful blog, Thanks for sharing us.

    what is arduino


Feel free to leave a comment about this tutorial below.
Any questions about your particular project should be asked on my Arduino Tutorials Discord Server.

Comments are moderated due to large amount of spam.