Vous êtes sur la page 1sur 3

Interfacing to the Arduino, Simplified.

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.

Analog Values: SPAN and OFFSET.


An analog value covers a range of possible values. The minimum value is not always zero. The range of change from minimum to maximum is called the SPAN. The amount that the minimum varies from zero is called the OFFSET. So, if you have something that gives you a value of 5 to 25, that would be a SPAN of 20 and an OFFSET of 5. Span = Max Min Offset = min. This is important because you'll often convert between two different analog values that have different spans and offsets. Example: Fred has a special knob that gives him a value of 28 to 428. This is just how the knob is, and it's the one fred wants to use. It has a span of 400, which should be enough. Fred is trying to drive a dimmer from his arduino that takes a value of 32 to 712. It's an odd dimmer, but fred doesn't care about the actual numbers. He wants the minimum knob value to map to the minimum dimmer value, and the maximum knob value to map to the maximum dimmer value. Here are our spans and offsets: KNOB SPAN1 = 400 OFFSET1 = 28 DIMMER SPAN2 = 680 OFFSET2 = 32

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.

Vous aimerez peut-être aussi