Skip to main content
Translate content

To read content in another language, use your browser’s Translate page option (right-click the page or open the browser menu). Choose your preferred language; if offered, enable automatic translation. The site language menu changes navigation, not the original article. Interactive activities may remain in their original language.

roboticsFundamentalsEstimated time: 30 minUpdated November 26, 2026

Arduino for Beginners: Understanding Inputs and Outputs

Digital signals, analog signals and virtual circuit experiments

An Arduino board can blink a light, read a button, measure a knob and dim a lamp, all from a few lines of code. The secret behind every one of these projects is the same: inputs and outputs. In this lesson you will learn the difference between digital and analog signals and run four virtual circuit experiments.

Arduino Uno board wired to a breadboard with an LED, a push button and a potentiometer

Learning objectives

  • Explain the structure of an Arduino sketch: setup() and loop()
  • Distinguish inputs from outputs and digital from analog signals
  • Use pinMode(), digitalWrite() and digitalRead()
  • Explain floating pins and the INPUT_PULLUP mode
  • Use analogRead(), map() and analogWrite() with PWM
  • Choose a safe series resistor for an LED

An Arduino board can blink a light, read a button, measure a knob and dim a lamp, all from a few lines of code. The secret behind every one of these projects is the same: inputs and outputs. In about 30 minutes you will learn the difference between digital and analog signals and run four virtual circuit experiments right on this page: a blinking LED, a push button, a dimmer and an LED resistor lab. A self-graded quiz closes the lesson.

  • Time: 30 minutes
  • Level: beginner
  • Prerequisites: none
  • You need: a web browser (a board is optional)

1. What is an Arduino?

An Arduino is a small, low-cost electronic board built around a microcontroller: a tiny computer that runs one program and talks directly to electronic parts through its pins. You write the program on your computer, send it to the board over USB, and the board runs it by itself.

An Arduino program is called a sketch. Every sketch has two functions:

  • setup() runs once when the board starts. You use it to prepare the pins.
  • loop() runs again and again, forever. This is where your program does its work.

This lesson uses the Arduino Uno, the most common beginner board. Other boards use different voltages and pins, so check your board's documentation.

Uno pinsWhat they do
Digital pins 0 to 13Read or write HIGH and LOW. Pins 0 and 1 are shared with the USB serial connection, so avoid them at first.
PWM pins 3, 5, 6, 9, 10, 11 (marked ~)Digital pins that can also imitate an analog output with analogWrite().
Analog pins A0 to A5Measure a voltage between 0 and 5 V with analogRead().
5V, 3.3V, GNDPower supply pins and the ground (0 V) reference.

2. Inputs and outputs: the big idea

Almost every Arduino project follows one pattern:

  • Inputs bring information into the board: buttons, knobs (potentiometers), light sensors, temperature sensors.
  • Outputs let the board act on the world: LEDs, buzzers, small displays, and, through a suitable driver circuit, motors.
  • Your sketch decides what each output does according to the inputs.

Signals come in two families, and choosing the right functions depends on knowing which one you have:

Digital signalAnalog signal
ValuesOnly two: HIGH or LOWAny value in a range
On an UnoHIGH is about 5 V, LOW is about 0 VA voltage anywhere from 0 to 5 V
Input exampleA push button: pressed or notA knob: any position
Output exampleAn LED: on or offAn LED's brightness, from dark to bright
FunctionsdigitalRead(), digitalWrite()analogRead(), analogWrite()

3. Digital output: HIGH and LOW

To control an LED you first tell the board how the pin will be used, then set it HIGH (5 V, LED on) or LOW (0 V, LED off):

pinMode(13, OUTPUT);       // in setup(): pin 13 is an output
digitalWrite(13, HIGH);     // pin 13 gives 5 V: LED on
digitalWrite(13, LOW);      // pin 13 gives 0 V: LED off
delay(500);               // wait 500 milliseconds

Pin 13 is connected to a small LED built into the Uno board, which makes it perfect for a first test. Try the virtual pin below:

delay: ms

Experiment: press Blink, then shorten the delay. Around 100 ms the LED blinks quickly. If you could make the delay a few milliseconds, the blinking would become too fast for your eye to see, and the LED would look dimmer instead. Section 5 uses exactly that trick.

4. Digital input and the pull-up resistor

To read a button you use digitalRead(), which returns HIGH or LOW. There is a catch: a pin connected to nothing definite is said to be floating, and it picks up electrical noise and reads random values. The pin needs a resistor that pulls it to a known level when the button is not pressed. The Arduino has one built in:

pinMode(2, INPUT_PULLUP);   // internal resistor pulls pin 2 up to HIGH

