Jump to content
 
  • entries
    7
  • comments
    6
  • views
    11,213

6. 2-aspect signal control from a single GPIO pin


DavidB-AU

1,450 views

In the previous tutorial we saw two different ways to control an LED and how the same state can have the opposite effect depending on how the circuit is designed. We can use this to our advantage by driving both LEDs from the same GPIO pin. This requires a little more electronics.

blogentry-6959-0-85688500-1359256382.png

 

This does two things.

- When pin 18 is HIGH (True), it triggers the NPN transistor to power the red LED but prevents powering the green LED because the anode and cathode are both at 3.3V.

- When pin 18 is LOW (False), the transistor is turned off, hence the red LED is turned off, and the green LED is powered because the anode is at 3.3V and cathode is at 0V.

 

Any general purpose NPN transistor should work. I used a BC548 because I had one handy. Here's how it looks on the board.

blogentry-6959-0-44479600-1359256386.jpg

 

The code to run this is very simple.

import RPi.GPIO as GPIOimport time# Set up header pin 18 (GPIO24) as outputGPIO.setup(18, GPIO.OUT)state = Truewhile 1:  GPIO.output(18, state)  time.sleep(1)  state = not state

Run this as root and the LEDs will alternate, driven from the same pin.

 

Now let's adapt the previous signal control program. Create a program called signal2.py and enter the following:

import os, sys, termiosimport RPi.GPIO as GPIOGPIO.setup(18, GPIO.OUT)    # Set up pin 18 (GPIO24) as outputGPIO.output(18, True)       # Set the signal to red# Set up detection of keypressdef getKey():  fd = sys.stdin.fileno()  old = termios.tcgetattr(fd)  new = termios.tcgetattr(fd)  new[3] = new[3] & ~termios.ICANON & ~termios.ECHO  new[6][termios.VMIN] = 1  new[6][termios.VTIME] = 0  termios.tcsetattr(fd, termios.TCSANOW, new)  key = None  try:    key = os.read(fd, 3)  finally:    termios.tcsetattr(fd, termios.TCSAFLUSH, old)  return keystate = True                # The initial state for the signal is redwhile 1:  x = str(getKey())  if x == "r":              # If keypress is r    state = True            # ... set the state that turns the signal red  if x == "g":              # If keypress is g    state = False           # ... set the state that turns the signal green  if x == "q":              # If keypress is q    GPIO.output(18, True)   # ... set the state that turns the signal red    break                   # ... and quit  else:                     # In all other cases    pass                    # ... do nothing  GPIO.output(18, state)    # Set the state of the signal

Run as root and the signal control will work as in tutorial 4, but driven from only one GPIO pin.

 

blogentry-6959-0-07311300-1359257729.png

  • Like 2

2 Comments


Recommended Comments

Why connect the red LED between +3.3V and the transistor, why not connect it between GPIO and 0V?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...