Arduino Interfacing
From Hamsterworks Wiki!
Connecting your Arduino to the outside world is pretty easy.
Connecting switches
Switches are easy - just wire the switch between the pin and Ground. The ATmega chip has a 20K pullup resistor that can be turned off and on in software (great for saving power!). These built-in pullup resistors are enabled by setting the pin to "HIGH" even through it is configured as an input pin:
pinMode(pin, INPUT); // set pin to input digitalWrite(pin, HIGH); // turn on pullup resistors
Connecting LEDs
As long as you get them the correct way round low power LEDs are easy to interface - the longer wire connects to the positive side of the circuit and include a resister in series with the LED to limit the current flowing in/out of the I/O pin.
Ignoring the voltage drop over the LED the following resisters are a good starting point:
| Current | 3.3V | 5.0V |
|---|---|---|
| 5mA | 660 Ohm | 1000 Ohm |
| 10mA | 330 Ohm | 500 Ohm |
| 15mA | 220 Ohm | 330 Ohm |
| 20mA | 165 Ohm | 250 Ohm |
| 30mA | 110 Ohm | 165 Ohm |
A more precise formula is:
Resistor = (Supply Voltage - LED Forward Voltage) / LED current
As the LED will have a voltage drop over it, you can safely use the next lower standard value without any issue. Some short wavelength LEDs have a forward voltage drop much greater than the 0.6V of red LEDs - for example a AL-314B1C-00 blue LED has a voltage drop of 3.3V so is nearly impossible to reliably drive on a 3.3V system.
Switching higher powers
Any current than 40mA can not be switched directly by the micro-controller. Instead you need a transistor to boast the current that can be switched.
For really high powers it is best to drive a relay.
In this example the 12V relay coil is 160 Ohm, so current needed to close the relay is 75mA
The transistor has a minimum Hfe ('current gain') requiring at least 2ma into the base. For a 3.3V signal (less the emitter-to-base-base voltage of 0.6V) the drive resistor needed is (3.3V-0.6V)/0.002A = 1350 Ohms. Next smaller standard value is 1,000 Ohm.
It is most probably safe design practice to use a smaller value (like 470 Ohm) to ensure that the transister is saturated, but in this design it doesn’t matter.
The addition of a heatsink and the 10 Ohm resistor is a good idea just in case you install the protection diode the wrong way round. Without it you will almost certainly destroy the transistor!
