In order to have enough pins, I'm using a multiplexer to get input from 7 potentiometers. Since timing isn't super important with the routine, I was thinking of adding a couple momentary push-button switches and 4 SPST toggle switches to the multiplexer since I have plenty of pins open there.
The question is, do I need to put a resistor in the circuit or since it's running through the multiplexer and into the analog input is it fine to wire the switch direct from +3.3v through the switch, and to the multiplexer?
since i've never done this, i'll assume you can use pinMode to configure the pin with a pullup resistor (i.e INPUT_PULLUP) and connect a switch to ground. The switch will make the pin LOW.
do I need to put a resistor in the circuit or since it's running through the multiplexer and into the analog input is it fine to wire the switch direct from +3.3v through the switch, and to the multiplexer?
i'm not sure what you're connecting to the analog pin? is the switch connected to both the analog pin and a multiplexer pin?
i'm not sure what you're connecting to the analog pin? is the switch connected to both the analog pin and a multiplexer pin?
Nope, the switch pins would just be connected to the multiplexer. I wouldn't be setting the analog pin as a digital input either, but rather what I am assuming, (possibly wrong here), is that when pushing a switch or toggling a switch to high, the analog pin would read 1023 at 3.3v which is my reference voltage. I was thinking that no resistor would be necessary when using the pin this way, but I don't want to damage the board and this isn't a regular use case I wouldn't imagine so I figured I'd ask... I suppose I could put a resistor there and it would simply read less than 1023 and I can code to check for whatever value it ended up being, but if it's not necessary and won't hurt anything, I'd rather not.
What do you get from the multiplexer when the selected input is floating (momentary switch is open)? How are the toggle switches wired to the multiplexer?
Thank you all for the help. It turns out that I did need to put in a resistor so that the switch grounds when low and then when it goes high, the multiplexer reads "1023". It just gives me an ability to get switch input without taking up the already scarce digital pins. When not grounding the switch through a resistor, I suppose in a 'floating' state, the values go all wacky. A little experimenting taught me the lesson. I was just a little fearful that by experimenting, I might damage the board. Everything worked out fine though.
Basically, I have +3.3v to one side of the switch and the other side of the switch it's wired to the multiplexer. A 10k resistor connects between the switch output and the multiplexer and the other side of the resistor connects to ground. This allows the multiplexer to be grounded when the switch is open. Works well this way. It's easy enough to check for a "1023" value in the code to indicate that the switch has been pushed. Thought I'd share this and if perhaps someone else needs the question answered, the information would be here.
If anyone finds something wrong with my solution, please feel free to chime in and let me know.
I wouldn't be testing for the analog input to be 1023 when the button input is high, instead pick a value about midway (512 in this case) and check for the value > 512 as a high or <512 if testing for low. That way if you input is slightly off for some reason and reads 1022 instead of 1023, it will still get recognized as a high.
You can connect MULTIPLE switches (6 or more is not uncommon) to a single analog input, using resistors so each one, when pressed, generates a different voltage at the pin....
david_2018:
I wouldn't be testing for the analog input to be 1023 when the button input is high, instead pick a value about midway (512 in this case) and check for the value > 512 as a high or <512 if testing for low. That way if you input is slightly off for some reason and reads 1022 instead of 1023, it will still get recognized as a high.
Yeah that's exactly how I am doing it. I mapped the values down to 0-255 and then check for values > 128. It works perfectly.
RayLivingston:
You can connect MULTIPLE switches (6 or more is not uncommon) to a single analog input, using resistors so each one, when pressed, generates a different voltage at the pin....
I have a matrixed button pad that does exactly that. For my application, since I already had extra multiplexer pins, it seemed like a good idea to go ahead and use those since I already had a routine to read them.
I've had it working fine for a few days. It works exactly like I hoped it would and without issue. I have it wired to the multiplexer the same standard way you would wire a switch to a digital pin. There's no point where the multiplexer input pin can be in a floating state. In that regard, it's working fine. I'm having issues with noise in my multiplexer when using the I2S library on a MKRZERO. I'll make a separate thread for that though. Thank you all for helping me out. I hope one day to return the favor on here as I continue to learn more.