Arduino provides many built-in functions that make it easy to control hardware and read information from sensors.
These functions allow the Arduino to:
- Configure pins
- Turn devices ON or OFF
- Read digital signals
- Read analog signals
- Generate PWM output
- Create delays
- Perform tasks based on time without blocking the program
In this lesson, we will learn:
pinMode()digitalWrite()digitalRead()analogRead()analogWrite()delay()millis()
1. pinMode()
The pinMode() function is used to configure an Arduino pin as an input or output.
Syntax
Here:
pin → The Arduino pin number.
mode → Defines how the pin will be used.
Common modes are:
INPUTOUTPUTINPUT_PULLUP
Example
This configures digital pin 13 as an output.
We can now use pin 13 to control something such as an LED.
Input Example
This configures pin 2 as an input.
We can use it to read a push button or another digital signal.
INPUT_PULLUP
Arduino also provides an internal pull-up resistor.
This allows us to connect a push button without necessarily adding an external pull-up resistor.
With INPUT_PULLUP, the input is normally read as HIGH and becomes LOW when the button is connected to GND and pressed.
Example
2. digitalWrite()
The digitalWrite() function is used to set a digital output pin to either:
HIGHLOW
Syntax
Example
This sets pin 13 to a HIGH output state.
To set it LOW:
Turning an LED ON and OFF
For a typical LED connected appropriately to a digital output:
The LED will turn ON for one second and OFF for one second.
Understanding HIGH and LOW
For a typical Arduino Uno digital output:
HIGH → approximately 5V
LOW → approximately 0V
The exact electrical behavior should always be understood from the board's specifications.
3. digitalRead()
The digitalRead() function is used to read the state of a digital input pin.
A digital input generally has two logical states:
HIGHLOW
Syntax
The function returns either HIGH or LOW.
Example
When the button is not pressed, the pin will normally read:
HIGH
When the button is pressed and connected to GND:
LOW
Example with LED
We can use a push button to control an LED.
Now:
Button pressed → LED ON
Button released → LED OFF
4. analogRead()
The analogRead() function is used to read an analog voltage from an analog input.
This is extremely important for sensors such as:
- Soil moisture sensors
- Potentiometers
- Light sensors
- Temperature sensors with analog output
Syntax
On the Arduino Uno, the analog inputs are typically:
A0 to A5
Arduino Uno ADC
The Arduino Uno has a 10-bit ADC.
This means an analog reading is represented using values from:
0 to 1023
So:
0 → Lowest input level
1023 → Highest input level
For an Uno's default analog reference, the full-scale range corresponds approximately to 0V to the board's analog reference voltage, commonly 5V.
Example
The Serial Monitor might show:
The actual values depend on the voltage coming from the connected sensor.
Example with Potentiometer
Connect a potentiometer so that:
One outer pin → 5V
Other outer pin → GND
Middle pin → A0
Then:
When you rotate the potentiometer, the voltage at A0 changes and the analog reading changes.
5. analogWrite()
The analogWrite() function is commonly used to control the PWM duty cycle on supported Arduino pins.
Despite its name, on the Arduino Uno it does not generate a continuously varying analog voltage. It generates a PWM signal.
PWM
PWM stands for Pulse Width Modulation.
PWM rapidly switches a digital output between HIGH and LOW.
By changing the percentage of time the signal stays HIGH, we can control the average power delivered to some loads.
Syntax
On the Arduino Uno, the value is normally:
0 to 255
Where:
0 → 0% duty cycle
255 → 100% duty cycle
Values between them produce intermediate duty cycles.
Example
A value of 128 produces approximately a 50% PWM duty cycle.
For an LED, this can make the LED appear partially bright.
Example: LED Brightness
The LED gradually becomes brighter and then gradually becomes dimmer.
PWM Pins on Arduino Uno
On the standard Arduino Uno, the commonly marked PWM pins are:
3, 5, 6, 9, 10, 11
These pins are marked with a ~ symbol on many Uno boards.
Only supported PWM pins should be used with analogWrite() when PWM output is required.
6. delay()
The delay() function pauses the program for a specified amount of time.
Syntax
The value is given in milliseconds.
Common Values
1000 milliseconds = 1 second
500 milliseconds = 0.5 second
100 milliseconds = 0.1 second
Example
This pauses the program for approximately one second.
LED Example
The LED turns ON for one second and OFF for one second.
Problem with delay()
delay() is easy to understand, but it blocks the program.
For example:
During those five seconds, the program cannot continue normal execution of the code after that statement.
This can become a problem when an IoT device needs to perform multiple tasks at the same time.
For example, an ESP32 may need to:
- Read a sensor
- Update a display
- Check a button
- Control a relay
- Communicate over Wi-Fi
Using long delay() calls can make the system less responsive.
7. millis()
The millis() function returns the number of milliseconds that have passed since the Arduino started running the current program.
Syntax
It returns an unsigned long value representing elapsed milliseconds.
Example
If approximately 5 seconds have passed since startup:
Why Use millis()?
millis() allows us to perform tasks based on time without blocking the entire program with a long delay().
For example, suppose we want to read a soil moisture sensor every second.
Instead of:
we can check elapsed time.
Example
This reads the sensor approximately every 1000 milliseconds while allowing the rest of the loop() function to continue running.
delay() vs millis()
The difference is important for Arduino and IoT programming.
delay()
delay() pauses the program.
Example:
The program waits for approximately one second before continuing.
millis()
millis() allows us to check how much time has passed without blocking the program.
This is much more useful when an IoT device needs to perform multiple tasks.
Practical Example: Multiple Tasks with millis()
Suppose our IoT system needs to:
- Read a soil moisture sensor every 1 second
- Blink an LED every 500 milliseconds
- Perform another task every 2 seconds
We can use millis() to manage these tasks independently.
Here, the sensor reading and LED operation use separate timing intervals.
This is a very important technique for larger Arduino and IoT projects.
Summary of Arduino Functions
pinMode()
Used to configure a pin as input or output.
Example:
digitalWrite()
Used to set a digital output HIGH or LOW.
Example:
digitalRead()
Used to read a digital input.
Example:
analogRead()
Used to read an analog input.
Example:
analogWrite()
Used to output PWM on supported pins.
Example:
delay()
Pauses the program for a specified number of milliseconds.
Example:
millis()
Returns the number of milliseconds elapsed since the program started.
Example:
What We Can Build Using These Functions
Once these functions are understood, we can already build many useful Arduino projects.
LED
pinMode() + digitalWrite() + delay()
Push Button
pinMode() + digitalRead() + digitalWrite()
Potentiometer
analogRead() + Serial.println()
LED Brightness Control
analogRead() + analogWrite()
Soil Moisture Monitor
analogRead() + if + Serial.println()
Automatic Plant Watering
analogRead() + if + digitalWrite() + relay/pump control
Advanced IoT Timing
millis() + sensors + communication + outputs