across the globe hundreds of thousands of Arduinos are controlled by potentiometers mindlessly obeying their will these poor souls have no say in what values they will be presented on their analog inputs but today Arduino fights back today Arduino will control the potentiometer with the help of a tiny chip called x9c103s also known as digital potentiometer welcome back so what is this chip and how are we going to use it let's take a look this thing has eight pins a power supply between VCC and VSS free control inputs and we have three potentiometer pins two ends and one wiper this thing has no mechanical parts inside how does this work so we have 99 resistors and 100 wire tap points as soon as a transistor turns on it connects this wiper tap point to the external pin and using this control inputs you can now control which part of the resistor network will be connected to your wiper pin also very important we have a nonvolatile memory inside so even if you remove the power this thing still remembers its last state just like your mechanical potentiometer it should be possible to measure the resistance between this pin and this pin I have the X9C103S so it should be around 10 kΩ however this chips comes with an end to end resistance of +/- 20% 10 kΩ would be perfect but it should be somewhere between 8 and 12 I have 9.5 what do you think will happen if I measure the resistance between one end and the wiper think about it for a second 2.7 MΩ so it's not really connected because we have no power supply unlike your traditional potentiometer this one requires a power supply to connect the wiper to your resistor Network because the transistor needs to be turned on and that requires gate voltage and this also brings me to an interesting point in this diagram it's not clear if it's an N Channel or a p Channel mosfet but my guess is it's an N channel in order to turn it on you need a positive voltage between source and gate so gate needs to be above Source this in turn would limit the voltage because as soon as you would connect a voltage to the wiper that is 5 volts you need a voltage higher than 5 volt to turn on the transistor inside of this transfer gates and they don't tell you much about it but there is a small hint and that is this paragraph So the internal charge pump allows a wide range of voltages from minus 5 volts up to 5 volts so this chip creates a voltage higher than the supply voltage so you can connect any of these potentiometer pins to any voltage between Min -5 and + 5 Volts now this charge pump operates at a frequency of 850 khz so it creates a little bit of noise in this frequency range one very important thing to keep in mind is the very limited power rating of this device we only have 10 mW so try not to push it too hard the resistance of this resistor array are very very tiny if you want to power anything serious with it you need an amplifier as already mentioned we have three inputs a low active chip select a low active increment and up or down selection so if it's high then it's up and if it's low then it's down also one thing to note is it's not possible to read out the value of this potentiometer so in most applications Arduino will have to remember the state of the digital potentiometer and this should also be nonvolatile so we should store it inside of our EEPROM probably if you would like to play around with one of these yourself you can find a link to this product in the video description the next step is to connect it to my Arduino Uno in order to connect this chip to my Arduino I will solder it to a PCB and then put it onto a breadboard I already made a video about this process so you can check it out if you're interested [Music] if you like your breadboard don't do the next thing my breadboard is already very disgusting so I will just put it in there but this of course damages the breadboard so don't do this on your new shiny breadboard let's connect them ground on pin 4 5 volt Supply voltage on pin 8 now the potentiometer should actually start working the charging pump should work inside and it should actually turn on a transistor inside so let's see if that happens if I measure between wiper and low I see a resistance of 9.5 kilohm and if I go between
wiper and high it looks like the wiper is at the very top 20 ohm is the wiper resistance the typical wiper resistance is 40 ohm I measure 22 that's perfect so we are at the very top in order to control our digital potentiometer we need to connect these three pins let's connect increment to pin 10 const INT in increment pin pin 10 const int up down pin is pin and chip select pin is pin 8 we will also connect the output to an analog pin so that we can measure what is going on I will connect the top of the resistor array to 5 Volts and the bottom of the resistor array on pin six will be connected to VSS so ground so now it acts as voltage divider between ground and 5 Volts and we can get this voltage on the wiper pin and and I will connect it to analog input zero now how can we use this oh I need to fix that microphone one way of doing it would be to code it ourselves however I'm pretty sure someone already used this potentiometer and so I will search for libraries that looks pretty good let's install the very first one digipot and usually it comes with examples okay very simple let's copy everything that we need we include this Library we initialize a digipod object with increment pin up down pin chip select pin we also need a serial connection and then in inside of my loop actually it's a pretty good idea to do exactly that but I will also add the analog readings of course let's do serial. print A0 equals and then serial. print analog read A0 so now we should see the increase and the decrease all right this looks great so we're increasing and then we're decreasing again perfect it's exactly what we expect we can also measure the output voltage between ground I take it from here and pin five as you can see the voltage is increasing up to 5 volt and then it's decreasing again however the problem is Arduino doesn't remember we increase to the maximum and then I press restart then it starts again as you can see it's now increasing and now nothing happens on the output because it's already at the top now if it doesn't matter in your application this is also one way of catching up with the potentiometer and that is if the Arduino starts you just increase it 100 times and then you're sure that you are at the top in fact you can increase it 99 times but of course it's not always possible to just increase it it depends on the application but this is pretty boring right now we're just going up and down I would like to control it I would like to tell my Arduino jump to 20 enter and then it should jump to 20 how can we do that well first we need serial read it looks like this we need an string object called input string and it's empty at the beginning and it's not complete at the beginning and then we reserve a number of bytes for this input string 200 is a lot so we will not need 200 so let's copy the string we need to reserve some memory 200 is a lot I would use maybe 20 so then inside of a loop we check if the string is complete and if the string is complete then we do something and then we set it to false again so we will not print we we can print it yeah we can also print it let's let's copy it can also print it I will remove this for now and add this stuff here if the string is complete I will output the string and string complete is then false and we can capture a new string and this stuff right here we can use exactly as it is every time Arduino gets serial data serial event is called and you can Define it if you want to so in this case it checks if serial data is available and then it reads the next character and it adds it to its input string and if there is a \n that signals the end of the line then the string is complete so let's see what happens hello hello okay yeah very nice of you are you know um how are you now as you can see it just replies the same thing that I sent of course because that's all I do here we can now use this to control our potentiometer so first we need to convert it to an integer int Target value equals how can I convert a string into an INT dot toint and we can see if it works by printing the target value as decimal number let's see so 111 111 hello zero okay so if I put 1H1 it's 1 if I say 1h8 it's 1 [Music] 17three 17 okay so it takes whatever number comes first and if I say e2 E4 that's 0 okay so if it starts with a number it takes the number and else it's zero okay the great thing is I enter a number and it converts the number into an integer how can we now move to this target we need to increase it if our Target is higher and we need to decrease it if our Target is lower how do we know if the target is higher or lower we need a new variable let's create a new variable pot value equals zero because uh we will start at zero um and to be sure that the potentiometer is also at zero we need this code inside of our setup of course we can increase the speed and we can say decrease potential meter to zero so now inside of our setup we decrease it 100 times we set it to zero and then we know that the pot value is actually the true pot value because you never know we can't read it and then let's do something like this while Target value is not pot value if Target value is bigger than pot value so if the target is bigger than the pot value we need to increase and we do it like this and else we decrease so this would be one way of doing it or we input the number this would also work so because if we do it like this we need a a a while loop so while we're not exactly there we increase increase increase increase or we do something else we can also if Target value is bigger than pot value we increase it by the difference and we do it the other way around but we need to check if it's actually smaller because maybe it's the same let's keep this in our Loop and add a small delay of 100ms yeah we need a new line here A0 is zero so we start at zero that's great and now I would like to set it to 20 and it's set to 200 so if I go to 100 I go to the top 50 is okay decreasing doesn't work let's debug this okay so I can increase the value to up to 100 but I can't decrease it because it doesn't jump into it interesting my target value is 50 but this is not triggered ah because we need to update the pot value so after this we can say with confidence pot value equals Target value 100 boom 50 oh yes 100 zero that's how it should work okay we can remove this debug information here and this is exactly how it works awesome so now the potentiometer is at 1023 and if we restart it it goes to zero and by the way we don't really need this for Loop here we can also decrease it 99 times we don't need this so now we're at zero I go to 100 and I restart my Arduino and we jump to zero yeah that works the last step is to utilize the EEPROM so let's also utilize the EEPROM we go to examples eom now the EEPROM is the nonvolatile memory that you can use in your Arduino and every time I change the value of my potentiometer I would save it to the EEPROM so let's check EEPROM read so first thing we need to include the e prom library and then we read the value pot value equals eom read let's use zero one issue is that right now there is something inside of my EEPROM and most likely it doesn't correspond with whatever is stored inside of the potentiometer so for the very first time I will also set this to zero let's go to examples EEPROM write so just for the very first time address zero value Zero and then and now the potentiometer is set to zero and the EEPROM is also set to zero and now we can remove the write and we can also remove the decrease so from now on I just read back whatever is stored inside of my EEPROM and inside of this we will store the new pot value inside of our EEPROM as well one last thing we need to check if the input is valid if Target value is bigger or equal to zero and Target value is smaller or equal 100 then we do all of this stuff here let's try this all right so if you set it to zero it will go to zero if you set it to 100 it goes to 100 and if you restart it it stays at around 100 I think it jumps down by one interesting oh every time I restart it it jumps down just a tiny bit that's not good why so maybe it is yeah okay makes sense the maximum amount is 99 because zero is also one value yeah okay makes sense so should be between 0 and 99 actually mhm this makes sense yeah let's go to zero and now go to 99 yeah and now we're at Max 0 99 is the maximum 50 is somewhere in between yeah a little bit higher now I think it's perfect but let's see if it stays no no okay if I reset it it still goes down it's probably the pins that are toggling chip select is low at the very beginning so it should be possible to fix this by using a pullup resistor on the chip select now I I put in a pullup resistor let's see if that helps still decreasing that's a tragedy let's check the library open folder documents Arduino libraries this stuff right here uh do I trust yeah okay they're setting it yeah they're defining it as output and then they're setting it high so it's decreasing right here that's not good we need to change this yes! okay if you want to store the value inside of your e prom don't use this library because this has a bug obviously or or fix it yeah you can fix it by putting digital right before setting it as output because then chip select stays High otherwise it is low for a very short amount of time and that decreases the value but to be sure I would also add a pull-up resist on a chip select line as soon as you press the reset button Arduino gets unconscious and releases all its outputs so the chip select pin will just float around which is not great it might trigger the increment unless there is a pull-up resistor already inside maybe? No! okay there is no pull-up resistor definitely add this 10 kΩ pullup resistor between 5 Volts and the chip select line that helps let me try one last thing I would like to connect the low end to 5 Volts and the high end to 0 volts it should also work or it just dies let's see so this is the high end I will connect it to ground all right looks good and now everything should be inverted if I go to 100 uh 99 sorry and zero is now the maximum 99 is the minimum and if we go to 50 we are somewhere in between and 99 you yeah that also works and the microphone is already coming into the picture again if this video was helpful please like And subscribe to this Channel and if you would like to see a real world application of a digital potential meter check out my ESP32 power over ethernet project did you already use digitalpotentiometers in the past? which one? let me know in the comments down below thank you for watching see you in the next video
2025-02-04 02:04