Skip to main content

Arduino pinMode, digitalWrite, digitalRead, analogWrite, and analogRead

In Arduino, pinMode() sets whether a pin is an input or an output.
digitalWrite() sends a digital output, while digitalRead() reads a digital input.
analogRead() reads analog sensor values between 0–1023,
and analogWrite() outputs a PWM signal with values between 0–255.


What Are Arduino Pin Functions?

In Arduino projects, pins act as the connection between software and the physical world.
These functions are used to turn on LEDs, read buttons, collect sensor data, and control motor speed.

On this page, you will learn the following basic Arduino functions:

  • pinMode()
  • digitalWrite()
  • digitalRead()
  • analogRead()
  • analogWrite()

What Is pinMode()? What Does It Do?

pinMode() defines whether an Arduino pin will be used as an input (INPUT) or an output (OUTPUT).

Why Is It Used?

  • To turn on an LED, the pin must be set as OUTPUT
  • To read a button or sensor, the pin must be set as INPUT

To better understand input and output pins, visit the Digital and Analog Pins page.

Usage

pinMode(pinNumber, MODE);

Modes

  • INPUT

  • OUTPUT

  • INPUT_PULLUP

void setup() {
pinMode(13, OUTPUT);
}

What Is digitalWrite()? What Does It Do?

digitalWrite() sets a digital pin to ON (HIGH) or OFF (LOW).

Values

  • HIGH → 5V

  • LOW → 0V

Usage

digitalWrite(pinNumarasi, HIGH veya LOW);

Example

void setup() {
pinMode(13, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}

What Is digitalRead()? What Does It Do?

digitalRead() reads the current state of a digital pin.

Possible Values

  • HIGH
  • LOW

Usage

digitalWrite(pinNumarasi, HIGH veya LOW);

Example: Button Reading

void setup() {
pinMode(2, INPUT); // input device
pinMode(13, OUTPUT); // output device
}

void loop() {
if (digitalRead(2) == HIGH) { // if button is pressed
digitalWrite(13, HIGH); // turn LED on
} else {
digitalWrite(13, LOW); // turn LED off
}
}

What Is analogRead()? What Does It Do?

analogRead() converts the voltage from analog pins into a digital value.

Value Range

0 – 1023

Usage

int deger = analogRead(A0);

Example: Potentiometer

void setup() {
Serial.begin(9600);
}

void loop() {
int value = analogRead(A0); // read value
Serial.println(value); // write to serial monitor
}


What Is analogWrite()? What Does It Do?

analogWrite() controls output using a PWM signal.

⚠️ Works only on PWM pins marked with ~.

Value Range

  • 0 – 255

Usage

analogWrite(pinNumarasi, deger);


Example

void setup() {
pinMode(9, OUTPUT);
}

void loop() {
analogWrite(9, 120);
}


Arduino Pin Functions Comparison

FonksiyonAmaçDeğer
pinModePin directionINPUT / OUTPUT
digitalWriteDigital outputHIGH / LOW
digitalReadDigital inputHIGH / LOW
analogReadAnalog input0 – 1023
analogWritePWM output0 – 255

Summary Answer

In Arduino projects, pinMode sets the pin direction, digitalWrite and analogWrite control outputs, while digitalRead and analogRead read input data. These functions form the foundation of Arduino programming.

Conclusion

These five functions are the core building blocks of Arduino. They are used in almost every project involving sensors, LEDs, motors, and other electronic components.