With INPUT_PULLUP, the button is wired between the pin and GND. Unpressed, the pin reads HIGH; pressed, it connects to GND and reads LOW. The logic is inverted, which surprises almost every beginner. Compare the three ways of wiring a button:

Pin mode:

Experiments: with INPUT_PULLUP, notice that the pin reads HIGH when the button is not pressed. Then choose the floating mode and leave the button alone: the readings jump randomly and the LED flickers. That is why a resistor is essential.

5. Analog signals: reading and PWM

Reading an analog signal. A potentiometer is a knob that changes a voltage smoothly between 0 V and 5 V. The board measures it with an analog-to-digital converter (ADC), which turns the voltage into a whole number. On an Uno, analogRead() returns a value from 0 to 1023 (10 bits):

voltage = reading × 5 / 1023

Writing an "analog" signal. Most Arduino pins cannot output a steady voltage in between 0 V and 5 V. Instead, analogWrite(pin, value) uses PWM (pulse-width modulation): the pin switches between 5 V and 0 V very fast, hundreds of times per second, and the duty cycle, the share of time spent HIGH, sets the average. The value goes from 0 to 255:

duty cycle = value / 255     average voltage ≈ duty cycle × 5 V

The Arduino function map(x, 0, 1023, 0, 255) converts a reading (0 to 1023) into a PWM value (0 to 255). Turn the virtual knob below and watch the whole chain:

Knob position: %

PWM waveform: ▬ pin voltage   - - average voltage (this is a simplified drawing: the real signal repeats about 490 times per second on most Uno pins)

Experiment: move the knob and watch the HIGH part of the waveform grow while the LED brightens. At the far left the pin stays LOW all the time (0%); at the far right it stays HIGH all the time (100%).

6. Protecting your LED: the resistor lab

An LED is not a lamp you can connect directly to 5 V: it lets a large current flow once it turns on, which can destroy it and overload the pin. A series resistor limits the current. Ohm's law gives the value:

I = (Vsupply − VLED) / R     so     R = (Vsupply − VLED) / I

VLED is the LED's forward voltage, which depends on its color (typical values below; check your LED's datasheet). A common target is about 10 to 15 mA, which is bright enough and safe: an Uno pin should supply no more than about 20 mA, and 40 mA is the absolute maximum. Pick a resistor and see what happens:

Supply: LED color: Resistor:

Experiments: choose 47 Ω with a red LED at 5 V: the current is far too high. Then choose 220 Ω, the classic beginner value: bright and safe. Try 10 kΩ and see how dim the LED becomes. Finally switch the supply to 3.3 V with a blue LED and notice that there is hardly any current: the supply is barely above the LED's forward voltage.

7. Your first three sketches

Sketch 1: blink the built-in LED (digital output).

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

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

Sketch 2: a button lights the LED (digital input and output). The button goes between pin 2 and GND.

const int BUTTON_PIN = 2;
const int LED_PIN = 13;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {   // LOW means pressed
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

Sketch 3: a knob dims the LED (analog input and PWM output). The knob's middle pin goes to A0, and the LED, with its resistor, to pin 9.

const int POT_PIN = A0;
const int LED_PIN = 9;    // a PWM pin (marked ~)

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int reading = analogRead(POT_PIN);            // 0 to 1023
  int brightness = map(reading, 0, 1023, 0, 255);
  analogWrite(LED_PIN, brightness);               // 0 to 255
  Serial.println(reading);                       // see it in the Serial Monitor
  delay(20);
}

You can try these sketches without any hardware in a free online simulator such as Tinkercad Circuits or Wokwi, then move to a real board when you are ready.

8. Safety and good habits

  • Always use a resistor with an LED (section 6).
  • Do not power motors or other heavy loads directly from a pin. A pin supplies only a few tens of milliamps. Use a transistor or a motor driver and a suitable power supply.
  • Disconnect USB power before rewiring, and double-check connections before reconnecting.
  • Respect the voltage. An Uno works at 5 V; connecting a part meant for 3.3 V to a 5 V signal can damage it.
  • Test one piece at a time: make the LED work, then the button, then combine them.
The experiments on this page are simulations. When you build the real circuit, check each wire and each resistor value before powering it.

9. Practice exercises

Compute or complete each answer, then check it. The feedback explains the solution.

10. Final quiz: test what you learned

Ten questions, one correct answer each. You get instant grading and every answer is explained.

Key takeaways

  • An Arduino sketch has setup() (runs once) and loop() (runs forever).
  • Inputs bring information in; outputs act on the world; the sketch connects them.
  • Digital signals have two values (HIGH, LOW); analog signals vary over a range.
  • INPUT_PULLUP avoids floating pins, and makes a button read LOW when pressed.
  • analogRead() returns 0 to 1023; analogWrite() uses PWM with values from 0 to 255.
  • Always protect an LED with a resistor: R = (supply − LED voltage) / current.

