Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: 15yearoldnoob on Apr 14, 2016, 01:52 am

Title: Can someone help with shift register - i need more outputs but dont understand
Post by: 15yearoldnoob on Apr 14, 2016, 01:52 am
I have spent the last weeek trying to read everything ?I can on shoft registers, but its getting to point I dont understand any of it.

I have a Mega with 64 pins, and Im trying something where I need 200 LED options.
think syncronized lights that arrange together to spell out words.

I have easily do this with the 64 pins I have, but I want more.
And my problem is I still dont understand how I can call upon say pin 103 to turn on and off.
Then turn leds 107-115 on then off.

Does anyone have any words of wisdom or even a sample code I can use for this instance.
All I want to do it be able to call upo0n specific pins within the whole connection maze that will be the shift registers when all is said and done.

Thank you for anyone who truly takes the time to answer this with the intent of actually helping.
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: ieee488 on Apr 14, 2016, 03:52 am
The answer would depend on the LEDs you intend on using.

Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: 15yearoldnoob on Apr 14, 2016, 06:04 am
Since this is a programming question - lets say I get the "right" LEDs whichever those maybe be.
3 v LEDs are pretty standard arent they?  But for the arguement lets just focus on any type - or you create a type of LED example, and I can adjust the programming to it.

All I need is a sample of how I would power up say LED #97 in a 200 LED set up.
Lets not step off course..this is a shift register and programming a shift register question not LED question.  So can we focus on that.

All it takes is one person to completely hijack and sway a question of course.
Lets try not to do that here please.

Anyone else wanna give this a try.
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: Grumpy_Mike on Apr 14, 2016, 09:30 am
You can not just change one pin in a chain of shift registers you have to send all the data out to all of them. However you can have the same effect as just changing one pin if the data you send out is exactly the same as last time with the exception of the bit you want to change.

The data you send to the shift registers is kept in a variable, number of variables or an array. Your code has to translate the PIN number you want to change into the correct variable and the bit in that variable. This will depend on how your code is written. The bit write instruction can help you do this.

