Arduino function libraries~I/O Function
Arduino function libraries~I/O Function {Tutorial 13}
I/O Functions
Basically, all pins on the Arduino board can be configured as either outputs or inputs . Note that a majority of Arduino analog pins, may be configured, and used, in exactly the same manner as digital pins.
As INPUT
By default all arduino Pins are configured as inputs.
This means that it takes very little current to switch the input pin from one state to another. This makes the pins useful for such tasks as implementing a capacitive touch sensor or reading an LED as a photodiode.
Pull-up Resistors
Pull-up resistors are often useful to steer an input pin to a known state if there is no input. This can be achieved by adding a pull-up resistor (to +5V), or (resistor to ground) on the input. A 10K ohm resistor is a good value for a pull-up or pull-down resistor.
In-Built Pull-up Resistor with Pins Configured as Input
In-Built Pull-up Resistor can be accessed from software. These built-in pull-up resistors are accessed by setting the pinMode() as INPUT_PULLUP. This effectively inverts the behavior of the INPUT mode, where HIGH means the sensor is OFF and LOW means the sensor is ON. The value of this pull-up depends on the microcontroller used. On most AVR-based boards, the value is guaranteed to be between 20kΩ and 50kΩ. On the Arduino Due, it is between 50kΩ and 150kΩ. For the exact value, refer datasheet of the microcontroller on your board.
When connecting a sensor to a pin configured with INPUT_PULLUP, the other end should be connected to the ground. In case of a simple switch, this causes the pin to read HIGH when the switch is open and LOW when the switch is pressed. The pull-up resistors provide enough current to light an LED dimly connected to a pin configured as an input.
Sketch Example
pinMode(5,INPUT) ; // set pin to input, built in pull up resistor will be ignored. i.e pin = 5 ,Mode= INPUT pinMode(7,INPUT_PULLUP) ; // set pin to input using built in pull up resistor. i.e pin = 7 ,Mode= INPUT_PULLUP
As OUTPUT
Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means that they can provide a minimal amount of current to other circuits. This is enough current to brightly light up an LED (do not forget the series resistor), or run many sensors but not enough current to run relays, solenoids, or motors.
pinMode() Function
The pinMode() is a function used to configure a specific pin number to represent either as an input or an output. It is possible to enable the internal pull-up resistors with the mode INPUT_PULLUP. Additionally, the INPUT mode explicitly disables the internal pull-ups.
pinMode() Function Syntax
Void setup () { pinMode (pin , mode); // pin − the number of the pin whose mode you wish to set, mode − INPUT, OUTPUT, or INPUT_PULLUP. }
Sketch Example
int button = 7 ; // button is connected to pin number 7 int LED = 8; // LED is connected to pin number 8 void setup () { pinMode(button , INPUT_PULLUP); // set the digital pin as input with pull-up resistor pinMode(button , OUTPUT); // set the digital pin as output } void setup () { If (digitalRead(button ) == LOW) // if button pressed { digitalWrite(LED,HIGH); // turn on led delay(500); // delay for 500 ms digitalWrite(LED,LOW); // turn off led delay(500); // delay for 500 ms } }
digitalWrite() Function
The digitalWrite() function is used to write a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor.
If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim. Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor.
digitalWrite() Function Syntax
Void loop() { digitalWrite (pin ,value); }
- pin − the number of the pin whose mode you wish to set
value − HIGH, or LOW.
Example
int LED = 6; // LED connected to pin 6 void setup () { pinMode(LED, OUTPUT); // set the digital pin as output } void setup () { digitalWrite(LED,HIGH); // turn on led delay(500); // delay for 500 ms digitalWrite(LED,LOW); // turn off led delay(500); // delay for 500 ms }
analogRead( ) function
Arduino is able to detect whether there is a voltage applied to one of its pins and report it through the digitalRead() function. There is a difference between an on/off sensor (which detects the presence of an object) and an analog sensor, whose value continuously changes. In order to read this type of sensor, we need a different type of pin.
In the lower-right part of the Arduino board, you will see six pins marked “Analog In”. These special pins not only tell whether there is a voltage applied to them, but also its value. By using the analogRead() function, we can read the voltage applied to one of the pins.
This function returns a number between 0 and 1023, which represents voltages between 0 and 5 volts. For example, if there is a voltage of 2.5 V applied to pin number 0, analogRead(0) returns 512.
analogRead() function Syntax
analogRead(pin);
- pin − the number of the analog input pin to read from (0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega)
Example
int analogPin = 3;//potentiometer wiper (middle terminal) // connected to analog pin 3 int val = 0; // variable to store the value read void setup() { Serial.begin(9600); // setup serial } void loop() { val = analogRead(analogPin); // read the input pin Serial.println(val); // debug value }
Summary
pinMode() sets the pin as INPUT or OUTPUT digitalWrite() writes a value out on a pin (if output), or enables the pullup resistor (if input) digitalRead() reads a value in from a pin analogReference() configures the reference voltage used for analog input analogRead() reads an analog voltage and converts to an integervalue between 0 and 1023 analogWrite() uses PWM to write a value out on a pin.
More I/O Functions examples will be seen in project examples
Comments
Post a Comment