Arduino unit II
Arduino unit II
UNIT II
1. Digital I/O Functions
Digital Output
Digital Input
Analog Input
Both delay() and millis() are used to handle timing in Arduino, but they work differently.
delay(ms) Function
The delay() function pauses the program for a specified amount of time in milliseconds (ms).
Example:
digitalWrite(13, HIGH);
delay(1000); // Wait for 1 second
digitalWrite(13, LOW);
delay(1000); // Wait for 1 second
Limitations of delay()
millis() Function
The millis() function returns the number of milliseconds since the Arduino was powered on. It
does not pause execution.
Example:
}
Why Use millis() Instead of delay()?
Arduino pins are set as inputs by default, so you don’t need to use pinMode() to declare them as
inputs.
In this state, they have high resistance (high-impedance), meaning they draw very little current
from the circuit.
This makes it easy to change the pin’s state with a small amount of current, which is useful for
things like capacitive touch sensors or reading an LED as a light sensor.
If an input pin pinMode(pin, INPUT) is left unconnected, it may randomly switch between
HIGH and LOW due to electrical noise or signals from nearby pins.
Pull-up Resisters
Pull-up resistors help keep an input pin in a known state when no signal is present.
A pull-up resistor connects the pin to +5V, while a pull-down resistor connects it to GND.
This prevents the pin from floating and randomly switching states.
A 10KΩ resistor is commonly used for both pull-up and pull-down configurations.
The 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Ω.
The internal registers (memory locations) that control whether a pin is HIGH or LOW also
manage the pull-up resistors. Therefore, when a pin is set to INPUT mode with the pull-up
resistor enabled, it will be at a HIGH state.
If the pin is later switched to OUTPUT mode using pinMode(), the pin will remain HIGH, as
the pull-up resistor is still active. Similarly, if an output pin is set to HIGH and then switched to
INPUT mode, the pull-up resistor will be enabled, keeping the pin at a HIGH state.
Example:
pinMode(3, INPUT) ; // set pin to input without using built in pull up resistor
Pins configured as OUTPUT with pinMode() are in a low-impedance state, allowing them
to provide up to 40 mA of current. This is sufficient for small components like LEDs (with a
current-limiting resistor) and sensors, but not for high-current devices like relays, solenoids, or
motors.
Overloading output pins can damage the internal transistors or even destroy the entire
Atmega chip, often resulting in a "dead" pin while the rest of the chip remains functional.
To prevent damage, it's recommended to connect output pins to external devices through
470Ω or 1kΩ resistors unless the application specifically requires the maximum current.
pinMode() Function
The pinMode() function is used to configure a specific pin to behave 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.
Syntax
Void setup () {
pinMode (pin , mode); }
pin − the number of the pin whose mode you wish to set
mode− INPUT, OUTPUT, or INPUT_PULLUP.
Example
void setup()
{
pinMode(7, OUTPUT); // Set pin 7 as an output
pinMode(8, INPUT); // Set pin 8 as an input
pinMode(9, INPUT_PULLUP); // Set pin 9 as an input with an internal pull-up resistor
}
void loop()
{
digitalWrite(7, HIGH); // Turn on the output at pin 7
delay(1000); // Wait for a second
digitalWrite(7, LOW); // Turn off the output at pin 7
delay(1000); // Wait for a second
}
digitalWrite() Function
The digitalWrite() function sets a digital pin to HIGH or LOW. If the pin is set as an OUTPUT
using pinMode(), it outputs 5V (or 3.3V on some boards) for HIGH and 0V for LOW.
If the pin is set as an INPUT, digitalWrite(HIGH) enables the internal pull-up resistor, and
digitalWrite(LOW) disables it. It is best to use pinMode(INPUT_PULLUP) to enable the
internal pull-up resistor.
If pinMode() is not set to OUTPUT, an LED connected to the pin may glow dimly when
digitalWrite(HIGH) is used. This happens because the internal pull-up resistor limits the
current.
Syntax
Void loop()
{
digitalWrite (pin ,value);
}
Example
int LED = 6; // LED connected to pin 6
void setup()
{
pinMode(LED, OUTPUT); //set the digital pin as output
}
void loop()
{
digitalWrite(LED,HIGH); //turn on LED
delay(500); //delay for 500 ms
digitalWrite(LED,LOW); //turn LED
delay(500); //delay for 500ms
}
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, there is an “Analog In” pins. These special pins not
only detect the presence of voltage but also measure 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.
Syntax
analogRead(pin);
Example:
int analogPin = 3;//potentiometer wiper (middle terminal)
//connect 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
}