Thursday, December 30, 2010

Arduino Pins - Digital pins

For an Arduino beginners, to talk to microcontrollers, you interfaces sensors, actuators and chips (integrated circuit) using pins. There are a few types of pins, input output (I/O) pins : digital pins & analog pins  and other pins like power pins (+5V, +3.3V, Ground ) and others like analog reference, reset and crystal pins. Some of these pins are not accessible on an Arduino board but need to be wired up for an Arduino breadboard version.  Let me explain each of these in more details.

ATMega 168/328 pinout

Arduino Digital Pins 

Digital pins are much easy to understand as there are only two state, either ON or OFF state. In Arduino sketch terms, a ON state is known as HIGH (5V) and OFF state is known as LOW (0V).

You can use these digital pins either as input or output pins. Use the function pinMode() to configure the pins either to INPUT or OUTPUT. The default mode is INPUT if you do not specify them using pinMode().

Sample Code :
pinMode(12, OUTPUT);   // Set digital pin 12 as OUTPUT pins

For advance users, you can use AVR port registers to manipulate the pins using low level method describe at :

When you set a digital pin as HIGH, you send a +5V to the pin and when you set the digital pin as LOW, the pins is pulldown to 0V. You can verify this with a multimeter (red probe to the pin to be tested, black probe to the ground)  or put a LED and resistor between the pin and the ground pin.

The first Arduino tutorial, "Hello World" or Blink tutorial does just that with a delay() in between.

Use the function digitalWrite(12, HIGH) or digitalWrite(10,LOW) to set the state of the pin.

There was once, when I was doing my realtime clock project to have the pin 13 blink every second ( turn on for 500 millisecond and turn off for 500 millisecond ). 


After a restart Arduino, I notice the LED on pin 13 was very dim.  As I did not change the resistor value, I was wondering why the LED at pin 13 became so dim. After reviewing the codes, I notice that I did not put the statement : 

pinMode(13, OUTPUT) in setup().

The default mode of the pin was INPUT. As INPUT mode, with digitalWrite(13,HIGH), it have activate an internal 20K pull-up resistors and lit the LED dimly. Changing the pinMode(13, OUTPUT) solved this issue.

All Arduino pins as OUTPUT mode will send a maximum current of 40mA, enough to drive a LED, LCD modules, opto-isolators or low powered sensors  but not high enough current to drive coils, relays or motors. Always refer to the datasheet for the maximum value the sensors or actuators can accept.

Digital Pin as INPUT

With pinMode() set as INPUT, you can use digitalRead() function to get a status of the pin.

The easiest way to explain this is reading a status from a push button / tactile switch.  

Push button with pull-up resistor to 5V

Digital Pin 3 is connected with a 100 ohm resistor to another 10K ohm pull-up resistor to +5V pin. With the +5V and 10K resistor, the default state of Pin 3 will be HIGH when you read it with digitalRead() function. On the same circuit, a wire connect to a push button to the Ground pin (Orange wire).

When the push button is pressed, due to lower resistance, the circuit is grounded and the state of Pin 3 would be LOW or 0V.

It is easier to hold the pin at HIGH state using pull-up resistor like 10K ohm than a LOW state. An example to turn on and off a LED using the push button at default HIGH state. Always have input pins in a known state ( pull-up or pull-down ) instead of an unconnected state to prevent any intermittent issues.

Pin 3 is connected like the above example.

pinMode(3, INPUT);

 if ( digitalRead(3) == HIGH  ) { // Button not pushed
        digitalWrite(13, LOW )        // Turn off the LED
 } else {                                          // Button is pushed
        digitalWrite(13,HIGH)         // Turn on the LED

Switches without a resistor trick

Like the above example, if you do not want to use a resistor between the switch/push button, you can do this neat feature by utilizing the ATMEGA328 internal 20K pull-up resistor.

The command to activate the internal 20K pull-up resistor is :- digitalWrite(HIGH)

Arduino switch without resistor

Using Digital pin 3 as an example :-

pinMode(3,INPUT);            // default mode is INPUT
digitalWrite(3, HIGH);     // Turn on the internal pull-up resistor, default state is HIGH
state  = digitalRead(3);      // read the state of the pin

For the programming logic, when the switch is OPEN, the state is HIGH and when the switch is CLOSED or pressed, the state is LOW. 

There is another neat Arduino trick to read two switches using only one digital pins at Arduino reference site. It is a combination of the above two examples.

1 comment:

  1. Usefull pinout atmega/arduino picture, it has every pwm pin available mapped.




Related Posts Plugin for WordPress, Blogger...