Optimizing work with Arduino Digital Input Pins: Build BCD-to-Decimal Converter with Dip Switch"

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I made several videos in which I was showing how to control multiple LEDs using fewer number of pins we explored multiplexing using shift registers Max 7219 module or even uh LED strips all those videos focused on making the most out of the output pins but we have never done the same thing for input pins there is a good reason for this in my project I used at most most three push buttons which Arduino can easily handle but let's imagine we are having much more push button switches to provide input to Arduino this video is going to guide you through the options available to you when dealing with large number of inputs sounds interesting stick [Music] [Applause] [Music] around when you want to make things happen happen in electronics you usually use buttons and switches of all kinds but if you have many of them it can be hard to make everything fit and work especially on a breadboard in this video I will show you a simpler way instead of using seven separate switches I'll use something called dip switch to make things easier to handle a dip switch short for dual inline package switch is a manual electronic switch that integrates several switches seven in case of this video dip switches are often used in electronic devices and circuit boards to configure settings or control various aspects of the devices operation one example being binary encoding each switch represents a binary digit zero or 1 allowing for the configuration of different combinations to represent specific values first we'll make the easiest circuit using a switch from the dip switch to control a single LED you will need a 5vt power source and the positive side connects to the LED's longer leg through a resistor the shorter leg of the LED is connected to one side of the dip switch the other side of the dip switch is connected to the negative side of the power supply now we'll set up the same circuit on the breadboard here's our power supply dip switch and the LED connect them following the diagram's instructions now let's apply power to the Circuit as you toggle the first switch you will notice LED turning on and off to use all seven switches you could connect seven LEDs the same way but let's try something more exciting instead we'll use a common anode 7 segment display this means we can still control seven LEDs that form the display using this display we can input more meaningful information using the dip switch to keep things simple I'll use a single resistor in the circuit typically we would use seven resistors one for each segment in our setup we ground all the switch legs on one side and the legs on the other side connect to pins representing individual segments this approach allows us to create a display that changes based on how we set up the switches so instead of just turning LEDs on on and off we can now create patterns or numbers on the seven segment display with a dip switch let's set up this circuit on the breadboard we'll remove the LED and current limiting resistor here is the seven segment display the middle pins at the top and bottom of the display are common anode pins we'll connect the current limiting resistor to the top one for better visibility I have already connected segment C d and e to their corresponding pins at the bottom using custom jumper wires now with standard jumper wires we can connect the remaining segments a b f and g it's time to ground all the pins on the other side of the dip switch by toggling the switches we can display different digits for instance with switches 1 2 and 3 we can light up segments a b and c to display the digit 7 now let's turn on segments D and G with switches 4 and 7 to display digit 3 next we'll turn off segment G and turn on segments E and F with switches five and six showing the digit zero one more we will turn off segment a turn on segment G and then turn off e and D oops wrong switch that's it we have successfully displayed digit 4 however my channel revolves around Arduino so where does Arduino fit into this scenario we can place Arduino between the dip switch and the components we want to manage let's revisit the simple example of controlling a single LED in this setup we introduce Arduino into the circuit for Led connectivity we connect the anode through current limiting resistor to the digital pin that will control the LED and the cathode goes to Arduino ground connecting the dip switch is a bit trickier one side connects to the digital pin through which we input data to Arduino and the other side goes to 5 volts turning on the switch registers a high signal the one at the Arduino pin when the switch is off there is no signal leading to a floating pin situation and potential noise to avoid this we pull the signal down to ground using 10K pull down resistor now let's reflect the circuit on the breadboard starting with connecting the ground and VCC rails of the breadboard the short leg of the LED connects to ground while the long leg goes to digital pin six one side of the first switch connects to 5 volts and the other goes to digital pin 12 with this setup we can now write the code starting with dip switch and led D pin declarations in the setup we configure the dip switch pin as input and the LED pin as output in the loop we check the value at the dip switch pin if it's high we send the high signal to the LED pin and if it's low we send low signal to the LED pin now that I think of it we can simplify this code we can directly read the signal from the dip switch to the LED pin when it's high the LED will turn on and when it's low low LED will turn off we power the Arduino and there is an issue with the switch off the LED should be off but it remains on for a while this is due to the floating pin situation I mentioned earlier adding a pull down resistor will resolve this issue and as you can see this code works and we can toggle the switch and with it control the LED there is another Improvement we can make by eliminating the pull down resistor we'll reconnect one side of the dip switch from 5 Vols to ground now when the switch is on WE register a low signal at the dip switch pin however when the switch is off we might encounter a floating pin situation this time we can resolve it by pulling the signal to 5 volts using the built-in pullup resistor each Arduino digital pin is equipped with to activate this pull-up resistor instead of of declaring the dip switch pin as input we declare it as input pullup now for the code to achieve the same result we check for low signal at the dip switch pin when detected we send the high signal to the LED pin otherwise we send low signal here too we can simplify the code but this time we read the signal from the dip switch pin and propagate its inverted value to the LED pin if the concept of P pull up and pull down resistor is not entirely clear to you please refer to one of my videos dedicated to this subject let's make the necessary changes in our circuit let's configure the dip switch pin as input pullup and load the code now when the switch is off the LED is on and VI Versa which is the opposite to the desired Behavior to correct this we can adjust the if statement in the code and reload it and as you can see we can control the LED with the switch in the same way we can also use an Arduino to control a seven segment display with dip switches in this scenario we also can make use of the build-in pull-up resistors by grounding the legs of all seven composite switches on one side of the dip switch the legs on the other side are then connected to Arduino digital pins that will be used for input the anode of the seven segment display is connected to the Arduino 5 Vols pin and the segment pins are connected to Arduino digital output pin in the code whatever we did previously we can extend for all seven segments however I won't implement it as there is no significant added value or optimization when using Arduino the microcontroller doesn't perform any sophisticated action and we are nearly exhausting all available Arduino pins what I'm going to do however is look at the binary to decimal encoder take a look at this table each row represents a decimal digit we want to encode and the four columns represent the binary representation of each digit these four bits can serve as our input values to display the corresponding digit on the seven segment display in this scenario we achieve the same objective with just four pins how will this approach affect our circuit let's eliminate connections to three obsolute switches of the dip switch the remaining four are no longer linked to controlling any particular segment of the display let's build this circuit I have already connected the arduino's ground and 5vt spin to the breadboards ground and VCC rails additionally I have grounded one side of the dip switch to enable the use of the build-in pullup resistors connecting the display's anod to 5 volts switches one to 4 to their corresponding Arduino pin and each segment of the display to the appr Arduino pins please note that I have adjusted the pin assignment compared to the diagram for easier breadboard connection and better visibility now let's look at the code we will need two tables one containing BCD coding and the other containing seven bit segment combinations for each digit we will declare four pins for the dip switch and seven pins for the seven segment display note that this pin assignment is different from the one shown in the previous diagram this one made wiring a bit easier with these pin definitions in the setup let's declare the dip switch pins as input pullup and display pins as output next let's create a custom function that can display a given digit on a seven segment display passing the digit as a parameter for each segment we use digital right to set it to high or low State based on the values extracted from the segments array thus changing the state of that segment consider the following example imagine a seven segment display let's execute the function for digit 4 the function examines the fourth row or the segments array processing it one bit at a time and then sends the read values to the appropriate segments using the digital write command in the loop we read the state of four dip switch pins into four variables then we scan the BCD table to identify bit combinations when a match is found we have successfully identifyed the digit allowing us to execute the custom function to display it on a seven segment display the code is already loaded we'll go through all BCD combinations starting from 0 1 2 3 4 5 6 7 8 and finishing with nine interestingly enough achieving BCD coding is possible without Arduino by utilizing the 74 LS 47 chip instead of code it employs logical gate configuration to translate bit combinations from four inputs into seven outputs corresponding to the seven segments of the seven segment display for a while I was planning to create a video about this component but with slightly different approach the plan was to optimize the use of output pins by connecting just four Arduino output pins to the input pins of the Chip and using the Chip's seven outputs to control the seven segment display I'll get around to making this video eventually this binary to decimal converter by introducing additional logic allows you to decrease the number of switches that you need from 7 to four to achieve the same objective but sometimes you cannot really decrease the number of button switches that you need keyboard is a perfect example so then we need to look for an alternative one option to consider is this little fell this is a shift register that helps you process eight inputs and pass them to Arduino using just three digital pins moreover you can daisy chain these registers and doesn't matter how many of them you connect in a serus you still can interface them with Arduino using just three digital pins sounds interesting hm but this is a topic for a whole new video we got to the end of this video if you made this far give me a favor and drop a like on this video subscribe to my channel if you want to support me further you can do it through patreon PayPal or becoming my YouTube channel member think of it as buying me a cold one as a token of your appreciation I hope to see you in my next video [Music] [Applause] [Music] ciao
Info
Channel: Mario's Ideas
Views: 903
Rating: undefined out of 5
Keywords: arduino, nano, BCD, BCD-to-Decimal, converter, DIP switch, breadboardd, tutorial, how to, instructable, arduino programming, Arduino coding, microcontroller, 7 segment display, 7-seg
Id: fQVvl5462N0
Channel Id: undefined
Length: 15min 54sec (954 seconds)
Published: Mon Oct 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.