Interfacing To The Arduino, Simplified.: Digital Inputs
Interfacing To The Arduino, Simplified.: Digital Inputs
Digital Inputs
The digital inputs on the arduino are TTL. This stands for Transistor-to-Transistor Logic. The simple explanation is that when one of these inputs is connected to +5 volts, the logic level is HIGH. When it's connected to GROUND, the logic level is LOW. The problem is that if the pin is not connected to anything, the logic level is undefined. Any noise, radio waves, or other stuff will cause the state to change. So, this is why a switch connected to a TTL input needs a resistor to 'pull' the input UP, or to pull it DOWN, when the switch is open. A PULL DOWN RESISTOR
This way, when the switch is OPEN, the resistor is connecting the input to Ground, guaranteeing a LOW. When the switch is closed, the switch is holding the input HIGH, and the resistor is still there. Now the resistor is connected directly from 5 volts to ground. For a normal wire, this would cause a short circuit. However, our pull down is a safe value to connect directly from 5 volts to ground, so there is no short circuit. Here is a PULL UP:
Potentiometer (Three terminal variable resistor) The potentiometer is connected from 5 volts to ground, with the variable terminal connected to the analog input. The knob outputs a voltage from 0 to 5 volts, giving the analog pin a value of 0 to 1023 proportionally.
Variable Resistor Sensors These are sensors that change their resistance, such as light sensors, temperature sensors, or knobs with only two wires. They need a fixed resistor to make a VOLTAGE DIVIDER. The output of the voltage divider will go to the analog input. The voltage will be between 0 and 5 volts to give a proportional analog value of 0 to 1023. Note that the fixed resistor should be safe to connect from 5 volts to ground, so it must be a MINIMUM of 250 Ohm.
If the variable resistor is R1, and the fixed resistor is R2, Then the output voltage is Vout = R2 ------------------- X 5 R1 + R2
To get the analog value this will give you, just replace the 5 with 1023. TIP: I usually don't bother with this. I just find a safe resistor to use as R2 and then find out what the values are by testing and then take it from there. The range is usually good enough.
The conversion is: KNOB OFFSET1 Dimmer Val = ----------------------- X SPAN2 + OFFSET2 SPAN1 (Note that you add OFFSET2 last.) So if Fred's Knob reads in at 212, then 212 28 ---------- X 680 , plus 32 = 344.8 400 This formula will map the knob to the dimmer very nicely. HOWEVER, since we're working with INTEGERS in the arduino, we have to do it in a different order. We could use decimals, but there are all these conversions, so to keep it easy we'll just do the division LAST. dimmer = ( (212 28) * 680); dimmer = (dimmer / 400) + 32; This will give you a nice integer value, rounded down. If you divided first, the arduino would round THAT, and then it would be either 0 or 1 time 680, so you'll get either 32 or 712, which are hopelessly wrong. If you want to convert to decimal and back again, see the arduino reference.