otoh
Posts: 4
Joined: Wed Feb 08, 2017 9:12 pm

How to get 10+ analogue outputs

Wed Feb 08, 2017 9:58 pm

I'm doing an audio project that requires multiple analogue inputs and outputs:

Inputs are to read various potentiometers - I've found the MCP3008 ADC which will do this nicely over SPI.

Outputs are to drive bargraph LEDs for a visualiser. I'm currently planning 10 bands, each driven by an LM3914, which is purpose made for the job - it expects an analogue input and lights up the graph accordingly.

This is proving trickier to figure out. I discovered the MCP4725, which is generally available as an i2c breakout from eg Adafruit, Sparkfun - but I assume each individual unit will have the same address so I could only use one. The Sparkfun has a single address selector, but that's still only two.

I have yet to find a suitable multichannel ADC IC to handle this. Any suggestions on what I can use to tackle this?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8931
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: How to get 10+ analogue outputs

Thu Feb 09, 2017 9:05 am

Why not just drive your leds via digital outputs, rather than convert from digital from the Pi, to analogue for the lm3914, which then converts it back to digital for the leds? That seems a rather overengineered solution.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

otoh
Posts: 4
Joined: Wed Feb 08, 2017 9:12 pm

Re: How to get 10+ analogue outputs

Thu Feb 09, 2017 9:31 am

6by9 wrote:Why not just drive your leds via digital outputs, rather than convert from digital from the Pi, to analogue for the lm3914, which then converts it back to digital for the leds? That seems a rather overengineered solution.
Thanks for the reply. I guess because the LM3914 has some nice logic built in re lighting the bars, and providing differing modes of filled bars vs dot mode... I figure making it easier to code on the Pi side if I can just send it a number and let it do the work!

But as you suggest, if not, I can drive them digitally. Not sure what the best IC is - I have some MAX7219s that I guess should be able to do it?

PiGraham
Posts: 3938
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: How to get 10+ analogue outputs

Thu Feb 09, 2017 10:29 am

You can do an LED bar graph with a shift register. One SR per bar would need one gpio for each bar plus two gpio for clock and latch for all bars at once.

See 74HCT595 or devices specifically for driving LEDs (constant current etc)

You could also use ws2812 neopixels for options of fancy colour effects. Interfacing is nice and easy. Just one gpio pin required for any number of bars.

SOme code here: https://github.com/mage0r/WS2811_bar

You can also use a multiplexing / chalieplexing scheme, but Pi GPIO has rather limited current handling capability for driving LEDs so using external drivers is probably a better option.
Last edited by PiGraham on Thu Feb 09, 2017 1:08 pm, edited 1 time in total.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8931
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: How to get 10+ analogue outputs

Thu Feb 09, 2017 10:46 am

otoh wrote:
6by9 wrote:Why not just drive your leds via digital outputs, rather than convert from digital from the Pi, to analogue for the lm3914, which then converts it back to digital for the leds? That seems a rather overengineered solution.
Thanks for the reply. I guess because the LM3914 has some nice logic built in re lighting the bars, and providing differing modes of filled bars vs dot mode... I figure making it easier to code on the Pi side if I can just send it a number and let it do the work!
Dot/bar mode selection is pretty trivial in code too. To make the maths easy, assuming a 0-99 input value to 10 LEDs:
Dot:

Code: Select all

unsigned int output = 0;
if(value>90)
  output|=1<<9;
else if(value>80)
  output|=1<<8;
else if(value>70)
  output|=1<<7;
...
else if(value>10)
  output|=1<<1;
