dionick
Posts: 14
Joined: Sun Mar 02, 2014 8:08 pm

Best way to control LED's, sound (and video) simultaneously

Sun Mar 02, 2014 9:55 pm

Hey,

I'm new to the "Raspeberry Pi Forum" and I consider myself a begginer despite the fact that I already have a Raspeberry Pi; but I'am here for a different reason, I would like to know how could I control LED's , sound and ,maybe, video just by clicking on a button (by the way, this is to incorporate on a school project) .

I know that could look weird but I will explain:I would like to when a button is pressed a sequence with a maximum of 10 LED's is started the way I want as there was a narration (and if possible, a video), which evolved with the LED sequence. I wish that some of you could briefly explain what material would be necessary and, if possible, show me some tutorial. I wish the project being as simple and cheap as possible.

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Best way to control LED's, sound (and video) simultaneou

Sun Mar 02, 2014 11:13 pm

An easy way to control a large number of lights is using SPI and shift registers. I have a quick tutorial here:

http://www.cupidcontrols.com/2013/12/tu ... -and-leds/

By 'clicking a button', I assume you mean in a browser? Or do you mean on the display of the Pi?

Colin
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

dionick
Posts: 14
Joined: Sun Mar 02, 2014 8:08 pm

Re: Best way to control LED's, sound (and video) simultaneou

Sun Mar 02, 2014 11:41 pm

I refer to a button, a button embeded on a school mockup.

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Best way to control LED's, sound (and video) simultaneou

Sun Mar 02, 2014 11:50 pm

A physical button? That's easy. You can get millions of different types of buttons, and then just have a program respond to a button press by sending data to either other GPIO or, as I mentioned above, to SPI, to control your LEDs.

The button is just a detect on contact close that changes logic state. Either use a pull-up to 3.3V to set your GPIO high when the button is not active or a pull-down to ground set it low. For the first case, you will have 3.3V on one side of your button and your GPIO on the other. For the second, you have GPIO on one side and ground on the other.

Here is a good python code example for how to respond to button input. This is a good place to start, and you can start by using the GPIO to directly drive LEDs. When you either want to use your GPIO for something else (or the number of LEDs you require exceeds the number of GPIO you have available - 8 total on the Pi), start putting them on SPI and you can have really as many as you want.

Colin
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

dionick
Posts: 14
Joined: Sun Mar 02, 2014 8:08 pm

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 12:20 am

But I would like to combine the LED's sequence and the sound (narration), just by clicking in the physical button, and I don't know how to do that. That's why I'am asking for a brief explanation without too much technical information and if possible a tutorial. It must be simple and cheap.

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 1:13 am

I'm sorry, but I'm really unclear what exactly you mean. Perhaps you could give an operational sequence for what you are talking about to specify exactly what interaction you are trying to describe.
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 2:39 am

Easy peasy, and you can do it in Python. If you don't know Python then now is an excellent time to learn.

For the hardware side, you can use one GPIO per LED, and one for the switch input. You have enough GPIOs to do this.

Do you need all LEDs on at the same time? If not, you can drive the LEDs directly from the GPIO. Each LED will need a current limiting resistor of around 200 ohms or so. If you do need several (or all) of them on at the same time then you should use a buffer transistor for each LED.

The switch is easy to hook up. Just connect it from a GPIO to ground.

