Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Lighting up 5 LEDs with I2C or SPI

Thu Jun 18, 2015 11:53 pm

I would like to be able to have say 5 LEDs that I can toggle each on individually. For example, I would like to tell it to turn on the first three or every other one on, but keep the other ones off. I don't want it to be limited by the number of output pins I have on my pi 2, so I am guessing the best way is to do it with I2C or SPI. I just don't really know how to go about doing it. I am guessing it involves a decoder and several flip flops or maybe I am completely wrong and there is an easier way. Any suggestions?

Thanks for your help, I'm new to pi.

User avatar
mikronauts
Posts: 2717
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Lighting up 5 LEDs with I2C or SPI

Fri Jun 19, 2015 12:18 am

Use MCP23017 I2C I/O expanders, you can have eight of them, with 16 I/O's each, on an I2C bus.

I use them a fair bit, so I even designed a board around them that holds two of them :)

http://www.mikronauts.com/raspberry-pi/pi-rtc-dio/

Here is an article on how one I use to read a 4x4 keypad:

http://www.mikronauts.com/raspberry-pi/ ... nd-python/

There are a ton of other examples, google "raspberry pi mcp23017"
Cole1717 wrote:I would like to be able to have say 5 LEDs that I can toggle each on individually. For example, I would like to tell it to turn on the first three or every other one on, but keep the other ones off. I don't want it to be limited by the number of output pins I have on my pi 2, so I am guessing the best way is to do it with I2C or SPI. I just don't really know how to go about doing it. I am guessing it involves a decoder and several flip flops or maybe I am completely wrong and there is an easier way. Any suggestions?

Thanks for your help, I'm new to pi.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
joan
Posts: 14200
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Lighting up 5 LEDs with I2C or SPI

Fri Jun 19, 2015 7:57 am

Ditto the MCP23017.

Here is one driving some LEDs.

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5052
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Lighting up 5 LEDs with I2C or SPI

Fri Jun 19, 2015 2:00 pm

There is also the MCP23008 (it's 8-bit cousin)** and, the somewhat simpler to program, PCF8574 (also 8-bit)****. Both are I2C devices. FWIW, an example of their use with various LED's can be found here:
http://www.cpmspectrepi.webspace.virgin ... oFull.html
Trev.
** SPI versions of both MCP devices also exist.
**** Often used for (4-bit) I2C interfaces to LCD displays (16x2, 20X4 etc.)
Still running Raspbian Jessie on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W) but Stretch on my 2xP3A+, P3B+, P3B, B+, A+ and a B2. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Re: Lighting up 5 LEDs with I2C or SPI

Fri Jun 19, 2015 4:10 pm

Thank you so much! I love these forums. I'll order some and try them out.

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5052
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Lighting up 5 LEDs with I2C or SPI

Fri Jun 19, 2015 4:20 pm

Cole1717 wrote:Thank you so much! I love these forums. I'll order some and try them out.
Have fun & let us know how you get on.
Trev.
Still running Raspbian Jessie on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W) but Stretch on my 2xP3A+, P3B+, P3B, B+, A+ and a B2. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Return to “Interfacing (DSI, CSI, I2C, etc.)”