Controlling X-Plane 11 with Arduino & MULTIPLEXERS (Home Cockpit Build Basics)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and a warm welcome to fun of flying in my last video i showed you how to connect seven toggle switches to an arduino leonardo microcontroller and how to write the relevant ide sketch code so that the seven physical toggle switches in the real world actually operated the seven corresponding switches in the virtual world now as you know the method of controlling x-plane 11 in this way is for the arduino microcontroller to issue various keyboard commands to your desktop based on how things like physical toggle switches are set the problem with this though is that the arduino leonardo is only one of a few small microcontrollers that can work in this way due to their native ability to send keyboard commands back to the pc that's attached to it the other micro controllers that you could use are the micro the duo a or the mkr0 now these are all well and good if your intention is to build a relatively small home cockpit setup with a limited number of switches connected however if it's your intention to build a much larger scale home cockpit setup then you're going to run out of microcontroller pins pretty quickly to which switches and the like can be connected with this in mind there are two ways that you can increase the number of micro controller pins available to you the first of these is to use a master controller and slave controller linked together such as an arduino leonardo as the master and a mega 2560 or even an uno as the slave and in this particular regard i'm currently in the process of preparing a video showing you how all this can be achieved something that i hope to release on youtube in the next month or so the second method is just to use one controller which must be a leonardo for reasons already explained which is then attached to one or more 8 or 16 channel analog multiplexers and this is what we're going to be looking at today specifically i'll be showing you what a multiplexer is how to wire it up to a microcontroller as well as the seven toggle switches that we used last time and then finally how to write the associated ide sketch code to make it all work [Music] okay so what is a multiplexer well in layman's terms such a device is a small printed circuit board with an on-board processor that acts as a glorified switch not any old switch mind you it's one that could have multiple inputs from up to say 16 toggle switches coming in on one side and only one signal output leaving the other side this signal output then goes back to one of the limited number of pins on the micro controller itself thus giving a theoretical input signal reduction ratio of about 16 to one when the device is set up like this it's generally referred to as a multiplexer or you may have even heard the term mux however the cleverness of this little device doesn't end there because depending on how you write the associated ide sketch code you can also set it up in completely the opposite way now what i mean by this is that now you can have one signal input coming from the micro controller to one side of the device and then have 16 different outputs leaving the device on the other side to say a number of lcd displays in this case you have a theoretical output ratio from the microcontroller of 1 to 16 and when it's set up like this the device is now referred to as a demultiplexer or demux now in terms of multiplexer or demultiplexer input output ratios of 16 to 1 or 116 and in the general sense of trying to minimize the number of microcontroller pins being used i was cautious to use the term theoretical why well whether you use the device as a multiplexer or a d multiplexer you still have to use at least four of the micro controller pins for serial communication which means that these particular pins can no longer be used as input or output pins for anything else having said that though my understanding is that these same microcontroller pins used for serial communication can actually be shared with any additional multiplexer or d multiplexers used in your circuit bottom line though using such devices basically saves pin availability on the micro controller which in terms of the small capacity leonardo has to be an advantage no matter which way you look at it okay so here we are in one of my favorite desktop applications namely fritzing which is an excellent software tool for designing wiring circuits and on the screen you can see the main components that we're going to be using firstly we have the arduino leonardo microcontroller which is connected to a desktop via the micro usb cable which gives this leonardo 5 volt supply and the cable also allows for two-way serial communication between it and the desktop pc then we have over here our lovely multiplexer 16 channel multiplexer in this case here and you'll note that the numbers of the uh terminals start with zero c0 and end in c15 it's not 1 to 16 it's actually c to 15 so that can be a bit confusing sometimes when you're connecting up and then we have our seven toggle switches that we used in our previous video so the first thing we need to do is to power up the multiplexer and we can do that by taking a five volt supply from the leonardo and connecting it to this side of the multiplexer to pin uh vcc victor charlie charlie and then of course a simple ground cable here to the ground terminal [Music] then we need to pick up the ground side of each of the seven toggle switches like we did before if you remember and that goes to the spare ground cable on the micro controller [Music] simply enough so far but now comes a little bit more complicated stuff um we have to use um as i said earlier whether you're using this as a multiplexer or d multiplexer it's a multiplexer in this case now you need four cable or four wires going from these four pin terminals here sierra zero to sierra three going back to um four available pins on your micro controller it doesn't matter where you put these pins they could be in four five six and seven ten eleven twelve thirteen i've chosen eight nine ten and eleven um you can put them wherever you like within reason you can't use these two terminals here or those two there or that one maybe or that one so from pins 2 to 13 you can put them in there somewhere [Music] so the important thing to remember here is that these serial cables have to be wired up to the correct terminals of the multiplexer so for eight pin eight here that goes to terminal sierra zero pin nine goes to sierra one pin ten goes to sierra two and lastly pin 11 goes to sierra three whatever you do make sure that these are wired up the right way around otherwise it won't work at all [Music] then we have a signal cable and i've taken from pin three here and that goes to the sig or signal terminal on the multiplexer quite straightforward now when you've connected all your switches up uh to this side the the input from those switches goes through to that one cable there and it's red from that pin that's all [Music] so there we have our well seven tools which is in this case all connected up um and i've used pin terminals uh charlie zero through to uh charlie six um now it's important when you start writing your id eco that you remember which switch is connected to which channel on the multiplexer and you also need to remember which where you put your signal cable here say to pin three on the microcontroller so that is important that number there is important when you write your sketch code and these numbers here are important because you need to know uh for charlie zero for example it's going to in this case the beacon lights charlie 4 is going to the strobe lights you need to remember that otherwise you could end up with a bit of a mess so i've put the information up here so that you can see it more readily and that my dear friends is it as far as the wiring is concerned um so the next thing i'm going to do is to show you what that looks like on my little test board [Music] okay so this is my little test board here that i made for the last video and the various components are here we have the leonardo microcontroller we have a mega 2560 microcontroller which is not being used in this particular video either we have a power on uh indicator led here we have a distribution board of five volt pins in red here and then ground pins in black in the middle is the new item here the multiplexer just here and then on that side of course you've got your seven toggle switches so in terms of the wiring um as explained just now in the fritzing application we have a 5 volt supply coming from the board and to the board here from the desktop so this this 5 volt supply goes goes from here to my led indicator and then goes to this distribution board here and from there this red one red cable here goes to pin vcc on the multiplexer similarly on the ground side you've got a ground pickup from the board here to to this distribution board and over to a gnd pin on the multiplexer from the other side of the microcontroller here we've got these four uh serial communication cables the yellow orange red and brown and all of those are on pin 8 through pin 11 and they go over to the multiplexer pin terminals sierra zero to sierra 3 and as i said earlier it's imperative that you get these the right way around so on here you've got pin eight on the board on the microcontroller going to sierra zero on the multiplexer pin nine going to sierra one pin 10 going to sierra 2 and pin 11 going to sierra 3 then this blue one here is from the pin terminal on the on the micro controller pin 3 and it goes to the sig terminal on the multiplexer on the other side of the multiplexer you have seven wires from channel c 0 charlie zero through to charlie six and each of those go to uh one of the respective toggle switches over the other side and on the other side of the switches you've got a ground being picked up they come all the way back here to this distribution board and then from there back to the micro controller board and similarly as i said on this it's important to get the serial cables the right way around it's obviously important to remember which way around you've got these toggle switches connected ie this pin this one here goes to uh charlie five it looks like so you have to remember that when you're writing your ide code and speaking of ide code why don't we now go and have a look at that okay so here we are in the arduino ide sketch code application and this is the start of the code um all of this bit up here you can ignore because we covered that in the last video um down here is the libraries included we need the keyboard.h library again as we used before because we are sending keyboard commands to the desktop and ultimately x-plane 11 and we also need this library here mux dot h which is your library for using uh multiplexers and in this case i'm using a 406 7 16 channel multiplexer if you want to try this and you want to get hold of this mux.h library then you can get it at github which is the address of which is here um this uh i'm not entirely sure what this is in the code um i've looked it up and apparently it's a naming convention for uh codes or libraries um and it just avoids conflicts with all the different libraries and the names being used um i wouldn't get too hung up on that so the next bit down here is an important part this slot here this bit up here is is purely text it's not included in the code but this bit here where it starts mux 1 that is included in the code and basically what this means is you're setting up a an instance of a multiplexer where you're setting up one multiplexer so you start with the phrase mux which has got a nice orange color to say to show that that is actually um correct and then you give it a name mux1 open parenthesis or brackets and then you got pin and if you remember we have a signal cable going from pin three of the micro controller um to pin sig sig on the multiplexer so that's where we're picking up all our information and we're setting up that pin as an input because we're reading from it we're getting data into it and reading from it and we're using the internal pull-up resistor the pin type is digital [Music] um because we're getting one or zero uh or high voltage or low voltage at that pin and the pin sets eight nine ten eleven if you remember where all the serial communication cables uh go from the micro child uh controller to pins sierra zero to sierra 3 on the multiplexer so those are the bits that enable the multiplexer to talk to the leonardo this one down here is uh is blocked out from the code but it's um going to be a little test for me later where i'm going to actually trying to use two multiplexers um the leonardo will only read from one at a time um but i'm going to try this one and see if it will actually read from two not at the same time at different times but it there will be a second multiplexer figure physically put into the circuit and you'll notice that all of this is the same here as the one above except for uh pin four which is where i'll connect the signal cable from the multiplexer to the leonardo as i referred to earlier the pin set 8 9 10 11 on the serial cables and i i understand that uh however many multiplexers you have you can use these same pins as as these will be shared between the multiplexers okay so moving down the code a little bit we have all of this here and you if you saw my last video you you might remember this um this is uh two lines of code for each of the seven switches basically very similar looking and what we're setting up here is an integer in memory storage an integer if you remember as a single number with no decimal places and we start it off with a value of zero and switch beacon state i the beacon switch state as as the code loops around and around and around um it checks the beacon state each time it does so and it's looking to see if it was different from the last time it did it and if it is then it assumes that the total switch has been moved and that then will uh make something happen further down in the code so all of this is number storage for each of the toggle switches current beacons light state in this case and previous beacon light state and the same for all the other switches down here going down to void setup initially we open a communication with a serial monitor because in the code later on i'm actually going to put the results of this program up on the serial monitor screen so you can see what it's doing when i throw the respective switches on my little test board so that's all that is there when i first came across or when i first started researching um code for multiplexers i came across this line uh here which apparently was required for the leonardo microcontroller only well i'm i'm using that microcontroller and i've blanked that line out and it still works so i'm not entirely sure what it is but i've left it there just in case i need to refer to it later then we come to the more interesting parts and this is the bit that's more specifically written for the multiplexer itself and we are going to ask the program essentially to um go through each of the multiplexer input channels one by one and read each one and then do certain things with that information so um to start the loop we have this little bit of code here and i'll try and explain it i've written some text here in an attempt to do that to try and explain it a bit a bit more for you but basically uh keeping it simple we are asking the program to look at channel 0 or c0 on the multiplexer i means channel doesn't matter which number channel it just stands for a channel but in this case we're saying if channel if the channel is 0 that's where we want you to start looking i.e the first uh terminal on the multiplexer um so start at zero but don't go any further um uh finish at zero that's this this bit here i is less than one prevents um the program from looking any further than channel zero because that's the one we're really interested in um and there are more examples of this a bit further on if that if i had put i is less than nine then it would have cycled through each of the channels courtesy of i plus plus until it got to nine and then it would start again but we're not doing that in this case because we're not interested in nine channels or the data from nine channels i'm just interested in one channel and it's charlie zero and that is where our beacon light switch is connected to the multiplexer [Music] so what we're saying here is okay start at channel one read the information there sorry channel zero read the information don't go any further because that's the only one we're interested in and read the pin uh which is uh signal pin three on the on the microcontroller um let me rephrase that it starts by asking us to look at channel 0 on the multiplexer which comes through on signal pin 3 on the microcontroller but once it's read the data it's looking for a high or low voltage and when it's uh as it loops through as the code loops through in the void loop section it will come across this pin this channel each time it will read this channel each time and it will say right well is this uh is the data on that pin different from what it was last time and if it was then it assumes that the physical toggle switch for the beacons beacon lights has been moved it's been switched on or off so yes it is different from last time in uh let's assume that and it will say okay okay if it is different and the voltage is low then what i want you to do is issue a lowercase c keyboard command to the desktop and x-plane and that will move the beacon light toggle switch accordingly in the virtual world i also want you to serial print [Music] this these this text here i want you to say beacon lights are switched on if it's a low voltage and i want you to serial print the value uh or the channel i um on the screen as well i'll show you that in a minute however if the voltage state at the switch are at channel 0 is high then i want you to issue keyboard command lowercase c again because that's how x plane 11 works you use the same keyboard command in many cases to switch to turn the switch on and turn it off only in this case i also want you to print beacon lights are switched off so when you using this code for real in a home cockpit setup you won't need to have this serial print line here or this serial print line here it's just a visual representation on a serial monitor to show you whether your code is working or not so eventually when we hone this program i will be removing those serial print commands okay so we've determined that the switch is different from when i looked when the program looked at it last time and we've done one or either of these two things based on whether it was a high voltage or a low voltage at channel zero on the multiplexer then the program normalizes or neutralizes i should say um what it's seen before and it's now going to declare for itself by courtesy of this uh like a change state command i now want you to assume that the last time we looked at the the switch state is now the same as it is this time you're looking at it to stop continually looping through this program if you don't have this bit in here then the program will continue to continually issue keyboard commands of lowercase c and you'll get rows and rows and rows of them on your serial monitor and the switch in the virtual world of x-plane will be flicking on and off all the time which we don't want so you have to put this in here and when the uh these switch states are considered to be the same this time and the last time we looked then release all keyboard commands so then moving further down it's just pretty much the same thing again for all of the um other toggle switches only there are some subtle changes to what we're asking the program to check in terms of the multiplexer this time here for the landing lights switch we're saying we'll start at channel one this time on the multiplexer because that's where our landing light switch is connected don't go any further than one by issuing this command here i less than two so really interested in channel c1 don't bother incrementing any further because again that's where we're what we're interested in so read it and if there is a low voltage at that channel on the multiplexer then issue keyboard command d um if the switch state this time you checked is different from the last time you checked i.e the switch has been moved so issue keyboard command d in this case and print landing lights are switched on and then if the voltage is high um issue keyboard command d again and print on the serial monitor landing lights are switched off neutralize the code with this bit here and release all keyboard commands and it's the same all the way down through all of the toggle switches the only thing that more changes is the description of the switches um and this uh this commands here for checking the multiplexer each time we're only interested in one channel so we're confining the check or the search through the multiplexer to one channel only and we do the same thing all the way down to the last switch which is for pitot heat and this is we're interested in channel zero s charlie six on the multiplexer so that's where you start looking no point in looking at channel 0 1 2 3 4 5 because there's nothing of interest in that on those channels as far as this part of the code is concerned we only want to look at channel charlie 6 for the pito heat switch because that's where it's connected and issue these commands down here as we did before okay so that is more or less it for cut for the code um admittedly i just it looks complicated they always do these codes um the only way you're going to learn is to use them play around with them uh making sure that you set up your physical wiring uh to match the pin numbers that you've declared in the code and uh what i'm going to do now is i'm going to switch my microcontroller on there it goes and i'm going to open the serial monitor up here [Music] and it's currently showing up on my other screen so i'm just going to bring it across here like this word of warning when you test this code not only will you get text showing up here but don't forget the the code is doing what it's designed to do and that is to issue keyboard commands and it will do that irrespective of where your focus is on your pc you could be in a word document and it will start issuing those keyboard commands in that document if you're focused on this code it will start issuing keyboard commands in here wherever your cursor happens to be so if we if i'm down here for example and i run this code then it could start putting letters in here and ruining your code so don't just be aware of that and if you are going to run the code make sure your cursor is in this box up here so when the program starts issuing keyboard commands it will do it here and not in your code which which could conceivably do a lot of damage and it will take you a while to find out what's what's actually happened so now with that cursor firmly in that box i'm going to throw a switch here and look at that it works i just switched on the fuel pump and it's on channel charlie 5 on the multiplexer and it's switched on i'm going to turn it off again there we are now the next one beacon lights on off landing lights on off taxi lights on off navigation lights on off strobe lights and lastly pito heat [Music] there we are and you can see up here is there's the keyboard commands being issued there and this is the text uh that we asked it to print on the serial monitor as well so when we get this code up and running in the real world in the real home cockpit setup we're not going to ask it to print this it's just taking up memory on on your micro controller all we want it to do is issue these keyboard commands which go through to your desktop through to explain and move switches in the virtual world [Music] okay so having done that we can we've tested it it's okay we'll dismiss the serial monitor now we don't need it anymore um i don't think i've made any uh changes to my code by mistake i'll just run the compiler again to make sure it still works yes it does that's good that's it we don't need to do any more so what i'm going to do next is to um open up a session of x-plane again with our little cessna 172 and i'll inlay some video of my test board like we did last time so that you can see the uh physical switches operating the uh the corresponding switches in the virtual world of x-plane okay so let's go and do that okay so here we are in our little cessna 172 and these are the switches that we're interested in so we bring in our video overlay of my test board and we'll just go through these switches one by one [Music] starting with the fuel pump on off and then the beacon lights on off landing lights taxi lights [Music] nav lights strobe and lastly apito heat so we can switch them off very quickly or switch them on very quickly i should say in quick succession and then switch them all off and there are no delays they all do what they're programmed to do okay so that really brings us to the end of this particular video um it's taken an awful lot of time to try and work out how to implement multiplexers into a circuit where it can be used to control a flight simulator x-plane 11 in this case but i'm really pleased that i persevered with it um it does work and if there's anything you can do to reduce the requirement for pin usage on the arduino leonardo then so much the better and the multiplexer does just that unfortunately the leonardo is is a definite requirement in these circuits because it's the only one or one of a few that can issue keyboard commands but unfortunately it's memory capacity and the number of available pins to use are not that extensive so um why not use multiplexers instead so there we are um if you've got any questions as usual please let me know um give it a go see how you get on and if you found this video of interest then please smash the like button and also don't forget to hit the subscribe button and the bell so that you don't miss anything going forward okay so that's it from me this time tata for now
Info
Channel: Fun of Flying
Views: 553
Rating: undefined out of 5
Keywords: #Arduino, Multiplexers, HomeCockpitBuilds, XPlane11
Id: v9SLjLVk1YA
Channel Id: undefined
Length: 35min 21sec (2121 seconds)
Published: Sat Nov 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.