Wednesday 25 March 2015

Single LED with Transistor - Flashing

Going on from my previous post of a flashing LED. Here I have introduced an NPN transistor, as in later projects I will be using them to control LED segments, namely 7 segment LED displays.

A transistor has three pins. A base, collector and emitter. A small current at the base control's the switching of current from the collector to the emitter of the transistor. 

The ingredients:
  • 1 x LED (Any colour)
  • 1 x resistor (round the 250ohm mark)
  • 1 x NPN Transistor (2N3904)
  • A few jump wires

Layout


Here we have connect 5v from GPIO 2 to the collector pin on the transistor (Red Wire). Then we connected the base pin on the transistor to GPIO 25 (Yellow Wire) to control the switching. And then from the emitter pin on the transistor, we attached a resistor and then connected that to the anode pin on the LED and in turn connected the cathode pin on the LED to the ground on the Pi (Black Wire).

The Code

Using basically the same code as in the previous project :

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)

while True:
    GPIO.output(25, True)
    time.sleep(1)
    GPIO.output(25, False)
    time.sleep(1)

This now loops and every second gives a current to the base pin on the transistor via GPIO 25. This causes a current to flow from the collector to the base pins on the transistor intermittently every second, which in turn makes the LED flash on and off

No comments:

Post a Comment