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

Monday, 9 March 2015

Single LED - Flashing


In this series I am going to play around with LED's. These simple devices are just great to learn the basic's and even some advanced electronics.

This specific one is quite a simple one. Basically just making an LED switch on and off. Once again using the Rasperry Pi and its GPIO pins.

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

Layout


Here we connect a jump lead (Red) from one of the GPIO Pins (18 in this case) to a resistor which in turn is connected to the anode (positive) end of the LED. The Cathode (negative) end of the LED is then connected to Ground, by the black jump lead.

The Code

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)

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

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