On the software side, you need something like this (it's not real code, just the outline of the idea). Basically, when playing your video or audio you should use a stopwatch and make a note of how long each LED should be turned on before lighting up the next one. You will end up with a list of 10 intervals. Start the video running, then, in parallel, turn the LEDs on in order:

Code: Select all

while True

  while button_is_not_pressed
    loop

  start_video (or start_audio)

  turn_on_LED(0)
  wait(LED_0_on_time)
  turn_off_LED(0)

  turn_on_LED(1)
  wait(LED_1_on_time)
  turn_off_LED(1)

  turn_on_LED(2)
  wait(LED_2_on_time)
  turn_off_LED(2)

  etc.
You can use a system call to start the video or audio, and you could have a Python list containing the LED number and delay time for each step. You could iterate through the list so that you end up with simpler, shorter code:

Code: Select all

' List of LED numbers and 'on' periods
LED_timing=[(0,2),(1,3),(2,3) etc.]
for LED_info in LED_timing:
  turn_on_LED(LED_info(0))
  delay(LED_info(1))
  turn_off_LED(LED_info(0))
You could also put the LED numbers and 'on' times in a file and read that with Python, then it can be edited independently from the source code.

Anyway, start small, and build up from there.

Here's how to drive one LED:
https://projects.drogon.net/raspberry-p ... ingle-led/

There are many more examples on the web.

Good luck!

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 2:43 am

iinnovations wrote:When you either want to use your GPIO for something else (or the number of LEDs you require exceeds the number of GPIO you have available - 8 total on the Pi), start putting them on SPI and you can have really as many as you want.

Colin
There are many more than 8 GPIO pins on the Pi.

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 4:31 am

ame wrote:
iinnovations wrote:When you either want to use your GPIO for something else (or the number of LEDs you require exceeds the number of GPIO you have available - 8 total on the Pi), start putting them on SPI and you can have really as many as you want.

Colin
There are many more than 8 GPIO pins on the Pi.
Not if you reserve the shared GPIO pins for the other interfaces. There are only 8 GPIO that have not been assigned for other purposes. These are 4, 17, 21, 22, 18, 23, 24 and 25.

http://www.hobbytronics.co.uk/raspberry-pi-gpio-pinout
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 5:05 am

iinnovations wrote:
ame wrote:
iinnovations wrote:When you either want to use your GPIO for something else (or the number of LEDs you require exceeds the number of GPIO you have available - 8 total on the Pi), start putting them on SPI and you can have really as many as you want.

Colin
There are many more than 8 GPIO pins on the Pi.
Not if you reserve the shared GPIO pins for the other interfaces. There are only 8 GPIO that have not been assigned for other purposes. These are 4, 17, 21, 22, 18, 23, 24 and 25.

http://www.hobbytronics.co.uk/raspberry-pi-gpio-pinout
Yes, but the OP doesn't need the extra functions, so he can use all 17 of them, which is plenty for the 10 LEDs and one button.

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 5:11 am

ame wrote: Yes, but the OP doesn't need the extra functions, so he can use all 17 of them, which is plenty for the 10 LEDs and one button.
Why is why I said that he should start there. I offered SPI as a flexible alternative/upgrade. It would also be unfortunate to have him/her all the way down that path and then realize he wanted SPI, I2C, UART, etc.
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

dionick
Posts: 14
Joined: Sun Mar 02, 2014 8:08 pm

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 1:42 pm

Thank you for all the support!

Ok so here's my sequence as asked:

1-Start button;
2-Sound (and video) starts;
3-Led 1 turns on at ,for example, 00m.02s.00;
4-Led 1 turns off at ,for example, 00m.05s.00;
5-Led 2 turns on at ,for example, 00m.05s.50;
6-Led 2 turns off at ,for example, 00m.08s.00;
7-Led 3 turns on at ,for example, 00m.08s.50;
8-Led 3 turns off at ,for example, 00m.25s.00;
9-Led 4 turns on at ,for example, 00m.25s.50;
10-Led 4 turns off at ,for example, 00m.30s.00;
11-Led 5 turns on at ,for example, 00m.30s.50;
12-Led 5 turns off at ,for example, 00m.35s.00;
13-Led 6 turns on at ,for example, 00m.35s.50;
14-Led 6 turns off at ,for example, 00m.40s.00;
17-Led 7 turns on at ,for example, 00m.45s.00;
16-Led 7 turns off at ,for example, 00m.50s.00;
17-Led 8 turns on at ,for example, 00m.50s.50;
18-Led 8 turns off at ,for example, 01m.10s.00;
19-Sound (and video) stops at ,for example, 01m.10s.00;

If possible create a material list.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 6:54 pm

Ok, that matches what I thought.

We're not going to do your homework for you, although my pseudo-code is pretty close to what you want. What is your next step?

Will you use Python? Have you got the LEDs, resistors and switch?

dionick
Posts: 14
Joined: Sun Mar 02, 2014 8:08 pm

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 7:47 pm

I will use the simplest and cheapest method (I am waiting for someone to say which method is the better) and I have no material yet.

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 8:00 pm

Easiest and cheapest is GPIO

LEDs:
http://www.adafruit.com/products/299

Here's a tutorial on using them: http://learn.adafruit.com/all-about-leds

Buttons:
http://www.adafruit.com/products/1119

Breadboard:
http://www.adafruit.com/products/64

Breadboard wires:
http://www.adafruit.com/products/153

I'd recommend one of these for breadboarding:
http://www.adafruit.com/products/914

Head over to ebay and pick up a bunch of cheap resistors in a variety pack. Make sure to get plenty in the 50-1000 ohm range. The rest is just programming.

Colin
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 8:03 pm

You should also mention if you have a monitor and speakers for the video and audio. That part is straightforward and up to you.
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Best way to control LED's, sound (and video) simultaneou

Mon Mar 03, 2014 11:59 pm

dionick wrote:I will use the simplest and cheapest method (I am waiting for someone to say which method is the better) and I have no material yet.
Ok. The simplest way to do it is exactly as I suggested.

Use Python for programming. Use the Python RPi.GPIO library for handling the LEDs and switches on the GPIO pins.

Get 10 LEDs and 10 220 ohm resistors and some wire, and a 26-pin plug (or some other way to connect to the GPIO header).

Get a pushbutton switch that is robust and looks nice for your project.

If you are doing only audio, get a small amplified speaker to plug into the 3.5mm audio jack.
If you are doing audio and video, get a small amplified speaker to plug into the 3.5mm audio jack, and a composite-input TV, or an HDMI input TV or monitor.

Write a Python program having the general structure I outlined above. I recommend you come up with some sort of data format for the LED on and off times to store in a file, then read the file in a loop in Python.

???

Profit!

However, even with such a clearly-defined goal it's going to be a bit overwhelming unless you break the problem down into manageable parts.

Right now, even without any extra hardware, you can make a program with the basic functionality of the final version.
* Instead of a GPIO pushbutton, wait for the user to press the space bar
* Instead of LEDs, print a message on the screen saying which LED would be turned on or off
* The video or audio is already supported by the Pi hardware

If even this is too new for you try the Python 'Hello world' example and work from there.

garycooke
Posts: 79
Joined: Wed Feb 12, 2014 10:38 am

Re: Best way to control LED's, sound (and video) simultaneou

Tue Mar 04, 2014 7:49 am

I have just read the bit about the 10 leds and 10 220 ohm resistors for the gpio pins, can you tell me why they are 220 ohm and how you work it out please

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Best way to control LED's, sound (and video) simultaneou

Tue Mar 04, 2014 8:56 am

It's a classic electronic calculation.

LEDs typically need 2 volts and will light up with a few milliamps.

The GPIO 'high' state is 3.3 volts. So we have to dissipate the extra 1.3V in a resistor.

Using Ohm's law, that resistor drops 1.3V at, say, 5mA, so its value is 1.3/0.005 ohms, which is 260 ohms.

If we make the resistor a little lower (220 ohms) then the current will be a little higher.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Best way to control LED's, sound (and video) simultaneou

Tue Mar 04, 2014 10:24 am

Here's an online calculator:
http://led.linear1.org/1led.wiz

Note that you shouldn't draw more than about 15mA from the GPIO pins. If you plug those numbers in then you get 100 ohms. So, your choice of resistor is anything from 100 ohms (max. current, so max. brightness) to about 300 ohms (allows just enough current to light the LED). Any lower than 100 ohms may damage the GPIO pin, any higher than about 300 ohms and the LED will be too dim, but feel free to experiment.

You can take the same LED and do the same calculation to power it from a 5V supply, or a 12V supply. You just need a bigger resistor.

Tarcas
Posts: 741
Joined: Thu Jan 09, 2014 5:38 am
Location: USA

Re: Best way to control LED's, sound (and video) simultaneou

Tue Mar 04, 2014 4:58 pm

When you get your LEDs and buttons, I recommend running through this tutorial to understand a little more about how things are wired up and how they work. The code to the final program is also a good starting point to build what you're looking at doing. (It incorporates lighting, flashing, and unlighting LEDs in a timed pattern, following a button press.) Code is available both as a shell script and a C program. You should be able to modify either for your purposes. Just pick the one you're more comfortable modifying.

https://projects.drogon.net/raspberry-p ... -crossing/

dionick
Posts: 14
Joined: Sun Mar 02, 2014 8:08 pm

Re: Best way to control LED's, sound (and video) simultaneou

Tue Mar 04, 2014 6:03 pm

Thank you for the support!

Maybe today I will post a list of the meterial that I will buy so you can approve it.

I there any way that when raspberry pi is turned off no additional command is required for the project continues to operate? Like a automatic initialization on startup?

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Best way to control LED's, sound (and video) simultaneou

Tue Mar 04, 2014 8:56 pm

garycooke wrote:I have just read the bit about the 10 leds and 10 220 ohm resistors for the gpio pins, can you tell me why they are 220 ohm and how you work it out please
iinnovations wrote:Here's a tutorial on using them: http://learn.adafruit.com/all-about-leds
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Best way to control LED's, sound (and video) simultaneou

Tue Mar 04, 2014 8:58 pm

ame wrote:Here's an online calculator:
http://led.linear1.org/1led.wiz

Note that you shouldn't draw more than about 15mA from the GPIO pins. If you plug those numbers in then you get 100 ohms. So, your choice of resistor is anything from 100 ohms (max. current, so max. brightness) to about 300 ohms (allows just enough current to light the LED). Any lower than 100 ohms may damage the GPIO pin, any higher than about 300 ohms and the LED will be too dim, but feel free to experiment.

You can take the same LED and do the same calculation to power it from a 5V supply, or a 12V supply. You just need a bigger resistor.
The proper way to do this is using the current/voltage of the LED, not guesswork.
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Best way to control LED's, sound (and video) simultaneou

Tue Mar 04, 2014 11:51 pm

iinnovations wrote: The proper way to do this is using the current/voltage of the LED, not guesswork.
Some of us know what we're doing.

Return to “Automation, sensing and robotics”