Frequently asked questions

What is the difference between digital and analog on an Arduino?

A digital signal has only two states, HIGH and LOW, read with digitalRead() and set with digitalWrite(). An analog signal can take any value in a range: analogRead() measures it as a number from 0 to 1023 on an Uno, and analogWrite() imitates it with PWM.

Does analogWrite() really produce an analog voltage?

No. It produces PWM: a digital signal that switches rapidly between 0 V and 5 V. The share of time spent HIGH, the duty cycle, sets the average voltage, which an LED or a motor responds to.

Why does my button read LOW when I press it?

With INPUT_PULLUP the pin is pulled up to HIGH and the button connects it to GND, so pressing it makes the pin read LOW. The logic is inverted by design, and the sketch should test for LOW.

Why do I need a resistor with an LED?

An LED lets a large current flow once it turns on. The resistor limits that current, protecting both the LED and the Arduino pin. A 220 ohm resistor is a common choice at 5 V.

Do I need to buy an Arduino to follow this lesson?

No. The virtual experiments on this page and a free online simulator are enough to learn the concepts. A real board makes the projects more rewarding once you are comfortable.

Definitions

Microcontroller

A tiny computer on a chip that runs one program and talks to electronic parts through its pins.

Sketch

An Arduino program, made of a setup() function that runs once and a loop() function that repeats forever.

Digital signal

A signal with only two values, HIGH and LOW (about 5 V and 0 V on an Arduino Uno).

Analog signal

A signal that can take any value in a range, such as a voltage between 0 and 5 V.

Floating input

An input pin connected to nothing definite, whose reading is random because of electrical noise.

PWM (pulse-width modulation)

A digital signal that switches rapidly between HIGH and LOW; the share of time spent HIGH (duty cycle) sets the average voltage.

Duty cycle

The percentage of each PWM period during which the signal is HIGH.

Forward voltage

The voltage that appears across an LED when it conducts, which depends on its color.

Worked examples

From knob to PWM

analogRead(A0) returns 768 on an Uno powered at 5 V. Find the voltage, the value passed to analogWrite() with map(768, 0, 1023, 0, 255), and the duty cycle.

Voltage = 768 x 5 / 1023 = 3.75 V. map gives 768 x 255 / 1023 = 191.4, so 191 with integer arithmetic. Duty cycle = 191 / 255 = about 75 %.

Choosing an LED resistor

A green LED (forward voltage 2.2 V) runs on 5 V with a target current of 10 mA. Which standard resistor do you choose, and what is the real current?

R = (5 - 2.2) / 0.010 = 280 ohms. The next standard value is 330 ohms. Real current = 2.8 / 330 = about 8.5 mA.

Reading a button with INPUT_PULLUP

A button is wired between pin 2 and GND, with pinMode(2, INPUT_PULLUP). Write the condition that detects a press.

if (digitalRead(2) == LOW) { ... } because the pin reads LOW when the button connects it to GND.

Common mistakes to avoid

  • Connecting an LED without a series resistor
  • Leaving a button input floating instead of using INPUT_PULLUP or a pull-down resistor
  • Testing for HIGH when the button uses INPUT_PULLUP (the logic is inverted)
  • Using analogWrite() on a pin without PWM support
  • Powering a motor directly from an Arduino pin
  • Confusing the range of analogRead() (0 to 1023) with that of analogWrite() (0 to 255)

Practice exercises

PWM value

What duty cycle and average voltage does analogWrite(9, 64) produce on a 5 V Uno?

Hint: Duty cycle = value / 255.

Show solution

64 / 255 = about 25 %, so an average of about 1.25 V.

Resistor for a red LED

Choose a standard resistor for a red LED (forward voltage 2 V) on 5 V with a target current of 10 mA.

Hint: R = (5 - 2) / 0.010, then take the next standard value.

Show solution

R = 300 ohms, so use 330 ohms (current about 9 mA).

Find the wiring mistake

A button is wired between pin 2 and 5 V, and the sketch uses pinMode(2, INPUT). The LED flickers when the button is not pressed. Explain why and give two fixes.

Hint: Think about what the pin is connected to when the button is open.

Show solution

The pin is floating when the button is open, so it reads random values. Fix 1: add a 10 kilo-ohm pull-down resistor from pin 2 to GND. Fix 2: wire the button between pin 2 and GND, use INPUT_PULLUP and test for LOW.

Key takeaways

  • An Arduino sketch has setup() (runs once) and loop() (runs forever)
  • INPUT_PULLUP avoids floating pins and makes a button read LOW when pressed
  • analogRead() returns 0 to 1023; analogWrite() uses PWM with values from 0 to 255
  • Always protect an LED with a resistor: R = (supply - LED voltage) / current