Advertisement
microrobotics

Interface 2N2222A with Arduino

Apr 22nd, 2023
2,468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The 2N2222A is an NPN transistor that can be used for various purposes, such as switching a load (e.g., LED, motor, etc.) or amplifying a signal. In this example, I'll show you how to use the 2N2222A transistor to switch an LED on and off with an Arduino.
  3.  
  4. Components required:
  5.  
  6. Arduino board
  7. 2N2222A TO-92 NPN transistor
  8. LED
  9. 220-ohm resistor (for the LED)
  10. 1k-ohm resistor (for the transistor base)
  11. Breadboard and jumper wires
  12. Circuit connections:
  13.  
  14. Connect the transistor's emitter (E) pin to the Arduino GND pin.
  15. Connect the transistor's base (B) pin to the Arduino digital pin 9 through a 1k-ohm resistor.
  16. Connect the transistor's collector (C) pin to the cathode (short leg) of the LED.
  17. Connect the anode (long leg) of the LED to the Arduino 5V pin through a 220-ohm resistor.
  18. Now, upload the following code to your Arduino:
  19.  
  20. This code sets the transistorBasePin as an output and toggles it on and off every second in the loop() function. When the transistorBasePin is set to HIGH, current flows from the base to the emitter of the transistor, allowing current to flow from the collector to the emitter and turning on the LED. When the transistorBasePin is set to LOW, the transistor is turned off, and no current flows through the LED.
  21.  
  22. After uploading the code to your Arduino, the LED should turn on and off every second. You can modify the code to control other devices, such as motors or relays, or use sensor inputs to adjust the behavior according to your project requirements.
  23. */
  24.  
  25. const int transistorBasePin = 9;
  26.  
  27. void setup() {
  28.   pinMode(transistorBasePin, OUTPUT);
  29. }
  30.  
  31. void loop() {
  32.   digitalWrite(transistorBasePin, HIGH); // Turn the LED on
  33.   delay(1000);
  34.   digitalWrite(transistorBasePin, LOW);  // Turn the LED off
  35.   delay(1000);
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement