MCP23017 Port Expander and ESPHome: Add More I/O Pins to Your Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you ever run into a project where you wish you had more gpio pins on your ESP board today I'm going to show you a simple way to use something called a port expander to add up to 128 GPI pins to your ESP board using just two wires we'll take a look at the pin out and wiring connections we'll do a simple breadboard example and we'll take a look at the ESP home code to make this easy to integrate into home assistant and at the very end I'll show you kind of an impractical crazy example of how I created 75 input and output pins using just two pins on a simple esp8266 so hang [Music] around hi and welcome to resin chem Tech now you may not have done a project yet where you've actually run out of pens on your ESP board but if that ever does happen today I'm going to show you an easy way to add additional inputs and outputs to your esp8266 or your esp32 now there are a lot of different chips and options and available ways to do this out there but the one I'm going to look at today is the mcp23017 16bit io expander now as always be sure to check the video description for additional information including links to a written guide where I'll have parts list wiring diagrams code samples and more with that let's get started let's take a look at a really simple example of where you might need something like an IO Port expander now this is kind of a a silly example but it is based on a real project that one of my subscribers was working on let's say we've got eight cabinets and each of those cabinets has a wired magnetic read switch attached to the door yes I know you could use something like zigg switches that wouldn't require any wiring but for the purpose of this let's say we've got wired switches when we open each one of those cabinets we want to turn on the light in that cabinet whether that be via a relay or some other output Source well just that alone is is going to require eight digital inputs for our switches and eight digital outputs to control our light source so how do you do something like that with a simple single esp8266 an esp8266 only has 17 GPI pins and only a maximum of 10 of those are available for digital input or output so you might be thinking well let's just move up to an esp32 an esp32 does have 48 pin but not all of those are Expo Exposed on every board and a maximum of 26 or available for digital input and output but four of those are input only so only 22 that could be used for input or output and if we have something like maybe 10 or 12 cabinets we're going to quickly max out the esp32 especially if we want to do things like attach some buttons or some additional sensors so how do we get around this limitation well we can use something called the i squar c bus on our ESP board and we can attach an mcp23017 Port expander but by using just two wires or two pens on our ESP board I can add up to 16 digital IO ports that alone is enough to cover the initial example with plenty of pins left over on our ESP board but what happens again if you got that 10 or 12 what if you need more than 16 digital ports well since this is iqu C you can run multiple devices on the same two wires so I can simply add another Port expander as long as it has a different iqu C address and if that's not enough well because the mcp23017 has addressable pins it can have up to eight different i squar c addresses we can continue to add additional expanders all the way up to eight for a total of 128 GPI pens again only using two pens on our ESP board now you might be asking how does this work in something like ESP home we'll get to that in just a second first let's take a look at the pinout of the mcp23017 it's actually pretty easy to use we connect our voltage and our ground to the vdd and vsss pins now this will operate off of either 5 volts or 3.3 volts since I'm outputting 3.3 volts on the GPI open pins to my ESP controller I'm just going to choose to operate everything at 3.3 volts next we hook up our two wires from our ESP board these are the only two pins we're going to need and that is our i squ c pins for data and clock over here are three iqu C address pins this what allows us to connect multiple devices to just those same two pins by taking each one of these pins and either raising it up to the operating voltage in my case 3.3 volts or connecting it to ground I can change the i squ c address by using the combination of these three that's what gives me up to eight different I sarc addresses and why I can add eight of these devices to those same two pins on my ESP controller next is a reset pin you can pull that load to reset the port expander normally you're going to connect that to your operating voltage pull that high if you wanted to you could connect a button or you could actually connect that to a different pin on your ESP board to actually be able to reset the port expander then you have the 16 digital input and output pins arranged in two Banks of A and B now those are arranged in two Banks of A and B because of these interrupts now what the interrupts do it allows you to detect aate change on any one of those pins in either Bank a or Bank b or they can actually be Ed together you might have a special use case for that I'm not going to be using the interrupt pins in my examples now there are some limitations with these Port expanders that you need to be aware of especially when it relates to current maximum current you can output on any single pin is 25 milliamps and all of your output pins combined can't exceed 125 milliamps be aware of that in my case where I'm actually going to be using LEDs I'm actually going to be using resistors to limit the current total output on any given Port EXP expander if you exceed this current odds are the Chip is going to overheat and eventually fail one other thing I should mention about this particular Port expander it is a digital IO expander that means your inputs and your outputs can only be binary on off true or false it won't work for things like temperature sensors or things that use analog values for that you need to use something like a multiplexer or a different type of Port expander but this will work well for anything that has an on or off State so how does this work in ESP home well ESP home has native support for the mcp23017 port expander as you can see here at the top the GPI pins in the data sheet which are labeled GPA 0-7 and B 0 to7 are mapped consecutively in this particular integration from 0 to 15 so we Define our mCP 2317 we tell it what I squ C address we're using this is what allows us to connect multiples and I'll show an example of that in a minute and then we just Define a normal gpio switch for our outputs or binary sensors for our inputs just like we always do the difference is underneath the pin when we would normally indicate a particular pin on the ESP board we tell it to use our mcp23017 and then we can use the PIN numbers 0 to 15 okay enough with the theory let's actually throw something together on a breadboard test this and take a look at the real ESP home code that makes it work power test setup I'm going to be using my benchtop power supply that I built out of an old PC power supply and it's nice because I can have more than one output so I'm out putting 5 volts to one bus and 3.3 volts out to another bus I'll explain why I can't run 3.3 volts off of the D1 mini here really has to do with drawing too much current but just know that I've Got 5 volts running to one bus and 3.3 volts running to another here's what I've created is a simple demo to show you how you can use the two pins to get multiple input output pins ends using this port expander now I'm just using a weos D1 mini an esp8266 really just to show you this can work fine with an esp8266 of course it will also work with the esp32 you would just need to substitute your I squared C pins on the esp32 quick little recap again of our pin out here I've got my 3.3 volts and ground coming into my port expander I've got my clock and my data which again is the only two pins I'll be using on the esp8266 these three yellow wires are setting my i squ c address for the chip right now I'm pulling all three of those to ground which gives me the default hex address of 20 my reset pin I'm pulling the high and then I have five inputs and five outputs connected all this is running es home and right now it's very simple all I'm doing is toggling each corresponding LED with the push button again you probably can't see the individual colors there because they're being washed out on the camera but you can see that I now have five inputs and five outputs from just these two pins on the esp8266 and I still have six inputs and outputs available here that I'm not yet using now I did mention that I'm powering using a separate power supply for my 3.3 volts versus my 5 volts you might be thinking why can't I power the 3.3 Volt Bus right off of this D1 mini well for this simple example you probably could because it's not going to pull that much current but if you start using multiples of these chips remember that each one of these could draw up to 125 milliamps if you Max it out and if you actually use all eight them that would be a full one amp and even in my case I'm planning on using five of them and that's going to be north of 600 milliamps which is really too much current to pull through this 3.3 volt pin so on my actual install I'll be feeding the system 5 volts but I'm going to use this little step down Buck converter and I can take my 3 or 5 volts and step that down to 3.3 Volts for this bus and this thing will handle up to 3 amps so it would be fine even if I wanted to use eight of these Port expanders but let's take a look at the simple ESP home code and how I set up these 10 inputs and outputs off of these two pins now the ESP home code that I'm using is very similar to the example I showed before I'm going to Define my i squar c bus and tell it what pins I'm using then I'm going to Define my mcp23017 I'm going to give it an ID that I can use for my inputs and outputs and then tell it which I squ C hex address I'm using remember I pulled all those pins to ground so mine is using a hex address of 20 now I just Define my inputs and outputs for my outputs I Define switches again I give it a name this is my red LED I'm telling it which mCP board I'm using by that ID number and I tell it which of those pins 0 to 15 or a0 to A7 B 0 to B7 that I'm using so I'm using pin b0 in this case so that is pin number eight I do that for all of my other LEDs and then I come down here to my inputs which are my push buttons and I basically do the same thing telling it which board I'm using or which Port expander I'm using and which pin number on that Port expander to use in my case I'm actually having an onpress automation that all I'm doing is is toggling on or off the corresponding color LED it's really that simple to add again up to 16 digital input or output pin off of just those two on the ESP keyboard need more than the 16 ports no problem you can add a second 16 Port expander connected to the same two data and clock pin wires from our esp8266 once again powered by 3.3 volts the only thing we do different here is on these address pins I'm taking this first pin and I'm bringing it high to the 3.3 volts while the other two are still at ground that gave this a hex address of 20 this now has a hex address of 21 be add off the same two pins I now have 32 digital input output pins available and you can continue to add on to those as much as you need all the way up to a total of eight of these 16 Port expanders what does it look like an ESP home when you have multiple port expanders well it's very easy we just add the additional Port expanders underneath our mcp23017 integration each one will have a different ID and you notice each one also has a different i squ c address then in our outputs we simply refer to which of those expander boards we're using and as you can see here I've got a switch for each of the three boards 1 two and three they're all three using the same pin number but they're referring to different port expanders in the same way on my inputs or my binary sensors I can use three different boards all of them using the same pin number but again it's referring back to the different port Port expanders and that way you can continue to add gpio pins or input output ports to your ESP board so now let's take the concept of using multiple port expanders to build that clock you saw at the beginning of this video now let me State this right up front and before you jump down to the comments this is not the best way to build a clock there are many many better ways to build a clock than what I'm doing here for one the final version here is pretty impractical why spoiler alert this is what the final wiring looks like yeah good luck hanging that on your wall but my purpose here is again to show you how to use multiple port expanders in my case I'm going to be using five port expanders to create a total of 75 inputs and outputs again off of just two pins on an esp8266 so while I will cover how I built this clock it isn't really meant to be a step-by-step how to for you to duplicate this project the goal here is for you to understand how you can use these Port expanders to add a lot of additional inputs and outputs for an ESP 8266 or an esp32 should you need that in one of your future projects so this idea came about a couple of months ago when I was browsing the local hobby store and ran into these little wooden clock faces they were about 2 and A2 bucks so I picked one up with no real idea what I was going to do with it when I started looking at Port expanders I thought I might be an opportunity to use this clock face to create a bunch of individual outputs for the minutes and the hours with no real idea of what I was getting myself into so I'll quickly cover the build of the clock face here if you're interested if not feel free to use the timeline down below to skip ahead but I started out by using a compass to create a circle for all of my minute LEDs would be installed I then used a digital angle finder to make a mark Every 6° Which would give me my 60 LEDs for the full 360° Circle I then drilled each of those holes out to accommodate 8 3 mm LEDs which is what I used for the minutes I then went ahead and cut a hole for my II 9341 which is really not a part of this project but since I can't ever seem to cut a nice square hole with a jigsaw I simply 3D printed a trim piece that would help cover up those rough edges I then went out and gave it a pretty crappy paint job I didn't spend enough time sanding between coats to make this look really nice but I was a little bit rushed once that was done and dried I flipped it over and installed the 60 LEDs for the minute on the back side I used bare copper to create a ground Loop that all of the ground wires from the LEDs would be soldered to that way I would only need one ground wire back to my controller as you can see from here I also installed the II 9341 it's just screwed to the back again as you can see this has its own controller if you're interested in that and how it works with the ESP home I do have another video on that but it's really not part of this project for the hour LEDs I 3D printed these little enclosures that would help stop the light from bleeding over from one number to the next I simply glued those into place and also attach attach those ground wires to my ground Loop and that pretty much completes the build of the clock face itself so now I'm ready to move on to talk about the clock controller these are all the power connections for the five port expanders and my wios D1 mini now all these three power rails are all 3.3 volts this is 5vt but it's only going to power this for each Port expander just like I shown in the wiring diagram we've got 3.3 volts in ground powering it and over here the reset pin is pulled higher to 3.3 on all of the chips and then we vary the ground versus the 3.3 connections on these first three pins for the address so just in order the hex address for each of these are going to be 20 21 22 23 and 24 those are all of the 3.3 5vt and ground connections here's the board with all the i squ c wiring completed once again we're just using two pens off our ESP board a data and a clock signal the blue wire is the data signal and the yellow wire is the clock signal running to each of the five port expanders so this wiring right here I now have 80 digital inputs or outputs across these five port expanders and the only thing really left to do is to wire in whatever inputs or outputs that I want in my case having a that's enough for my 72 LEDs for the clock face plus maybe three or four push buttons to add some optional controls and here's what's quickly becoming a Franken clock but this is all the wiring for all of the outputs that'll go to the actual clock itself again 72 LED controls UH 60 for minutes and 12 for hours will be controlled by these five chips with three input buttons as well so now the remaining part is to actually make the connections for the outputs to to the clock itself again there are much better ways to build a clock but all I'm really doing here is exploring the use of these mcp23017 16bit Port expanders and this just seems like a good way to do it now my goal is to eventually move all of this to a PCB is one of the things I've wanted to learn one of the things I've not done before but I do plan on trying to move all of this and designing my first PCB and shrinking this down small enough that I can put it on the back of the clock and you saw this picture before my spoiler alert it was quite a challenge getting all those wires uh connected and into the right spots on the breadboard and again my hope is to eventually create a custom PCB and eliminate a lot of these wires but for a breadboard bench test it actually works let's take a look at the final build now that I have all of the LEDs wired and I have 72 outputs and three inputs so go ahead and kick this off first thing it's going to do is it's going to run a little system boot check just to check all of the LEDs so the minutes and then the hours and then what it's going to do is it's going to display the current time now the hours are a little bit brighter when you look at it straight on but these LEDs are so bright it just overwhelms the camera so I'm shooting at an angle and of course the II 9341 display here in the center will eventually display the current day of the week and the date but that has its own controller and is really separate so it's not really practical but it is a good example of using these Port expanders and I am running 75 inputs and outputs off of just two pins on my weos D1 mini esp8266 so that's my LED clock as I told one of my subscribers on Discord this would be what rub Goldberg would create if he were to create a clock but again the idea here was to show you the potential uses of Port expanders just some ideas that popped to mind would be something like maybe a model railroad situation where you had a bunch of individual LEDs that you wanted to be able to turn off or on in a situation where something like an addressable LED strip just probably wouldn't work very well or some place where maybe you have a whole panel of input push buttons that you need to use so let me know down in the comments if you've used these Port expanders in your own projects or if you have ideas of where they might be useful and if you've seen anything in this video that you like do me a favor and hit that thumbs up button click the Subscribe button if you want to see more of my videos ending the little bell icon if you want be notified when I release new content as always I'd like to say thank you for watching and I hope to see you [Music] soon
Info
Channel: ResinChem Tech
Views: 10,503
Rating: undefined out of 5
Keywords: ESP8266 Port Expander, ESP32 Port Expander, ESP8266 Pin Expander, ESP32 Pin Expander
Id: GyHiSyoyk_0
Channel Id: undefined
Length: 20min 39sec (1239 seconds)
Published: Sat Oct 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.