# variable resistor as a switch?

say i have a pot that is putting out 3.15v at its minimum. would it be possible to run that voltage in to a digital input and have it act as a switch for anything above the 3.15v?

if so, how would you go about doing that?

i have been trying to wrap my brain around it all morning.

i've been trying funky hardware solutions, trying to add resistors to drop the 3.15v and such but it isn't working. it seems like there must be a solution with code. :-/

good news update! i got the variable resistor working from a lower voltage source so the code i setup is working.

but i still need to figure out how to get it working with the higher voltage source.

what voltage is a digital pin looking for to trigger a switch? does under 3.3v translate to off and anything above that = on?

The Digital pins are looking for about 5V. I think it will register as low as 4.5V but i’m not sure. I’m pretty sure anything under 2V will register as off. It may be less then that.

Why are you trying to use a pot like this anyway. You know you can get pots that have on/off switches built in if your looking to make and on.off with volume control kind of a deal.

If you have to use a pot to turn things on and off then use it threw the analog pins. Set it up on a analog pin and have what is read from the pin sent out threw the serial monitor to see what values you get. Then use the upper value you want and lower values you want (+/- a few points) to designate on and off.

3v will trigger a high condition on 5v based stuff, 3v based stuff can go lower than that

I would consult the atmega datasheet

it isn’t actually a pot but the touch screen i have been working with (i thought it might be easier if i just described it as a pot).

it is currently already sending the 3.15v in to the analog0 pin, it would be nice if i could just add the code to make it react as a switch there.

ideally i want the 3.15v to send a midi message of:
sendCC(22, char(0)); // send CC message 22

then for anything above 3.15v i would have it send:
sendCC(22, char(127)); // send CC message 22

the problem is, i have no idea how to add the on/off feature to the analog0. i have only dealt with digital switches in my limited experience.

The analog pins 0-3 are all being used for the screen and pins 4-5 are reserved for the BlinkM. i hope adding the on/off feature to pin 0 isnt an issue.

thanks!

Basiclly you build an if() statement looking for the values reported by the analog pin. You’ll want to get a high and a low value as the reading will fluctuate a bit.

``````if (analogRead() > lowValue && analogRead() < highValue)
{
sendCC(22, char(0));  // send CC message 22
}

{
sendCC(22, char(127));  // send CC message 22
}
``````

Something along those lines anyway.

Well, mike beat me, but here’s my post anyhow:

i have no idea how to add the on/off feature to the analog0

All you have to do is check the value, like so:

Above a value:

``````if (analogRead(pin) > value) {
// do something
}
``````

Below a value:

``````if (analogRead(pin) < value) {
// do something
}
``````

Between values:

``````if (analogRead(pin) > value1 && analogRead(pin) < value2) {
// do something
}
``````

(not that i am claiming to fully understand the situation) but maybe you just need to use some if statements to see if the analog value is below a point

ie

if(in <= target) do nothing;
else do something;

thanks guys! i will give that a shot and report back, hopefully with good news!

thank you all so much for the info!

you guys are AWESOME!!!

thank you so much for the help, the screen is up and running as it should now! ;D

In general it's risky to drive a logic (digital) input (other than a Schmitt-trigger) with an analog voltage.

Fortunately the ATmega does seem to have Schmidtt trigger inputs (although can't find the exact spec for them).

However its a bad habit to get into as feeding a high-impedance or slowly varying analog signal into a logic gate can cause oscillation or switch on both the NMOS and PMOS FETs in the gate causing large currents to flow continuously.

Perhaps the best way to interface slow analog signals to digital inputs is with a comparator chip - these are designed specifically to do the job and allow you to setup exactly the threshold voltage and hysteresis you want. Alternatively an op-amp followed by a Schmitt-trigger will work, but with the Arduino the analog input pin is the most obvious solution!