For bar mode, replace all the "else if(" with "if(".
(I know I've blundered there and will never light the bottom LED, but you get the idea. I don't know the size of your input data and that would affect all the numbers anyway)
Alternatively make a lookup table of input value to output bitmask - it costs more in memory (which isn't normally a limit on a Pi), but is faster to lookup.
Either method also allows you to apply non-linear representations to your bargraphs - switch between a virtual LM3914, LM3915, and LM3916 at will, or dream up your own.
otoh wrote:But as you suggest, if not, I can drive them digitally. Not sure what the best IC is - I have some MAX7219s that I guess should be able to do it?
Yes, at a quick glance the MAX7219 with the BCD decode mode bypassed would work.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

ElEscalador
Posts: 839
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: How to get 10+ analogue outputs

Thu Feb 09, 2017 1:29 pm

Have a look at a little gizmo called the Digispark - it's my new favorite pi accessory...6 IO pins - several can do PWM or ADC. Two for serial...you program it with Arduino sketch, can run on 3.3volts so easy pi interface compared to other Arduino, on-board led addressable just as pin 1 or 2.and you can get them for $1.50. (I bought a 5 pack on ebay for $7 or $8.) Your project will still require a few of them, but you can write the code for them to listen for a certain address...AND (one of my favorite parts) they come on a little board the size of a quarter that plugs right into a USB port for programming (from either a PI or a PC)...so I can carry around little microcontrollers and not need extra cables or programming boards...and they will run their programs from USB power too. Unless you need blazing fast reactions from the leds, give it a thought. I've got one reading a temp sensor and PWMing a fan to control temp in one of my PI cases.
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

PiGraham
Posts: 3938
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: How to get 10+ analogue outputs

Thu Feb 09, 2017 2:22 pm

ElEscalador wrote:Have a look at a little gizmo called the Digispark - it's my new favorite pi accessory...6 IO pins - several can do PWM or ADC. Two for serial...you program it with Arduino sketch, can run on 3.3volts so easy pi interface compared to other Arduino, on-board led addressable just as pin 1 or 2.and you can get them for $1.50. (I bought a 5 pack on ebay for $7 or $8.) Your project will still require a few of them, but you can write the code for them to listen for a certain address...AND (one of my favorite parts) they come on a little board the size of a quarter that plugs right into a USB port for programming (from either a PI or a PC)...so I can carry around little microcontrollers and not need extra cables or programming boards...and they will run their programs from USB power too. Unless you need blazing fast reactions from the leds, give it a thought. I've got one reading a temp sensor and PWMing a fan to control temp in one of my PI cases.
Digisparks are great, but not needed here. The Pi can drive the LEDs as easily as the Digispark could so you just add some complexity. Shift registers are addressable LEDs are a better option IMHO. 6 I/o (4 if using USB) is not enough to directly drive the LEDs.

otoh
Posts: 4
Joined: Wed Feb 08, 2017 9:12 pm

Re: How to get 10+ analogue outputs

Fri Feb 10, 2017 9:33 am

6by9 wrote: Alternatively make a lookup table of input value to output bitmask - it costs more in memory (which isn't normally a limit on a Pi), but is faster to lookup.
Either method also allows you to apply non-linear representations to your bargraphs - switch between a virtual LM3914, LM3915, and LM3916 at will, or dream up your own.
otoh wrote:But as you suggest, if not, I can drive them digitally. Not sure what the best IC is - I have some MAX7219s that I guess should be able to do it?
Yes, at a quick glance the MAX7219 with the BCD decode mode bypassed would work.
Many thanks for pointing me in the right direction - I'm convinced :) I've actually done some similar stuff before on the Arduino but long enough ago that I'm having to relearn a lot, and get familiar with Python - but it all seems doable.

I'm still curious about getting many analogue outputs though - some future iteration of this may involve a VU meter based on vacuum-tube bargraphs which would definitely need an analog signal. The longer-term search continues...

otoh
Posts: 4
Joined: Wed Feb 08, 2017 9:12 pm

Re: How to get 10+ analogue outputs

Fri Feb 10, 2017 9:38 am

PiGraham wrote:You can do an LED bar graph with a shift register. One SR per bar would need one gpio for each bar plus two gpio for clock and latch for all bars at once.

See 74HCT595 or devices specifically for driving LEDs (constant current etc)
Many thanks for the reply - it's hugely useful to get different ideas :) I've actually used the same shift registers before, on an arduino project for driving 14 segment tubes - but long enough ago that I've forgotten how it all works!

Could definitely do it with 74HCT595 but it would involve many of them (200+ LEDs?) and/or some fiddly multiplexing - I am thinking of going with a driver IC like MAX7219.

PiGraham
Posts: 3938
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: How to get 10+ analogue outputs

Fri Feb 10, 2017 10:59 am

otoh wrote:Could definitely do it with 74HCT595 but it would involve many of them (200+ LEDs?) and/or some fiddly multiplexing - I am thinking of going with a driver IC like MAX7219.
Still simpler than using ten DACs and bargraph drivers.

MAX7219 seems a reasonable choice if you want to multiplex. Four chips required.
If you don't want to multiplex the TLC5926/TLC5927 could be a good option (13 chips to direct-drive 200 LEDs with 3 gpio).

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: How to get 10+ analogue outputs

Fri Feb 10, 2017 8:21 pm

Total different direction: If you have to send a signal :
I figure making it easier to code on the Pi side if I can just send it a number and let it do the work!
Why not use ten MCP23017 or MCP23016 and send a signal to them?
You can control 16 LEDs per chip and you can make ANY pattern. Dot graphs and bar graphs are but also a bar graph with dot 'peak' indicator etc.
Needs only two GPIO pins and can be controlled by python.
Have a look here https://github.com/fenlogic/multio for a 128 port I/O expander.
Schematics, PCB and example code. All free.

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to get 10+ analogue outputs

Fri Feb 10, 2017 9:31 pm

Gert van Loo wrote:
Why not use ten MCP23017 or MCP23016 and send a signal to them?
You can't address more than eight MCP23017s without using an I²C port expander.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: How to get 10+ analogue outputs

Sat Feb 11, 2017 12:23 pm

:oops: Oops yes, you can only address eight. :oops:
I thought 128 LEds would be enough for anybody.....
You could of course use 12 LEDs per indicator but the programming gets a tiny bit more difficult......

(But if you use 8 LEDS you can need only five chips and easiest to program)

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