Maker.io main logo

Make an LED Flasher with a Raspberry Pi

2016-12-01 | By All About Circuits

License: See Original Project Raspberry Pi

Courtesy of All About Circuits

Flash LEDs using your RPi!

LEDs can be found in most electronic devices. They can provide stunning effects for products with variations in color, intensity, and flashing rates. In this project, we’ll connect an LED to a Raspberry Pi and control it with a tactile pushbutton switch. The device you'll build with this simple LED controller is a pushbutton switch actuated electronic flasher.

This is a continuation of an earlier project:

Build a Raspberry Pi Pushbutton Switch

Project Parts Lists

Components to build the Raspberry Pi LED flasher are listed below.

Miscellaneous

  • Solderless breadboard
  • Jumper wires (hand stripped 22AWG solid wires or Adafruit Breadboarding wires Product ID: 153)
  • USB cable

A Variety of Raspberry Pi's

Before we build our RPi LED flasher, let's talk about the variety of Raspberry Pis in the maker sphere. There are four variations of the RPi (Raspberry Pi) low-cost SBC (Single Board Computer) that you can choose from for this project. The Model A is a 256MB RAM, 700MHz ARM processor SBC and can operate efficiently on a 9V battery. The major electronics peripherals included on the A are:

  • LCD connector
  • camera connector
  • HDMI connector
  • USB 2.0 connector
  • 3.5mm audio jack
  • 40 pin dual in line GPIO (general purpose Input-Output) connector
  • Ethernet port

The RPi Model A%2B

The RPi Model A

The Model B has 512MB of RAM, twice the memory of the A . In addition to the Model A’s electronics peripherals, the B version of the RPi has an extra USB port along with composite video. The composite video RCA jack allows you to connect your RPi to an ordinary television. The Model B’s dual-in-line connector only provides 26 GPIO pin accessibility as compared to the A-version SBC. A standard SD card is included to store the Linux operating system and application files.

The Model B

The Model B

The next iteration is the Model B single board computer. The Model B has 4 USB ports instead of the 2 that the Model B has. It includes a 40-pin dual-in-line GPIO connector and a microSD card instead of the Model B's traditional bulky SD card. The Model B also comes standard with an HDMI connector.

The Model B%2B

The Model B

The Pi 2 Model B’s processor is a 900MHz quad core (BCM2836, ARM v7) with 1GB of RAM. The previously mentioned RPi models use a single core ( BCM2835, ARM v6) component. The Pi 2 has the same peripherals as the Model B , which makes the hardware’s pin-to-pin compatibility seamless. However, “your existing Raspberry Pi SD card images may not work because the firmware and kernel must be recompiled/adapted for the new processor," according to Adafruit’s website. So you’ll need to give extra attention when upgrading firmware and Linux operating versions.

The Pi 2 Model B

The Pi 2 Model B

Any one of these models can be used in this project.

LED Flasher Block Diagram

An electronic LED flasher can be built easily with a 555 Timer IC, a few resistor-capacitor components, and (of course) an LED. The only difference between the electronic LED flasher and the RPi device is that we’re using software to adjust the flashing rate instead of changing hardwired resistor-capacitor component values. The RPi flasher has four sub-circuit building blocks. These sub-circuit building blocks are: a tactile pushbutton switch, an RPi, an opto-isolator, and an LED with a current limiting resistor. Below is a block diagram of the RPi LED flasher:

Componets required to build an RPi LED FlasherComponets required to build an RPi LED Flasher

The electrical-electronic components required to build an RPi LED Flasher

I provided an enhanced block diagram of the RPi LED flasher showing the actual electrical-electronic components for the project as an additional reference.

A modified RPi LED Flasher block Diagram

A modified RPi LED Flasher block diagram showing the actual electrical-electronic components

I used the Model B version of the RPi, but you can use the other models discussed previously as well. Now, let's move on to the opto-isolator!

A typical electronic symbol for an opto-isolator

A typical electronic symbol for an opto-isolator

