I was a maestro of IBM 370/390 assembler. You are a maestro of at least Arduino coding. So maybe also C and others. Anyway, I appreciate your coding. Even with the quick hacks you write for these little demos, the code is still tight without being so tight to be incomprehensible like some C/C++ coders like to do. You show off just the right amount. It is easy to understand and "elegant" for demo level programs. I have seen MUCH worse. Thank you for taking the time to do these demos. I understand the electronics and have for decades but your integration with programming is enlightening at the next level of HW/SW integration. Well done.
I'm not an electronic engineer but your explanation was pretty clear. Now i'm ready to solve a problem that seemed very complicated to me before watching this great video. Thanks!
The while loop at the end of the ISR should not be necessary, if the IRS is only called on the rising edge. And if you did want to improve your code(or make it shorter) you could also replace the whole check variable by replacing it with "1
my code was to simply toggle the LED on or off, but if you just need to read the value in as being just on or OFF, that is simple, since a double tap wouldn't affect you. Like the ISR being called would not flip your output
Methinks an array or two would help to clean up the code. You could remove the while loop at the end and repeat the pin read function without actually writing to the leds. Run pin_read, get an array for which buttons are pressed, compare it to the array for the buttons which were previously pressed. From that you can find which buttons have gone from 0 to 1. If nothing's changed, there's no difference, so pin_read can just keep looping while the input is high.
I am 2 minutes in and I already know I am going to subscribe. This guy is soooo good. Like butter. Thank you Kevin. Your presentation is just flawless and perfect. I cannot wait to watch many of your videos. Thank you so much. I am fan.
Good button technique but all those delays in an ISR is very bad practice. You should only record the state of the buttons into variables and let the main loop process debounce and determine if the buttons have been pressed. Another way is to check a clock state and determine if the button has been pressed long enough in the ISR and set a state. The objective is to manage the button state as quickly as possible in the ISR and return to the main loop (rest of the code).
Agreed, but the author was saying he had not prepared himself to the video, and he appologized to us. As for ISR, indeed the 'routine' (not function) should only set some flag to true/false, and that is all. Hardware interrupts work aside normal program execution. MC/CPU stops normal program execution for checking all that interupt stater and running ISRs. And I think unless the current ISR() routine finishes it halts executing next ISRs, thus stops running the code and other interrupts (timers?). I think, such behavior prevents also from multiple execution of the same ISR over, and over again berore the first one finishes. (we would experience interrupts interrupted by interrupts - endless loop). The loop function should do the job. Anyway great lecture, thank the author. Have a nice day.
You are right, but purpose of THIS video was not ISR, but the button/diode/pin 2 trick, no more. So the code is just as demo, how it is possible to read that, not how it should be done in "real" program and even waiting in ISR does not matter, as there is no other code to execute, nor other interrupt to take care of.
@@DupczacyBawol As pointed out, yea... this has nothing to do with ISR or anything like that. And even if he apologized for not being prepared to make a video... if you haven't prepared, why make the video? Lol. EVEN then, again, being prepared for a video has nothing to do with implementation. I could set up 20 servers for a business, but then go 'I wasn't prepared to give an orientation on how to use these servers', and yet that orientation would have NOTHING to do with me actually installing an OS onto the hardware and installing the necessary drivers/software...
Could you please post schematic of this circuit on your website. I understand shift registers pretty well but I'm a bit confused how you wired this project up.
Very efficient way to sense the actuation of any one or more of a number of buttons. Great. You are basically setting each button on a single bit then polling all the bits in the byte to see which one are on or off.
first he says he's going to assume we understand how shift registers work and then he says "I'm sure if you guys are familiar with diodes or not" haha I appreciate you explaining everything
hmmm, maybe? I think there might be a way to modify what I have here for something like that, but this is interrupt driven, so if you only care about a reading above a threshold then yea, I think this coudl work
I was going to ask why you didn't use a regular parallel in - serial out shiftregister but since multiple people already asked it I'm not going to ask it again.) It's actually a pretty neat technique that I've never seen before so thank you for explaining it. Using outputs as inputs if that's not thinking outside of the box, I don't know what is. Great video, as always.
Aren't LEDs horrible diodes? I have no facts to back it up, but I've heard that LEDs were made to emit light, and not to block the current. But if it works for testing purposes then I don't really see a problem either xD
Awesome tutorial, any chance i can get a schematic.. been at it for a week and i just cant get it to work. I am from South Africa, not sure where i am going wrong... difference in IC maybe... PS i am still a newbie, what is the best way of trouble shooting where i am going wrong?
So it's basically a ghetto MUX, which is pretty cool if you don't have an 8-to-1 MUX lying around, but the really interesting thing about this is that since you're strobing the source voltage across the pins, it should actually work with analog inputs as well as pushbuttons. When you do the strobing pass, the pulldown resistor will reduce the voltage across the inactive pins to 0, so you can theoretically get the voltage across only one input at a time. You may need to do some resistor math to figure it out exactly. It's been a few years since I had to calculate circuit diagrams, but could be especially useful if you have a limited number of ADC pins available.
Back to say: I just tried this, and it actually works way better than I expected, but not exactly how I described 2 months ago. Your analog inputs each have one reference pin tied to VCC or ground, depending on whether you want them to be active-high or active-low. (VCC for active-low, VSS for active-high) The other reference pin is tied to the shift register, and is strobed through just like in this example (or the later one where he switched from ISR to polling) Analog values are connected to a single analog pin via a diode. If the circuit is set up in Active-High mode, the diodes should be pointing *from the analog sensor into the MCU analog input pin.* In Active-Low mode, point them the other direction. The analog pin will read whatever the highest/lowest (depending on mode) value among all of the analog sensors is. By setting the shift register pin to inactive mode (low for active-high, high for active-low), you effectively remove that sensor from the circuit. Set only one sensor active at a time and strobe through them to read 8 analog values on a single input pin.
Hi Kevin, I'm trying to make a small simple midi keyboard with 25keys (nothing fancy, just digital switches reading on and offs and sending the data to the computer) . I just need it to be polyphonic upto 10keys. I.e it should be able to detect (on or off) for 10 keys at them same time. And it should be very responsive in reading it. Are multiplexers okay or shift registers? Which shift register do I need need. And i saw you pressing multiple buttons at once. How many can actually be pressed at the same time and still work in your setup.
i was making 5*5*5 led cube using 595 shift register and bc 547 transistor. it worked in starting and is not working properly. i have not attached any resistor in the base emitter of transistor and 220 ohms in the collector emitter .CAN THIS BE A PROBLEM. or can you guess...
u can use data lactches for same porpose. 74hc374 no code needed. just use external data read command 74138 demultiplexer will also help u for more inputs
I think. If using 74165 then no need to use 74595 anymore. It is possible to connect the push button directly to the 74165 to reduce complexity. The rest is just programming.
That is pretty neat. However for a thousand inputs it would probably be a good idea to be a bit more efficiant: You could make this even faster by divide and conquer instead of a linear search. For 1024 Inputs you only would need 10 checking cycles compared to 1024 in the linear search.
I am just getting into Arduino, but have experience with programming. I was just wondering about your code a bit. I was thinking that it may be better to make pen_read set a global boolean, then in your loop you would do basically what you are doing in pen_read right now. loop would be while(pressed) { *current_pen_read_code*; pressed = false; }; Like I said, I am new, and don't have the parts to test this yet. Can you think of why that would not work, or why that would be less effective?
Not sure if it works in the Arduino IDE, but just a suggestion for the future. In the for loop, instead of having the "Check" variable shifted and then run through the loop again, the parameters of the loop could be: for( Check = 1, Check >0, Check
Hmmm.. byte becoming -1, I thought bytes was unsigned number between 0..255 with values at every bit (seat) as follows.. 1 2 4 8 16 32 64 128. No minus for bytes.. at least in the language Pascal I use most of the time. Left shifting just shift value of bits to higher places, that is, race the value to power of two. You could also easily use 8 bit array values of bytes. When pushing two or more buttons, two or more hardware interrupts will occur. This "dirty" technics he is showing by overloading interrupt function in endless loop is very scary and against every thing I would even consider. Kind of challenge to optimize this, one idea is to save the state of prior interrupt's and use binary search, test first by using byte value of 15 (4 least bits signed) if no signal, then you know it is one of them who gives you 240, halve that and 3 readings is enough in worst scenario etc. and of cause take prior state of buttons in to those calculations with masking.
Brilliant! Provision for debounce, (frequently not the case with shift register videos). Plus SPI library for speed. Lots of lateral thinking here. Earned you a subscription
Cool trick! I was thinking of letting the last output shift everything back to the micro controller. As in insted of connecting another shift register in series, I would connect it back to the arduino in this case. There I would shift the bits into a byte and after the 8 clocks I would have the output byte back and could mask for the correct buttonpress. I figured it would maybe work since it shifts out from there. But maybe your loop is better hmm I'll try and see. This gave me inspiration! Thanx
Hi Kevin ! I'm working on a project using your setup (actually, using the "piano" version as it's exactly what I'm trying to achieve), and I can't figure out something. I don't get the usage and difference between the data and what you call "Input from the buttons" (pin 2). What exactly is this pin 2 doing and how to connect it to my buttons ? We can't really see the board or schematics in your video :/ Thanks !
Yes, this is an old thread, but still very, very useful. The technique to find out which buttons are pressed is super! And this, with a 595 which is supposed to drive something, not to (indirectly) detect if something is changed/pressed... Very clever indeed... Problem is that I don't know which Arduino has been used for this demo (if an Arduino has been used at all) but I struggled with the SPI communication because with the Arduino Uno, the clock and data pins are switched. In my case, the clock pin is Arduino pin 13 (and not 11 as per the tutorial) and the MOSI pin is Arduino pin 11 (and not 13 as per the tutorial). In the video where the code is explained (e.g. time 12.14) it's mentioned otherwise. Unless the comment written next to the pins is wrong, in which case I step back silently... Can this be confirmed? Best, --Geert
Hey I appreciate the video, but it would be very helpful to beginners like myself to have a schematic. Is there a wiring diagram that you could share with us??
Hi, Very useful information. I wasn't needing thousands but I am going to be making a control panel for a spaceship game I play to make it more interesting to play than just keyboard. Going to use different switches and types of lights. Was probably needing about 20 inputs and more outputs. Was going to add extra just to give it more of a spaceship feel too :) I thought I was going to have to use analog multiplexers for input but I guess I can just use the shift registers now.
Isn't this simplier with the CD4021 chip instead of the 74HC595 chip? I'm a novice so I really don't know, but it does seem like one chip is made to shift out (e.g., to a bunch of LEDs) and another to shift in (e.g., many buttons) On the other hand most arduino kits come with a 74HC595 and not a CD4021 so maybe I'm missing something.
To use a CD4021 (or other parallel to serial shift register) you have to constantly read in the register to know whether anything changed. With the '595 you just turn on all the outputs and you forget about it until an interrupt occurs. When an interrupt occurs then scan to see which one(s) caused the interrupt.
Hello Mr.Darrah , Kevin , First of all , congratulations for you excellent job. That being said , I wonder if you can post a tutorial with the wiring diagram of the circuit, because I tried several times and could not make it work. Thank you in advance. Wagner Hiroshi
+Timo Benkhard - Arduino pins configured for input are in high-impedance mode, and react to very small amounts of current, so they can pick up noise and appear to change state when they're not connected to anything. They have built-in pull-up resistors (that you can enable via software), but no pull-down resistors, so if you want the pin to always appear low when there's nothing connected (as in this case), you need a resistor between that pin and ground. 10k is a typical value (low enough to ensure the pin gets set to low when it doesn't have a signal, but high enough to make sure the pin goes to high when signal arrives). Check the "DigitalPins" page on the Arduino Tutorial website for more details.
@RFC3514 - I don't think it has something to do with the Arduino inputs directly. If the 10k resistor is not present and you put an output of the shift register IC to 1, you're making a short circuit between that output and ground (apart from the diode which is in between, but this one will conduct). I'm afraid this won't be healthy for the shift register device because I don't think there's protection inside the chip itself...
Erhm honestly thanks for showing that circuit with the analogRead at the start. That'll actually do for my project since it's only 4 buttons I'm using and they shouldn't/can't be pressed at the same time in my interface. I'll definitely start playing with shift registers in the future though when time is a less pressing issue :P
Great Video ! I want to use a Rotary Encoder and need the Interrupt for this one. But I could use any PinChangeInterrrupt for checking any pressed button, right ? Greetings from Austria !
@kevin darrah sir im asking for help do you have a schematic diagram of 4 or 5 or more inputs interlocking circuits...for example i have a four inputs push button 1 to 4 and output LED 1 to 4 the sequence is like this..if i press push button 4 the LED4 will turn on then when i press push button 1 the LED 4 will turn off then the LED 1 will turn on then when i press push button 3 the LED 1 will turn off then LED 3 will turn on and so on..it always one LED will acivate on OUTPUT...please...thanks in advance
Hi Kevin, I am trying to figure out how to make simple led selection indicator, so I want 8 LED to be shifted in an infinite loop by a press of a button. So one press next LED is lit, another press next to last LED is lit and so on in circle. I hope you understand what I want to do. Please help, thank you :)
Hi. I have a working circuit which has 4 digit 7 segment led display. I want to to read the Data and make my arduino send Smae numbers to my serial monitor tgat what my segment display shows simultaneously. What u wld advice? And umm this segment display has total 12 inputs which 8 for leds and 4 for shifting from eacg 1 number block cathodes(or anodes). Any advice?
Just wondering if you could offer suggestions as to why this code works on an UNO but not on a mega2650? I've tried changing the pins in the code to the appropriate MOSI and SCK pins on the mega but still no luck. Any help appreciated
I am relativity new to programming shift registers. How could I modify this code to use only 1 register for inputs and then do something like print "high" to the serial monitor?
Learn It's not necessarily an error, but he has to be sure the output was low before he makes it high. But you're right, reversing the order (first low, then high) would be better.
How would you apply the same principle for an analog signal ? The Arduino has only 6 analog inputs, what could we use to extend that to say 20+ analog inputs?
Basically the register connected to the buttons just send one bit per cycle to identify if the actual checked button is pressed!? This a is a amazing idea.
Can a automatic style casino keno game be made up with Arduino or some micro controller. It would show game number as well as drawing 20 random numbers out of 80 leds.
As any arduino board all pins can be used as digitalRead. My arduino Pro micro board has 18 pins for use as digitalRead. I Need to 42 pins for input to my arduino board(for different outputs) . Is it possible using 74HC595 shift register????? . If yes, please help me with arduino sketch & wiring diagram arduino board to 74HC595 and push buttons,,,,,,,
Is there a reason why I can't get my led circuit, the circuit is from a double ended light saber thing that have I think 6 leds on one circuit that has one + cable for each and one - cable for all of them.
I'm thinking about it. if you control the LED via 74HC595, you can control about 4x4 LED's with every shift register. But if you use it as an input, it's almost 8 at a time. That means if you have a lot of inputs, like 200, you will also need a ton of registers, like 25 for 200. I feel that there will be problems if the inputs are grouped in matrix, any idea?
You could possibly make a matrix input but I don't know if you could maintain the interrupt driven feature of this scheme. Therefore you would have to constantly scan the matrix. How about this: Use the '595 to drive 8 rows high. Use a parallel to serial shift register to read the columns, and constantly clock it with a PWM output. Don't actually read the input, just let it wait for an interrupt. When any button is pressed the input will go high within 8 cycles of the PWM. Then read the input shift register to see which column is on, then scan the '595 to see which row is on.
Sorry.. am only a beginner.. I got little confused.. I heard 74hc595 is an output shift register (Serial in parallel out) and 74hc165 is actually an input shift register.. But here (05:40) you are mentioning it(74HC595) connected with push buttons.. Little confused
you output 1 sequencially to each button and read the common part (pin2) to see, if there is 1, so if the respective button is pressed. Input is pin 2, the output register 165 shifts the "voice" to particular buttons.
I love this project, really, but it would be incredible if we had schematics. In the end, becomes a very dark and cloudy tutorial. How to work the outputs, I get loads of info, but I'm having trouble knowing where to connect the pin 2 to the shift register as said in the code. Actually, I can't find any documentation of using 74HC595 as an input expander. Only with other shift registers like 74hc165. I see 74HC595 as an output expander.
I don't know about mechanical switches though, rather hard to use code to debounce using switches comomg through shift registers, can be done though, resposiveness may be a trade off that needs to be made. cheers
could you please show me a schematic of how i would connect 8 switches(input) to my 74HC165 to Arduino and then to my 74HC595 to my 8 LEDs(output)?When a correspong switch is off, then a corresponding LED should go on
Nice walkthrough. On the software side you should be able to replace your bitRead()/bitWrite() with just Output ^= Check; Someone's probably already brought it up but I have no desire to fight youtube's comment system long enough to make sure...
Hi there, Has anybody got any tips/ advice for a relative beginner in electronics? I've got some basic knowledge about basic components(leds, resistors, caps) and the picaxe system. But I was wondering if there are any good websites or other resources that could help me broaden my knowledge/ understanding and work with more complex things with more integrated circuits driving a wider variety of outputs. Thanks.
If I want to use 4 total shift registers, 2 for buttons, 2 for lights, do I just wire them all up together from the same clock/data/latch pin? And what changes in the code when you add more registers?
Yes, you just chain them as many as you want (and even may play with the order) and in the code you need more "Output" variables (array?) and loop the "Change" over all inputing chips. Also note, that you can use part of chip for LEDs and part for buttons, if you wish so. Just little complicated code, but not too much. The idea is still the same - cycle 1 over buttons, detect, what is pressed, the restore outputs to proper state and wait for another interrupt.
@@Gilhad1 please sir could you write the code for us? i'm desperate for searching and i need only 16 input and 16 output just with the same i dea of Kevin
yes, that is a very cool method. You can roll that same technique into what I'm showing you. Hmmm, that's funny he also discovered the diode issue... guess it is pretty obvious
how many shift registers I need to control 25 key keyboard ? what is scan matrix? with 9*3 scan matrix can I control all 25 keys . how to set up that? pls advise
4 registers using this idea verbatim. to read matrix you need different aproach. Depends on many factors, what is the best, I woul use one mcp23017 over I2C for matrix up to 16 (rows+cols total)
The first switch scheme is flawed because it assumes that all the switches and wiring will remain pristine, but what happens when the switch contacts get tarnished and slightly resistive? It works good when the equipment is new, but what happens in a couple/few years? The shift register button scanning scheme, as you explained it, starts out with all 1s then upon getting the interrupt it shifts a 1 through the chain. You would have to continue shifting that 1 until none of them came back high, at which point you could then re-enable the interrupt, otherwise you would miss the successive switch closures because they wouldn't be generating an interrupt. Since you are stuck using a '595 anyway, just use a PISO shift register like a 4014- same number of pins, none more of the headache. Yeah I know this is old.
I was a maestro of IBM 370/390 assembler. You are a maestro of at least Arduino coding. So maybe also C and others. Anyway, I appreciate your coding. Even with the quick hacks you write for these little demos, the code is still tight without being so tight to be incomprehensible like some C/C++ coders like to do. You show off just the right amount. It is easy to understand and "elegant" for demo level programs. I have seen MUCH worse. Thank you for taking the time to do these demos. I understand the electronics and have for decades but your integration with programming is enlightening at the next level of HW/SW integration. Well done.
I'm not an electronic engineer but your explanation was pretty clear. Now i'm ready to solve a problem that seemed very complicated to me before watching this great video. Thanks!
yes, you have a good point, and I just added an annotation, but this does allow you to mix inputs and outputs in the same shift register bank
The while loop at the end of the ISR should not be necessary, if the IRS is only called on the rising edge. And if you did want to improve your code(or make it shorter) you could also replace the whole check variable by replacing it with "1
my code was to simply toggle the LED on or off, but if you just need to read the value in as being just on or OFF, that is simple, since a double tap wouldn't affect you. Like the ISR being called would not flip your output
Methinks an array or two would help to clean up the code.
You could remove the while loop at the end and repeat the pin read function without actually writing to the leds. Run pin_read, get an array for which buttons are pressed, compare it to the array for the buttons which were previously pressed. From that you can find which buttons have gone from 0 to 1. If nothing's changed, there's no difference, so pin_read can just keep looping while the input is high.
I am 2 minutes in and I already know I am going to subscribe. This guy is soooo good. Like butter. Thank you Kevin. Your presentation is just flawless and perfect. I cannot wait to watch many of your videos. Thank you so much. I am fan.
Good button technique but all those delays in an ISR is very bad practice. You should only record the state of the buttons into variables and let the main loop process debounce and determine if the buttons have been pressed. Another way is to check a clock state and determine if the button has been pressed long enough in the ISR and set a state. The objective is to manage the button state as quickly as possible in the ISR and return to the main loop (rest of the code).
Agreed, but the author was saying he had not prepared himself to the video, and he appologized to us. As for ISR, indeed the 'routine' (not function) should only set some flag to true/false, and that is all. Hardware interrupts work aside normal program execution. MC/CPU stops normal program execution for checking all that interupt stater and running ISRs. And I think unless the current ISR() routine finishes it halts executing next ISRs, thus stops running the code and other interrupts (timers?). I think, such behavior prevents also from multiple execution of the same ISR over, and over again berore the first one finishes. (we would experience interrupts interrupted by interrupts - endless loop). The loop function should do the job. Anyway great lecture, thank the author. Have a nice day.
You are right, but purpose of THIS video was not ISR, but the button/diode/pin 2 trick, no more. So the code is just as demo, how it is possible to read that, not how it should be done in "real" program and even waiting in ISR does not matter, as there is no other code to execute, nor other interrupt to take care of.
@@DupczacyBawol As pointed out, yea... this has nothing to do with ISR or anything like that. And even if he apologized for not being prepared to make a video... if you haven't prepared, why make the video? Lol. EVEN then, again, being prepared for a video has nothing to do with implementation. I could set up 20 servers for a business, but then go 'I wasn't prepared to give an orientation on how to use these servers', and yet that orientation would have NOTHING to do with me actually installing an OS onto the hardware and installing the necessary drivers/software...
Could you please post schematic of this circuit on your website. I understand shift registers pretty well but I'm a bit confused how you wired this project up.
+Kyle Leser Did you manage to make the wiring?
@@Leilektsoglou Did you manage to make the wiring?
@@ianthehunter3532 Did you manage to make the wiring?
@@jacobstrous8929 no, gonna use io extenders
Very efficient way to sense the actuation of any one or more of a number of buttons. Great. You are basically setting each button on a single bit then polling all the bits in the byte to see which one are on or off.
As you had as a baloon at the start, a parallel in serial out is the way ti go for input. 3 pins, clock, load & data.
cheers
I was about to post the same question. The 74HC595 is a Serial to Serial/Parallel IC, I'd use a Parallel to Serial IC with internal pull-up resistors.
Cheers Kevin, you did a better video explaining this technique than in possibly could.
yep this will work for you... sounds like an awesome project! You may want to review the other comments below to see what others have suggested
*Very nice, I actually under stand shift registers a lot better now!! Specifically the 74HC595!! Thanks!!!*
first he says he's going to assume we understand how shift registers work and then he says "I'm sure if you guys are familiar with diodes or not" haha I appreciate you explaining everything
hmmm, maybe? I think there might be a way to modify what I have here for something like that, but this is interrupt driven, so if you only care about a reading above a threshold then yea, I think this coudl work
I was going to ask why you didn't use a regular parallel in - serial out shiftregister but since multiple people already asked it I'm not going to ask it again.)
It's actually a pretty neat technique that I've never seen before so thank you for explaining it.
Using outputs as inputs if that's not thinking outside of the box, I don't know what is.
Great video, as always.
yea, I know... I need to do that. Many people have suggested the same. Just been really busy lately
This is really great. Thanks. It would be good to see how you wired everything up when you do these.
The diodes are required if anyone is wondering :) I didn't have any on hand so I just used some LEDs! for testing purposes, anyways.
Aren't LEDs horrible diodes? I have no facts to back it up, but I've heard that LEDs were made to emit light, and not to block the current. But if it works for testing purposes then I don't really see a problem either xD
Awesome tutorial, any chance i can get a schematic.. been at it for a week and i just cant get it to work. I am from South Africa, not sure where i am going wrong... difference in IC maybe... PS i am still a newbie, what is the best way of trouble shooting where i am going wrong?
So it's basically a ghetto MUX, which is pretty cool if you don't have an 8-to-1 MUX lying around, but the really interesting thing about this is that since you're strobing the source voltage across the pins, it should actually work with analog inputs as well as pushbuttons. When you do the strobing pass, the pulldown resistor will reduce the voltage across the inactive pins to 0, so you can theoretically get the voltage across only one input at a time.
You may need to do some resistor math to figure it out exactly. It's been a few years since I had to calculate circuit diagrams, but could be especially useful if you have a limited number of ADC pins available.
Back to say: I just tried this, and it actually works way better than I expected, but not exactly how I described 2 months ago.
Your analog inputs each have one reference pin tied to VCC or ground, depending on whether you want them to be active-high or active-low. (VCC for active-low, VSS for active-high)
The other reference pin is tied to the shift register, and is strobed through just like in this example (or the later one where he switched from ISR to polling)
Analog values are connected to a single analog pin via a diode. If the circuit is set up in Active-High mode, the diodes should be pointing *from the analog sensor into the MCU analog input pin.* In Active-Low mode, point them the other direction.
The analog pin will read whatever the highest/lowest (depending on mode) value among all of the analog sensors is. By setting the shift register pin to inactive mode (low for active-high, high for active-low), you effectively remove that sensor from the circuit. Set only one sensor active at a time and strobe through them to read 8 analog values on a single input pin.
Hi Kevin, I'm trying to make a small simple midi keyboard with 25keys (nothing fancy, just digital switches reading on and offs and sending the data to the computer) . I just need it to be polyphonic upto 10keys. I.e it should be able to detect (on or off) for 10 keys at them same time. And it should be very responsive in reading it. Are multiplexers okay or shift registers? Which shift register do I need need. And i saw you pressing multiple buttons at once. How many can actually be pressed at the same time and still work in your setup.
Hey @10magicalfingers27
I'm curious to know if you got a solution for your project. Did it work? And do you mind sharing your thoughts?
i was making 5*5*5 led cube using 595 shift register and bc 547 transistor. it worked in starting and is not working properly. i have not attached any resistor in the base emitter of transistor and 220 ohms in the collector emitter .CAN THIS BE A PROBLEM. or can you guess...
hi, are you using two 74HC595 for both controlling inputs and outputs? and is it possible to have the circuit schematic of this tutorial?
u can use data lactches for same porpose.
74hc374
no code needed.
just use external data read command
74138 demultiplexer will also help u for more inputs
yea they are, but no prob since we are debouncing in software. A simple delay is all that's needed
I think u can also use a 74165 parallel in serial out that has its output pin hooked to DS of the 74595 to achieve this
I think. If using 74165 then no need to use 74595 anymore. It is possible to connect the push button directly to the 74165 to reduce complexity. The rest is just programming.
Perfect. I was looking for a way to use BCD switches as inputs to Arduino and you have shown me the way.
Only 3 years late on commenting but a bitwise XOR (^) with a 1 would be a better way to toggle the bit, rather than the if else.
Only 3 years late but I don't have anything to say
@z3 Respectively to the comment it is 3 years
Respectfully it's never to late to learn a better approach
@@simonmasters3295 Is it possible to help me with the wiring?
I really wish I knew how this would look
That is pretty neat.
However for a thousand inputs it would probably be a good idea to be a bit more efficiant: You could make this even faster by divide and conquer instead of a linear search. For 1024 Inputs you only would need 10 checking cycles compared to 1024 in the linear search.
I am just getting into Arduino, but have experience with programming.
I was just wondering about your code a bit.
I was thinking that it may be better to make pen_read set a global boolean, then in your loop you would do basically what you are doing in pen_read right now.
loop would be while(pressed) { *current_pen_read_code*; pressed = false; };
Like I said, I am new, and don't have the parts to test this yet. Can you think of why that would not work, or why that would be less effective?
Not sure if it works in the Arduino IDE, but just a suggestion for the future. In the for loop, instead of having the "Check" variable shifted and then run through the loop again, the parameters of the loop could be:
for( Check = 1, Check >0, Check
Hmmm.. byte becoming -1, I thought bytes was unsigned number between 0..255 with values at every bit (seat) as follows.. 1 2 4 8 16 32 64 128. No minus for bytes.. at least in the language Pascal I use most of the time.
Left shifting just shift value of bits to higher places, that is, race the value to power of two. You could also easily use 8 bit array values of bytes.
When pushing two or more buttons, two or more hardware interrupts will occur. This "dirty" technics he is showing by overloading interrupt function in endless loop is very scary and against every thing I would even consider.
Kind of challenge to optimize this, one idea is to save the state of prior interrupt's and use binary search, test first by using byte value of 15 (4 least bits signed) if no signal, then you know it is one of them who gives you 240, halve that and 3 readings is enough in worst scenario etc. and of cause take prior state of buttons in to those calculations with masking.
@@bjarnieinarsson3472 wait, wut?
By "-1" I think the conditional tests true in that instance
And "Endless loop"? Where?
Good explanation of good design. Cinematography should be front lighting to remove glare from back light on desktop.
Brilliant! Provision for debounce, (frequently not the case with shift register videos). Plus SPI library for speed.
Lots of lateral thinking here. Earned you a subscription
Thanks alot for the explanation. I was wondering if you have the part numbers for the diodes you used?
Cool trick! I was thinking of letting the last output shift everything back to the micro controller. As in insted of connecting another shift register in series, I would connect it back to the arduino in this case. There I would shift the bits into a byte and after the 8 clocks I would have the output byte back and could mask for the correct buttonpress. I figured it would maybe work since it shifts out from there. But maybe your loop is better hmm I'll try and see. This gave me inspiration! Thanx
Hi Kevin ! I'm working on a project using your setup (actually, using the "piano" version as it's exactly what I'm trying to achieve), and I can't figure out something. I don't get the usage and difference between the data and what you call "Input from the buttons" (pin 2). What exactly is this pin 2 doing and how to connect it to my buttons ? We can't really see the board or schematics in your video :/
Thanks !
Yes, this is an old thread, but still very, very useful. The technique to find out which buttons are pressed is super! And this, with a 595 which is supposed to drive something, not to (indirectly) detect if something is changed/pressed... Very clever indeed...
Problem is that I don't know which Arduino has been used for this demo (if an Arduino has been used at all) but I struggled with the SPI communication because with the Arduino Uno, the clock and data pins are switched. In my case, the clock pin is Arduino pin 13 (and not 11 as per the tutorial) and the MOSI pin is Arduino pin 11 (and not 13 as per the tutorial). In the video where the code is explained (e.g. time 12.14) it's mentioned otherwise. Unless the comment written next to the pins is wrong, in which case I step back silently...
Can this be confirmed?
Best,
--Geert
Hey I appreciate the video, but it would be very helpful to beginners like myself to have a schematic. Is there a wiring diagram that you could share with us??
The buttons you are using here, are they not giving you any bounceproblems?, if so, what buttons are they?
Hi,
Very useful information. I wasn't needing thousands but I am going to be making a control panel for a spaceship game I play to make it more interesting to play than just keyboard. Going to use different switches and types of lights. Was probably needing about 20 inputs and more outputs. Was going to add extra just to give it more of a spaceship feel too :) I thought I was going to have to use analog multiplexers for input but I guess I can just use the shift registers now.
Isn't this simplier with the CD4021 chip instead of the 74HC595 chip?
I'm a novice so I really don't know, but it does seem like one chip is made to shift out (e.g., to a bunch of LEDs) and another to shift in (e.g., many buttons)
On the other hand most arduino kits come with a 74HC595 and not a CD4021 so maybe I'm missing something.
To use a CD4021 (or other parallel to serial shift register) you have to constantly read in the register to know whether anything changed. With the '595 you just turn on all the outputs and you forget about it until an interrupt occurs. When an interrupt occurs then scan to see which one(s) caused the interrupt.
Hello Mr.Darrah , Kevin ,
First of all , congratulations for you excellent job. That being said , I wonder if you can post a tutorial with the wiring diagram of the circuit, because I tried several times and could not make it work.
Thank you in advance.
Wagner Hiroshi
I am confused as to why you are using the 10k resistor, im quite new to this so I hope its not a stupid question. Your explanation was very clear!
+Timo Benkhard - Arduino pins configured for input are in high-impedance mode, and react to very small amounts of current, so they can pick up noise and appear to change state when they're not connected to anything. They have built-in pull-up resistors (that you can enable via software), but no pull-down resistors, so if you want the pin to always appear low when there's nothing connected (as in this case), you need a resistor between that pin and ground. 10k is a typical value (low enough to ensure the pin gets set to low when it doesn't have a signal, but high enough to make sure the pin goes to high when signal arrives).
Check the "DigitalPins" page on the Arduino Tutorial website for more details.
@RFC3514 - I don't think it has something to do with the Arduino inputs directly. If the 10k resistor is not present and you put an output of the shift register IC to 1, you're making a short circuit between that output and ground (apart from the diode which is in between, but this one will conduct).
I'm afraid this won't be healthy for the shift register device because I don't think there's protection inside the chip itself...
Geert Vancompernolle - Just try it, and you'll see. Or read the page I mentioned above; the Arduino manual makes it pretty clear.
So what happens if you're holding down one button and then press another? I'd assume that while loop at the end screws you over a bit there.
nice one! Thanks for the tip. I was just goofing around with these
Thank you so much Kevin. Please i need the diagram?
Erhm honestly thanks for showing that circuit with the analogRead at the start. That'll actually do for my project since it's only 4 buttons I'm using and they shouldn't/can't be pressed at the same time in my interface. I'll definitely start playing with shift registers in the future though when time is a less pressing issue :P
Great Video ! I want to use a Rotary Encoder and need the Interrupt for this one. But I could use any PinChangeInterrrupt for checking any pressed button, right ? Greetings from Austria !
Hey, I'm curious. What microcontroller are you using in this video?
@kevin darrah sir im asking for help do you have a schematic diagram of 4 or 5 or more inputs interlocking circuits...for example i have a four inputs push button 1 to 4 and output LED 1 to 4 the sequence is like this..if i press push button 4 the LED4 will turn on then when i press push button 1 the LED 4 will turn off then the LED 1 will turn on then when i press push button 3 the LED 1 will turn off then LED 3 will turn on and so on..it always one LED will acivate on OUTPUT...please...thanks in advance
Why not use a 74HC165 and just read the buttons directly
thank you so much Kevin for that awesome video, can you do another for 74HC165? it would really helpful. thank you.
yes you are right! Ha, this actually got me as well! will update the code
Hi Kevin, I am trying to figure out how to make simple led selection indicator, so I want 8 LED to be shifted in an infinite loop by a press of a button. So one press next LED is lit, another press next to last LED is lit and so on in circle. I hope you understand what I want to do. Please help, thank you :)
Kevin, how is this wired? I'm having problems getting this set up. Thanks.
Hi. I have a working circuit which has 4 digit 7 segment led display. I want to to read the Data and make my arduino send Smae numbers to my serial monitor tgat what my segment display shows simultaneously. What u wld advice? And umm this segment display has total 12 inputs which 8 for leds and 4 for shifting from eacg 1 number block cathodes(or anodes). Any advice?
Just want to say man, really good video very clear and understandable you have a great channel
Just wondering if you could offer suggestions as to why this code works on an UNO but not on a mega2650? I've tried changing the pins in the code to the appropriate MOSI and SCK pins on the mega but still no luck. Any help appreciated
Tq for the detail explanation,..about to build something and learning for shift reg, already watch many vid n good input
I couldn't find schematics for this in your site! are those published anywhere else? :O :O
oh boy, the only advice I have is to keep playing! Interested in something? Buy it, and start tinkering!
I am relativity new to programming shift registers. How could I modify this code to use only 1 register for inputs and then do something like print "high" to the serial monitor?
Have you tried connecting this to the arduino in your cube to change the animation that is running?
There is an error in the code. 595's are activated by rising edge, but your code is written in such manner digitalWrite(HIGH), then digitalWrite(LOW);
Learn It's not necessarily an error, but he has to be sure the output was low before he makes it high. But you're right, reversing the order (first low, then high) would be better.
How would you apply the same principle for an analog signal ? The Arduino has only 6 analog inputs, what could we use to extend that to say 20+ analog inputs?
What type of diode would I need for my push buttons? What type of diode are you using? Many thanks
any type of diodes should work correctly in this case... it doesn't matter as long as they are forward baised as shown on the tutorial
Jamie Lewis He's probably using 1N4148s. These are cheap and more or less the standard for small signal diodes.
Do you have schematics/diagrams for the connections please?
Basically the register connected to the buttons just send one bit per cycle to identify if the actual checked button is pressed!? This a is a amazing idea.
Can a automatic style casino keno game be made up with Arduino or some micro controller. It would show game number as well as drawing 20 random numbers out of 80 leds.
What pushbutton are you using?
thanks. clear video on how it works. but i'm not familiar with SPI, so the code is a little hard to understand.
As any arduino board all pins can be used as digitalRead. My arduino Pro micro board has 18 pins for use as digitalRead. I
Need to 42 pins for input to my arduino board(for different outputs) . Is it possible using 74HC595 shift register????? . If yes, please help me with arduino sketch & wiring diagram arduino board to 74HC595 and push buttons,,,,,,,
Is there a reason why I can't get my led circuit, the circuit is from a double ended light saber thing that have I think 6 leds on one circuit that has one + cable for each and one - cable for all of them.
I'm looking for something similar but I need to press a button and it will turn off the other. it's possible?
I'm thinking about it. if you control the LED via 74HC595, you can control about 4x4 LED's with every shift register. But if you use it as an input, it's almost 8 at a time. That means if you have a lot of inputs, like 200, you will also need a ton of registers, like 25 for 200. I feel that there will be problems if the inputs are grouped in matrix, any idea?
You could possibly make a matrix input but I don't know if you could maintain the interrupt driven feature of this scheme. Therefore you would have to constantly scan the matrix.
How about this:
Use the '595 to drive 8 rows high. Use a parallel to serial shift register to read the columns, and constantly clock it with a PWM output. Don't actually read the input, just let it wait for an interrupt. When any button is pressed the input will go high within 8 cycles of the PWM. Then read the input shift register to see which column is on, then scan the '595 to see which row is on.
Sorry.. am only a beginner.. I got little confused.. I heard 74hc595 is an output shift register (Serial in parallel out) and 74hc165 is actually an input shift register.. But here (05:40) you are mentioning it(74HC595) connected with push buttons.. Little confused
you output 1 sequencially to each button and read the common part (pin2) to see, if there is 1, so if the respective button is pressed. Input is pin 2, the output register 165 shifts the "voice" to particular buttons.
Kevin, I would like to discuss one idea with you. How could I contact you?
+Sergey Romanov kdcircuits.com
I love this project, really, but it would be incredible if we had schematics. In the end, becomes a very dark and cloudy tutorial. How to work the outputs, I get loads of info, but I'm having trouble knowing where to connect the pin 2 to the shift register as said in the code. Actually, I can't find any documentation of using 74HC595 as an input expander. Only with other shift registers like 74hc165. I see 74HC595 as an output expander.
This did blow my mind, it was so awesome. I could have never imagined such an amazing technique was even possible.
I don't know about mechanical switches though, rather hard to use code to debounce using switches comomg through shift registers, can be done though, resposiveness may be a trade off that needs to be made.
cheers
Hi! Does all this can work on a raspberry pi (and domoticz)? Thanks :)
That`s genious.
Who ever came up with using shiftregisters this way should have a madal.
This is remarkably similar to how a Diode Matrix Rom works. Very clever!
could you please show me a schematic of how i would connect 8 switches(input) to my 74HC165 to Arduino and then to my 74HC595 to my 8 LEDs(output)?When a correspong switch is off, then a corresponding LED should go on
did you found some schematics about it?
Nice walkthrough. On the software side you should be able to replace your bitRead()/bitWrite() with just Output ^= Check; Someone's probably already brought it up but I have no desire to fight youtube's comment system long enough to make sure...
This is so cool! Thanks for explaining!
And also: Fix your indentation!!! :-P
very nice video. i only have a basic knowledge of what different components do and i was able to understand this very clearly
Hi there,
Has anybody got any tips/ advice for a relative beginner in electronics? I've got some basic knowledge about basic components(leds, resistors, caps) and the picaxe system. But I was wondering if there are any good websites or other resources that could help me broaden my knowledge/ understanding and work with more complex things with more integrated circuits driving a wider variety of outputs.
Thanks.
If I want to use 4 total shift registers, 2 for buttons, 2 for lights, do I just wire them all up together from the same clock/data/latch pin? And what changes in the code when you add more registers?
Yes, you just chain them as many as you want (and even may play with the order) and in the code you need more "Output" variables (array?) and loop the "Change" over all inputing chips. Also note, that you can use part of chip for LEDs and part for buttons, if you wish so. Just little complicated code, but not too much. The idea is still the same - cycle 1 over buttons, detect, what is pressed, the restore outputs to proper state and wait for another interrupt.
@@Gilhad1 please sir could you write the code for us? i'm desperate for searching and i need only 16 input and 16 output just with the same i dea of Kevin
It would really be helpful if you upload the circuit schematic
yes, that is a very cool method. You can roll that same technique into what I'm showing you. Hmmm, that's funny he also discovered the diode issue... guess it is pretty obvious
Wow, no dislikes, congratulations! Great vid!
Thank you so much for this video! you answered a lot of questions I had for my pinball project.
how many shift registers I need to control 25 key keyboard ? what is scan matrix? with 9*3 scan matrix can I control all 25 keys . how to set up that? pls advise
4 registers using this idea verbatim. to read matrix you need different aproach. Depends on many factors, what is the best, I woul use one mcp23017 over I2C for matrix up to 16 (rows+cols total)
The first switch scheme is flawed because it assumes that all the switches and wiring will remain pristine, but what happens when the switch contacts get tarnished and slightly resistive? It works good when the equipment is new, but what happens in a couple/few years?
The shift register button scanning scheme, as you explained it, starts out with all 1s then upon getting the interrupt it shifts a 1 through the chain. You would have to continue shifting that 1 until none of them came back high, at which point you could then re-enable the interrupt, otherwise you would miss the successive switch closures because they wouldn't be generating an interrupt. Since you are stuck using a '595 anyway, just use a PISO shift register like a 4014- same number of pins, none more of the headache.
Yeah I know this is old.
Yes, I am confused about the chip choice. Isn't the 595 intended for parallel out? vs the 165 for serial in?
You are the best dude!!
hello, can you please put a fritzing picture or a schematic for the circuit ?