Raspberry Pi: Alternate Bi-color LED
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.
- Download and install SD Card Formatter for Windows; download from sdcard.org.
- Quick format the Raspberry Pi's micro SD card.
- Download and install Win32 Disk Imager; download from sourceforge.net.
- Download Raspbian Lite from raspberrypi.org.
- Install Raspbian Lite image using Win32 Disk Imager.
- Install micro SD card and boot Raspberry Pi device.
- Login as: pi, password: raspberry
Configure Raspbian by issuing a sudo raspi-config command from the command line prompt.
- Under Network Options, setup wireless networking.
- Under Interface Options, enable SSH.
- Under Localization Options, configure the keyboard.
- Under Advanced Options, expand filesystem.
- Issue a sudo apt-get update && sudo apt-get upgrade command to update the system.
To access the Raspberry Pi file system over the network from a Windows machine, install Samba.
- Issue a sudo apt-get install samba samba-common-bin command at the command line.
Issue a sudo nano /etc/samba/smb.conf command to edit the samba configuration.
Go to the end of the file and enter this text on new lines:
[user_pi] comment=User Pi path=/home/pi browseable=yes writeable=yes only guest=no create mask=0777 directory mask=0777 public=yes guest ok=yes
- Hit CTRL+X to exit.
- Save the file when prompted.
- Go to the end of the file and enter this text on new lines:
- Issue a sudo chmod 0777 /home/pi command at the command line.
- 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.
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 alternate.py then to run it issue a sudo python alternate.py 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 try: print "Look at the bicolor LED. It should be alternating. Ctrl+C to end." while(1): GPIO.output(led_pin1,state) GPIO.output(led_pin2,not state) state = not state # reverse state sleep(.25) # wait a quarter second except KeyboardInterrupt: GPIO.cleanup()
Now create a garage door opener.