Using an opto-isolator in this project is a nice way to use high voltage sources without damaging your RPi. The LED’s brightness is controlled by the onboard 5VDC power supply. The will turn on the phototransistor will be turned on by the opto-isolator. This will drive the LED using the onboard 5VDC power supply. Now let's build an RPi LED flasher!

The RPi LED Flasher Hardware

Building the RPi LED flasher is relatively easy. You can use either a solderless breadboard wiring diagram or an electronic circuit schematic diagram. You can find each diagram below. I suggest using the solderless breadboard wiring diagram for this project if you’re new to electronics circuits. For experienced electronics makers, I’d recommend the circuit schematic diagram is the preferred wiring document of choice. Remember to pay close attention to the Pi cobbler you are using based on which RPi model you use. The Pi cobbler pin outs are different for each of the RPi model sets.

The solderless breadboard wiring diagram

The solderless breadboard wiring diagram

Scheme-It

The electronic circuit schematic diagram

Unfortunately, the circuit schematic diagram doesn't provide details on how the photodiode-phototransistor pairs/optical channels are placed inside the 16 pin dual-in-line package (DIP). The CNY74-4H-ND opto-isolator electrical pin-out is shown below.

The CNY74-4H-ND

The CNY74-4H-ND’s physical package and electrical pin out

After completing the RPi LED Flasher’s hardware build, I suggest rechecking your circuit for wiring errors before applying power to your RPi. Here's a picture of my RPi LED Flasher circuit. Now that we’ve put our flasher together, we just need to implement the code!

The RPi LED Flasher

The RPi LED Flasher

The RPi LED Flasher Python Code

Here’s the gist of what the code does: when the pushbutton switch is released, the LED will stop flashing. Pretty simple. I named the Python code as pbswitch_flasher.py. Open Python IDLE editor or the LXTerminal's nano and type the RPi LED flasher code shown below into the LXTerminal screen.

Copy Code
                  # *********** Pushbutton Switch LED Flasher ******************
#
# created by Don Wilcher Nov 28, 2015
#
#
# Press/hold tactile pushbutton switch, LED flashes
# Release tactile pushbutton, LED turns off

# Add libraries to python script

import RPi.GPIO as GPIO
import time

# Include BCM I/O pins into python script and define pin numbers

GPIO.setmode(GPIO.BCM)
pbswitch_pin = 4
LED_pin = 18

# Create pbswitch pin as an active low switch (use RPi internal pullup resistor)
# and define LED pin as an output

GPIO.setup(pbswitch_pin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(LED_pin, GPIO.OUT)

# Define and set (initialize) the LED output state as False

LED_state = False


# pbswitch event monitoring loop: check pbswitch_pin and flashes LED output based on new input event being False

while True:
new_input_event = GPIO.input(pbswitch_pin)
if new_input_event == False:
LED_state = not LED_state
GPIO.output(LED_pin, GPIO.HIGH)
time.sleep(.1)
GPIO.output(LED_pin, GPIO.LOW)
time.sleep(.1)

To run the code on your RPi, in the LXTerminal type the following linux command after the prompt.

Copy Code
pi@raspberrypi ~ sudo python pbswitch_flasher.py

You’ll know it worked if the LED flashes when you press the button. Congratulations on building your second RPi controller! You can adjust the flash rate by changing the value of time (in seconds) to 1 sec. You can do this with the line of code below:

Copy Code
time.sleep(.1): change .1 to 1

You can create different flashing patterns by writing timing values that aren’t equal into the time.sleep() Python instruction.

 

制造商零件编号 CNY74-4H
OPTOISO 5.3KV 4CH TRANS 16-DIP
Vishay Semiconductor Opto Division
¥17.02
Details
制造商零件编号 SC0563
SBC 1.0GHZ 1 CORE 512MB RAM
Raspberry Pi
¥203.50
Details
制造商零件编号 SC1029
SBC 900MHZ 4 CORE 1GB RAM
Raspberry Pi
¥284.90
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum