articles » archived » raspberry-pi » alternate-bi-color-led

Raspberry Pi: Alternate Bi-color LED

A basic example to get started with the Raspberry PI

Required for this project is a Raspberry Pi 3 or newer with power supply, bicolor LED, 100-300 ohm resistor, breadboard, cables/wires, a basic understanding of the Python programming language and the Linux operating system.

This tutorial assumes the use of Windows 10.

Setup the Raspberry Pi

If the Raspberry Pi's Raspbian OS is already installed and configured, skip this section.

  1. Download and install SD Card Formatter for Windows; download from
  2. Quick format the Raspberry Pi's micro SD card.
  3. Download and install Win32 Disk Imager; download from
  4. Download Raspbian Lite from
  5. Install Raspbian Lite image using Win32 Disk Imager.
  6. Install micro SD card and boot Raspberry Pi device.
  7. Login as: pi, password: raspberry
  8. Configure Raspbian by issuing a sudo raspi-config command from the command line prompt.
    1. Under Network Options, setup wireless networking.
    2. Under Interface Options, enable SSH.
    3. Under Localization Options, configure the keyboard.
    4. Under Advanced Options, expand filesystem.
    5. Reboot.
    6. Login.
  9. Issue a sudo apt-get update && sudo apt-get upgrade command to update the system.
  10. To access the Raspberry Pi file system over the network from a Windows machine, install Samba.
    1. Issue a sudo apt-get install samba samba-common-bin command at the command line.
    2. Issue a sudo nano /etc/samba/smb.conf command to edit the samba configuration.
      1. Go to the end of the file and enter this text on new lines:
        comment=User Pi
        only guest=no
        create mask=0777
        directory mask=0777
        guest ok=yes
      2. Hit CTRL+X to exit.
      3. Save the file when prompted.
    3. Issue a sudo chmod 0777 /home/pi command at the command line.
    4. Reboot.
    5. Login.
  11. Type python at the command prompt. Python version 2.x should be in use though this project is compatible with version 3.x, too.

Program the Raspberry Pi

First, setup the breadboard. Connect one side of the LED to pin # 7 on the Raspberry Pi. Connect the other side to the resistor then connect the resistor to pin # 11 on the Raspberry Pi. See pin layout while noticing that pin one is square on the underside of the Raspberry Pi.

circuit diagram

Python Coding

Simply put, this code alternates polarity for pins 7 and 11 thereby changing the colors of the bicolor LED. Download this code and copy it to the shared directory or, if not using Windows, type this text into a new file. Name the file then to run it issue a sudo python command at the command line.

from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD) # set GPIO to use the pin numbers instead of pin names

led_pin1 = 7 # pin number 7
led_pin2 = 11 # pin number 11

GPIO.setup(led_pin1,GPIO.OUT) # set the pin to output
GPIO.setup(led_pin2,GPIO.OUT) # set the pin to output

state = 0

	print "Look at the bicolor LED. It should be alternating. Ctrl+C to end."
		GPIO.output(led_pin2,not state)
		state = not state # reverse state
		sleep(.25) # wait a quarter second
except KeyboardInterrupt:
alternating bicolor LED on a breadboard

Now create a garage door opener.

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.