How to Add Multiple Inputs to an #Arduino using a Shift Register - The Learning Circuit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the following program is brought to you by element14 the electronics community where you can connect and collaborate with top engineers from around the world join now at element14.com presents hi and welcome back to the learning circuit we've been learning about shift registers and in this video i'm going to go over how to use one to expand the inputs of an arduino but before i get into that let's review a little information about shift registers in general shift registers process data in the form of binary bits which are ones and zeros representing high and low voltage signals this data is processed in series or in parallel series data is processed in order one bit at a time requiring only one input or output parallel data has multiple bits processed simultaneously requiring one input or output for each bit of data being processed to expand the inputs on an arduino we need a shift register that can take in multiple bits of data in parallel and transfer it in series to a single output so we need a parallel in serial out shift register like this 8-bit 74-hc-166 this chip has 8 parallel data inputs labeled d0 through 7. since this chip can also take serial data in it has an additional input for that labeled ds pin 1. the serial output is q7 pin 13. data can come in in 8 bits at a time through the parallel inputs then shift out in series out q7 looking at the left side of this functional diagram we find the remaining pins again ds is the optional serial data input where bits of data could come in one at a time on each clock pulse as we commonly find in ics mr is the master reset which when pulled low resets all the internal data to low pin 7 cp is for the clock signal the clock can shift in the bits from all eight parallel inputs at once or shift out the bits stored in the shift register one at a time on each clock pulse we can see in this truth table that this happens when the clock pin goes from low to high pe is active low and is used to switch between the eight parallel inputs and the one serial input pulling the pin low enables the parallel inputs while pulling the pin high enables the serial input ce is used to select between shift in and shift out when the pin is pulled low the inputs are enabled and their data will be shifted into the registers when ce is pulled high the output is enabled and the bits stored in the shift register will be shifted out so mr gets connected to vcc to hold it high and inactive pe gets connected to ground holding it low which selects the parallel inputs to start we pull ce low to ground to enable the inputs when the clock pulse goes from low to high the bits at d0 through 7 are all shifted in in parallel and stored in the shift register pulling ce high will disable the inputs and enable the output on each clock pulse bits will be shifted out one at a time now that we know how the chip works let's look at how to use it to expand the inputs of an arduino start with the shift register on a breadboard and the arduino uno handy the power and ground rails of the breadboard will connect to ground and 5 volts on the arduino [Music] for the shift register start with pin 8 connected to ground and pin 16 connected to vcc as we determined before we can connect pins 6 ce to ground to keep the clock enabled and pin 9 mr to vcc to keep the reset inactive since we aren't using the serial data pin will pull pin 1 low to ground [Music] now for the 8 parallel input pins each will get tied to ground with a 10 kilo ohm resistor this way each input will default to a low signal pin 2 pin 3 pin 4 pin 5 pin 10 pin 11 pin 12 and pin 14. then we add eight buttons each button connects to one input pin [Music] then all the buttons connect to vcc so that when the button is pressed its pin is pulled high [Music] the three remaining pins will get connected to i o on the arduino as we will use those to control the shift register and receive data [Music] pin 15 pe will connect to pin 2 on the arduino pin 7 the clock input will connect to pin 3 on the arduino and pin 13 the serial output will connect to pin 4 on the arduino that's all the connections now let's look at the code start by establishing our three pins as integers i called them i o select clock pulse and data out we also need to create an integer used to declare which bit is set high another integer that stores the digital read value of the data pin and we use the arduino variable byte to store an array to show which button was pressed next is void setup where we declare the pins we use as an input or output and we use serial.begin to set the bit rate for data transmission now for the main program which is contained in void loop start by setting pin 2 low to enable the parallel inputs then pulse the clock to shift in the data from all 8 inputs then disable the inputs and enable the output this for loop will cycle through the bits from the eight inputs to determine which have been pressed the serial.print command tells it to print that information to the serial monitor showing which button and if it's pressed this if loop reads the same information but outputs the data as a decimal and a binary number let's review what that means binary can be a 1 or 0 depending on if the signal is high or low our 8-bit chip has 8 digits in decimal digits round after 9 while in binary digits round after one so the place for each digit of binary equals these numbers in decimal 1 2 4 8 16 32 64 and 128 the number is read right to left rather than left to right so the first button is represented by the rightmost digit pressing the first and third buttons would look like this and yield the decimal five while pressing the fourth and seventh buttons would look like this and yield decimal 72 at the end of our code we add a delay for debugging and so that the monitor will pause between each reading making it easier for us to see for now i'll set the delay to really long so we can see just one reading at a time okay that's all the code i'll plug the usb cable into the arduino upload the code and open the serial monitor so we can see our data [Music] here you can see the eight button positions listed with a value shown of whether the button was pressed or not at the bottom you can see the value of the buttons pressed expressed in decimal and in binary with only the minimum number of digits shown if i press button number 8 the binary number will show as an eight digit number since my delay is still really long i can hold down new buttons and press the reset on the arduino to get a new reading if i reset the delay back to 500 milliseconds we can see the code run over and over detecting new button presses [Music] by using just three pins on the arduino we can read the data from up to eight buttons or other input devices more inputs can be added by stacking multiple shift registers together let's see what two shift registers and 16 buttons looks like the second board gets power and ground from the first the second chip is set up like the first chip the clock pins pin 7 are tied together as well as the pe pins pins 15. the q7 pin of the second chip is connected to the ds pin of the first chip we still only need three pins on the arduino we also need to change the code so that it shifts through 16 digits instead of to get a proper decimal and binary readout we can swap out byte for this function which is for a 16-bit unsigned integer and i'll change the delay back to be long so we can see all 16 bits of data upload the code with the new code if i press button 1 on the first chip and button 8 on the second chip and run the program you can see position 0 and position 15 are a 1 high and the binary and decimal numbers still show up at the bottom those 8 new button values are pretty high so that's how to use three arduino io pins to read the data of 8 or 16 buttons you can keep adding buttons or other input sources by adding more shift registers you just need to adjust the code accordingly all right i have a challenge for you in the last video i showed how to use a shift register to expand the outputs of an arduino and we just learned how to expand the inputs of the arduino so see if you can combine the two if you can figure that out push your code schematics video photos whatever you come up with to the element14 community on element14.com forward slash the learning circuit i look forward to seeing what you can do happy learning [Music]
Info
Channel: element14 presents
Views: 42,829
Rating: undefined out of 5
Keywords: electronics, hardware, hacking, mods, weekly, element14, maker, engineering, element14presents
Id: k4va1JFtzSg
Channel Id: undefined
Length: 12min 47sec (767 seconds)
Published: Wed Sep 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.