The type of led is important because shift registers are limited in the total amount of current they can supply so you might end up overloading them.
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: Paul__B on Apr 15, 2016, 05:21 pm
If you are going to control 200 LEDs, the only sensible way is to use a MAX7219 (https://www.sparkfun.com/datasheets/Components/General/COM-09622-MAX7219-MAX7221.pdf) or more precisely four of them chained, controlled by only three pins on the Arduino (which means a UNO or Nano can control them perfectly easily).

These are readily available on eBay (http://www.ebay.com/itm/MAX7219-Red-Dot-Matrix-Module-MCU-Control-Display-Module-DIY-Kit-For-Arduino-/400373211698) as modules to drive matrix displays.
(http://i.ebayimg.com/images/g/RA8AAOxyFiRRzS3N/s-l1600.jpg) (http://www.ebay.com/itm/MAX7219-Red-Dot-Matrix-Module-MCU-Control-Display-Module-DIY-Kit-For-Arduino-/400373211698)

The point is that even if you do not need the matrix itself, this gives you a PCB with the MAX7219 mounted and the necessary support components.  If you did want to use matrix displays, there is a much more properly designed module (http://www.ebay.com/itm/MAX7219-dot-matrix-module-Arduino-microcontroller-module-control-Display-DIY-KIT-/311419148750) available.
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: CrossRoads on Apr 15, 2016, 07:12 pm
You can daisychain (200/8 rounded up) = 25 shift registers.
Then have an array of 25 bytes (8 LEDs controlled per byte), and manipulate the data as you need, example:
Code: [Select]

byte dataArray[25];
// couple of  manipulation examples
dataArray[0] = 0b00001111; // define initial data
dataArray[0] = 0b11111101 & dataArray[0]; // clear bit 1, dataArray[0] is now 0b00001101
dataArray[1] = 0b11110000; // define initial data
dataArray[1] = 0b00000100; // set bit 2, dataArray[1] is now 0b11110100
// etc up thru dataArray[24]
//send the 25 bytes out to the  shift registers
digitalWrite (latchPin, LOW);
for (byte x = 0; x<25; x=x+1){
SPI.transfer(dataArray[x]); // or use shifOut(dataPin, clockPin, MSBFIRST, dataArray[x]);
}
digitalWrite (latchPin, HIGH); // all outputs update on this rising edge

However, that does need the 25 parts, assuming no multiplexing.
With MAX7219, it's like 8 shift registers in one, the outputs are multiplexed so 8 are on at time.
If you have 4 of them, and you give each its own chip select, then you can easily access just 1 register at a time:
Code: [Select]

digitalWrite (cs0Pin, LOW); // have cs0 to cs3
SPI.transfer(registerAddress); // 1 to 8 (not 0 to 7 in this case)
SPI.transfer(data Array[0]); // 0 to 7 for cs0, 8 to 15, for cs1, etc for 16-23, 24-31, and 32-39
digitalWrite (cs0Pin, HIGH);

with dataArray[ x ] manipulated as above, or pulled from memory, or read in from the serial port, or whatever.

If the LEDs are not wired in a matrix, but each individually, I offer a board that makes that easy to do, with separate pairs of pins for each LED:
http://www.crossroadsfencing.com/BobuinoRev17/ (http://www.crossroadsfencing.com/BobuinoRev17/)
(http://www.crossroadsfencing.com/BobuinoRev17/MAX7219breakout.jpg)
I've got a picture somewhere showing the LEDs wired to pairs of male pins (30 AWG wirewrap wire works well) that are stuck into the female headers.
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: CrossRoads on Apr 15, 2016, 07:14 pm
MAX7219 is nice because it just needs the 3 components, vs discrete current limit resistor for each shift register output.
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: CrossRoads on Apr 15, 2016, 07:26 pm
If you do go  shift register, be sure to use TPIC6C595, 100mA sink capability per IO, vs 70mA total for 74HC595.
http://www.digikey.com/product-detail/en/texas-instruments/TPIC6C595N/296-2020-5-ND/276260 (http://www.digikey.com/product-detail/en/texas-instruments/TPIC6C595N/296-2020-5-ND/276260)
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: Paul__B on Apr 16, 2016, 12:28 am
If you have 4 of them, and you give each its own chip select, then you can easily access just 1 register at a time:
Actually, you can reasonably easily access just 1 register at a time with them chained - that is a very clever part of the design of the MAX7219.
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: 15yearoldnoob on Apr 16, 2016, 12:56 am
Thanks everyone whos trying to help.
Im trying to read all I can...
but the programming is difficult to understand.

By using the pins on the Mega, all I have to do is Digital Write a Pin to high or Low.
But what I dont get is lets say I do use 25 shift registers with 200 leds.

i still cant comprehend how I will have control over anyone of them at one time.
If I want to turn pin 95 on, HIGH, then LOW, is it as easy as locating Register 12, using digital write high for that one, the going Low to shut it off?

Or do I have to read up and first understand arrays, then this that and everything else just to make one LED blink.

Somehow Im thinking in terms of specific pin numbers, and the whole concept of shift registers just isnt sinking in and is actually confusing me more than anything.

Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: Wawa on Apr 16, 2016, 03:47 am
Imagine a shift register as a long row of people sitting an exam.
You want to hand out exam papers to them, but you want the last three to get a different one.
What do you do.

You give the first person the envelope, and ask him/her to pass it on to the next one.
Give the envelope >> tell to pass it on >> give the next envelope >> pass it on >> data >> clock >> data >> clock >> etc.
You have made sure you have given the three different exam papers first.
If you had the correct amount of exam papers, then the three different ones would have arrived at the right person, and the end of the row.
The last command you give to all students is "open the exam papers NOW" (latch pulse).

The row can be as long as you want.
74HC595 chips have 8 outputs.
The 'shiftOut' command that does all the passing on for you works in bytes (8 bits).
If you chain 25 chips, you have to send 25 bytes, every time.
Leo..
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: Grumpy_Mike on Apr 16, 2016, 04:36 am
Quote
If I want to turn pin 95 on, HIGH, then LOW, is it as easy as locating Register 12, using digital write high for that one, the going Low to shut it off?
Yes if by Register 12 you mean a variable you are going to send to that shift register and you write high in the bit that corresponds to 95 mod 8.

mod is the remainder of the division so 95 mod 8 is 7, so you change bit 7 in the variable that ends up being sent to shift register 12. Changing only that bit ensures that only that pin changes.

As stated before you have to send all the variables in turn so that they end up in the right place. A lot depends on how you number these. You are free to number them as you like but some numbering systems are a lot easier than others. For example the first byte you send ends up going to the last shift register in the chain. So if you call that byte0 then it makes sense to call the last shift register "register0". We always start numbering from zero by the way.

Quote
Or do I have to read up and first understand arrays
That would help a lot because the code would be way way simpler to write.

Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: 15yearoldnoob on Apr 17, 2016, 09:36 am
Thanks WAWA and Grumpy - its starting to sink in some.
I have tried to read upon Arrays and Im guessing will have to start reading on variables too.

I still cant comprehend all the programming that I see when I open the examples of arrays.
but if Im recieving this all right, is it safe to say that if I do use 25 shift registers...then Im probably going to have an extremely long code,
but that I could use something equivalent to....

all LEDS turn on except last shift register, than my programs going to look like
digital write (mod 0-23, HIGH;)

Then if I want to turn those off, and have the last 3 turn on, it would go something like
DigitalWrite (mod 0-23,Low); DigitalWrite (mod 24, High)

(I know thats not the right syntax, but I dont know the actual commands just yet,.
But is it safe to say, thats how it will end up...

And if say I want to turn on the infamous #95 LED on Shift Register 12
Would the programing go
DigitialWrite (mod12, Led4, HIGH)

Because I dont think I have seen a program I understand that actually isolates one pin in a vast grouping of pins that uses shift registers.
Its all been like a matrix that has a flow to it, and thats not really what I want to do where everything is like a wave.

Simply, I just want to be able to turn on any LED at will.
Not using them all, but #92 turn on.
number #102 turn on when #92 turns off
kinda thing.

Thanks so much for the help so far.
Hopefully it will sink in one day soon
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: Paul__B on Apr 17, 2016, 01:09 pm
I have tried to read upon Arrays and I'm guessing will have to start reading on variables too.
And almost certainly, many more things also.

is it safe to say that if I do use 25 shift registers...then I'm probably going to have an extremely long code,
Not really.  You use arrays, loops and functions, anywhere there is repetition.

all LEDs turn on except last shift register, than my programs going to look like
digital write (mod 0-23, HIGH;)

Then if I want to turn those off, and have the last 3 turn on, it would go something like
DigitalWrite (mod 0-23,Low); DigitalWrite (mod 24, High)
No.

Because I don't think I have seen a program I understand that actually isolates one pin in a vast grouping of pins that uses shift registers.
Its all been like a matrix that has a flow to it, and that's not really what I want to do where everything is like a wave.
Don't know about the matrix and wave business, but there are programs that do just this, it's just that it is difficult to find the correct search terms.

Simply, I just want to be able to turn on any LED at will.
Not using them all, but #92 turn on.
number #102 turn on when #92 turns off
kinda thing.
I think you need to make a habit of reading previous answers.

I explained this to you in another post (http://forum.arduino.cc/index.php?topic=393765.msg2713162#msg2713162).

To use shift registers - and the MAX7219 is the proper way to do it in this case; four chips makes vastly more sense than 25 - you have a copy or image of the LEDs in an array.  To change a given LED, you calculate where it is in the array - led number 102 is the sixth bit in the 13th byte of the array - and switch that on or off.  You then (after making all the changes necessary for various LEDs) call the single function that reads the whole array and writes it to the shift registers.  When it finally issues the latch signal to the registers, all the LEDs adopt the new pattern with whatever changes you have made in the array.

{In fact, with the MAX7219, it is possible to change only the 13th register byte, but it is not sufficiently quicker to make it worth the bother.}
Title: Re: Can someone help with shift register - i need more outputs but dont understand
Post by: Grumpy_Mike on Apr 17, 2016, 01:16 pm
You will end up writing a function that takes in a bit number and changes it. This function will be very short and will be used each time you want to change any LED.
Then you will write another function to set all the LEDs to off, again it is short.
By using for loops you will then make what ever pattern you can think of.

There is no reason why your code will be long at all.