This post contains affiliate links, marked with *. As an Amazon Associate I earn from qualifying purchases, at no extra cost to you. It helps keep the tutorials free.
Quick summary
This guide covers Arduino programming basics. It starts with installing the Arduino IDE and the structure of a sketch. It then explains the core C++ elements (variables, data types, functions, if/else), digital and analog input and output, and debugging (finding errors) with the Serial Monitor. Three projects use these elements: Blink flashes the built-in LED, Button LED switches an LED with a push button, and Analog Reader prints a potentiometer value to the Serial Monitor.
What you need
- Arduino Uno R3 (or a compatible board; the IDE lists it as Arduino Uno) and a USB cable that supports data transfer.
- Breadboard (a plastic board with connected holes for building circuits without soldering) and jumper wires (short wires with pin ends that plug into the breadboard and the board).
- 1 × LED and 1 × 220 Ω resistor.
- 1 × push button.
- 1 × potentiometer (a resistor with a turning knob, e.g. 10 kΩ).
- A computer with Windows, macOS or Linux.
What is an Arduino sketch?
A sketch is Arduino’s name for a program. The term comes from Processing, the programming environment the original Arduino IDE was based on. Every sketch is saved as a .ino file, a plain text file containing your code.
When you click Upload, the Arduino IDE compiles your code into machine instructions and sends them to the microcontroller, the programmable chip on the Arduino board (an ATmega328P on the Arduino Uno R3). The board then runs those instructions and controls the LEDs, sensors and motors connected to its pins, the numbered connection points along the edges of the board.
Every Arduino sketch must contain two functions: setup() and loop().
setup(): runs once
The setup() function runs once when the board powers on or resets. Use it to configure pin modes (whether a pin works as an input or an output), start serial communication with the computer, set starting values and initialize library objects. A library is ready-made code for a sensor, display or other part; you add it with #include at the top of the sketch. Think of setup() as the preparation step before your main program starts.
loop(): repeats forever
After setup() finishes, loop() runs continuously. The code inside loop() executes top to bottom and then starts over, as long as the board has power. This is where your main program logic goes: reading sensors, controlling outputs, making decisions and talking to other devices.
The minimal structure of every Arduino sketch looks like this.
void setup() {
// Initialization code runs once
}
void loop() {
// Main code runs repeatedly
}
Setting up the Arduino IDE
The Arduino Integrated Development Environment (IDE) is the free, open-source software (its source code is public) that you use to write, compile and upload code to your Arduino board. It runs on Windows, macOS and Linux and is available at arduino.cc/en/software. The steps below show Windows. On macOS, open the .dmg file and drag the app into Applications; on Linux, use the AppImage or ZIP file.
Step 1: Download. Get the latest version of the Arduino IDE for your operating system from the download page.

Step 2: Run the installer. Open your Downloads folder and double-click the installer file (for example arduino-ide_x.x.x_Windows_64bit.exe).

Step 3: Follow the wizard. Click I Agree to accept the license agreement, keep the default destination folder (C:\Program Files\Arduino IDE) and click Install. If Windows asks whether to install driver software for the board, confirm it. A driver is the software that lets the computer communicate with the board over USB. I took these screenshots with Arduino IDE 2.3.2 on Windows 11.

Step 4: Finish. Wait for the progress bar to fill. On the completion screen, leave Run Arduino IDE checked and click Finish. The IDE then starts on its own.

When the IDE opens, you see a new empty sketch with the setup() and loop() functions already in place.

Overview of the Arduino IDE interface
The table explains each numbered element in the two annotated screenshots.
| # | Element | What it does |
|---|---|---|
| ① | Menu Bar | File operations, preferences, board settings and help |
| ② | Toolbar | Holds the buttons for the most common actions: ⑥ to ⑧ on the left, ⑫ on the right |
| ③ | Code Editor | The central area where you write and edit your sketch, with syntax highlighting, which shows different parts of the code in different colors |
| ④ | Console / Output Window | Shows compile messages, errors and upload progress at the bottom of the IDE |
| ⑤ | Status Bar | Shows the current line and column, the board name and the connected port |
| ⑥ | Verify / Compile Button (check mark icon) | Checks your code for errors and compiles it |
| ⑦ | Upload Button (right arrow icon) | Sends the compiled program to the connected board |
| ⑧ | Board & Port Selector | Choose the board type (e.g. Arduino Uno) and the port, the serial connection over USB that the computer assigns to the board |
| ⑨ | Sketchbook | Lists all your saved sketches |
| ⑩ | Boards Manager | Installs support for other board families, such as the ESP32 |
| ⑪ | Library Manager | Search for and install code libraries for sensors, displays and other parts |
| ⑫ | Serial Plotter and Serial Monitor | The left icon draws incoming numbers as a live graph; the magnifying glass opens the Serial Monitor, which shows the data exchanged between the board and your computer |


Before you upload a sketch, confirm that the right board and port are selected in the Board & Port Selector (⑧). On Windows the port is named COMx (e.g. COM3); on macOS it appears as /dev/cu.usbmodem… or /dev/cu.usbserial…, on Linux as /dev/ttyACM0 or /dev/ttyUSB0. A wrong board or port is the most common cause of upload errors. The overview screenshots show the board entry Arduino Mega or Mega 2560 on COM7. For Blink in the video I connect an Arduino Uno, and before the upload I check that the selector shows Arduino Uno and the port COM5.
Core programming concepts
These elements appear in nearly every sketch.
Variables and data types
A variable is a named container for a value. When you create (declare) a variable, you also state its data type, which tells the compiler what kind of value it holds and how much memory to reserve. The most common types:
| Data type | Description | Example |
|---|---|---|
| int | Whole numbers (−32,768 to 32,767 on the Uno R3; a larger range on 32-bit boards such as the ESP32) | int speed = 150; |
| long | Large whole numbers (−2,147,483,648 to 2,147,483,647) | long distance = 100000; |
| float | Decimal numbers | float temperature = 23.5; |
| bool | True or false | bool ledOn = true; |
| char | Single character | char grade = ‘A’; |
| String | Text, e.g. a name or message | String name = “Arduino”; |
Constants
Use the const keyword for a value that never changes while the program runs. Constants make code easier to read and protect values from accidental changes. The example uses the name PI_VALUE because Arduino already defines a constant called PI.
const int LED_PIN = 13; const float PI_VALUE = 3.14159;
Arduino also defines built-in constants such as HIGH, LOW, INPUT, OUTPUT and INPUT_PULLUP.
Comments
Comments are notes in your code that the compiler ignores. They help you and others understand what the code does. Use // for a single-line comment and /* */ for a multi-line comment.
// This is a single-line comment /* This is a multi-line comment */
Functions
A function is a reusable block of code that performs one task. You already know setup() and loop(). You can also write your own functions to keep your code organized.
void blinkLED(int pin, int delayTime) {
digitalWrite(pin, HIGH);
delay(delayTime);
digitalWrite(pin, LOW);
delay(delayTime);
}
The keyword void means the function returns no value. The names pin and delayTime are parameters: values you pass in when you call the function, for example blinkLED(13, 500).
Decisions with if and else
An if statement runs a block of code only when its condition is true; the optional else block runs otherwise. Conditions use comparison operators: == (equal), != (not equal), < (less than) and > (greater than). A single = assigns a value; a double == compares two values. Project 2 uses if and else to switch the LED.
Digital output with digitalWrite()
Digital output switches a pin HIGH (5 V on the Uno, 3.3 V on 3.3 V boards such as the ESP32) or LOW (0 V). Set the pin as an output in setup(), then control it in loop().
pinMode(13, OUTPUT); // In setup() digitalWrite(13, HIGH); // Turn pin 13 ON digitalWrite(13, LOW); // Turn pin 13 OFF
Digital input with digitalRead()
Digital input reads whether a pin is HIGH or LOW, which is what you need for buttons and switches.
pinMode(2, INPUT_PULLUP); // In setup() int state = digitalRead(2); // Read pin 2
INPUT_PULLUP switches on a resistor inside the chip that connects the pin to 5 V (a pull-up resistor). The pin therefore reads HIGH while the button is open and LOW when the pressed button connects it to GND (ground, the 0 V reference of the board). This inverted logic is called active-low.
Analog input with analogRead()
The Arduino Uno reads analog voltages on pins A0 to A5. Its 10-bit analog-to-digital converter (ADC) turns 0 to 5 V into a number from 0 to 1023, about 4.9 mV per step. Typical sources are potentiometers, light sensors and temperature sensors.
int sensorValue = analogRead(A0); // Read analog pin A0
Serial communication and the Serial Monitor
The Serial Monitor is a window in the IDE that shows text the board sends over USB; it can also send text back to the board. Printing values is the easiest way to see what your sketch is doing. The number in Serial.begin() is the baud rate, the transmission speed in bits per second; 9600 is a common default. Start the connection in setup() and print values in loop().
void setup() {
Serial.begin(9600); // Start serial at 9600 baud
}
void loop() {
Serial.println("Hello!"); // Print text to Serial Monitor
delay(1000);
}
To open the Serial Monitor, click the magnifying glass icon in the top right of the Arduino IDE or press Ctrl+Shift+M (Cmd+Shift+M on macOS). The baud rate selected at the bottom of the Serial Monitor must match the value in Serial.begin().
Project 1: Blink the built-in LED
Blink is the standard first test for a new board. It needs no extra parts: the Arduino Uno has a built-in LED, marked L, connected to pin 13. If it blinks, your board, IDE and USB connection work.
Wiring (optional external LED)
You can run Blink with the built-in LED alone. To use an external LED on a breadboard, connect it like this:
- Connect the long leg (anode, +) of the LED to Arduino pin 13 through a 220 Ω resistor.
- Connect the short leg (cathode, −) of the LED to GND.
The resistor limits the current to about 14 mA for a red LED; without it, the LED and the pin can be damaged. The diagram below shows only where the two legs go and leaves out the resistor; add it between pin 13 and the long leg.

The Blink code
/**
* Author: Omar Draidrya
* Date: 2024/04/11
* This code blinks an LED connected to a specified pin on and off with a one-second interval.
*/
// Define the LED pin
const int LED_PIN = 13;
// The setup function runs once when you power up or reset the board
void setup() {
// Set the LED_PIN as an output
pinMode(LED_PIN, OUTPUT);
}
// The loop function runs over and over again forever
void loop() {
digitalWrite(LED_PIN, HIGH); // Turn on the LED
delay(1000); // Wait for a second
digitalWrite(LED_PIN, LOW); // Turn off the LED
delay(1000); // Wait for a second
}
Line-by-line explanation
const int LED_PIN = 13;declares a constant integer for the LED pin number. Using a named constant like this makes your code easier to update later.pinMode(LED_PIN, OUTPUT);runs once in setup() and configures pin 13 as a digital output, so it can send voltage to the LED.digitalWrite(LED_PIN, HIGH);sets pin 13 to 5 V, which turns the LED on.delay(1000);pauses the program for 1000 milliseconds (one second).digitalWrite(LED_PIN, LOW);sets pin 13 to 0 V, which turns the LED off.delay(1000);pauses again for one second before loop() repeats.
Upload and test
Connect the board to your computer with the USB cable, select the board and port in the IDE and click Upload. When the upload finishes, the LED blinks: one second on, one second off. In the video I then change the first delay(), the one after HIGH, to 500: the LED stays on for half a second and off for a full second. With both delays at 500, it blinks twice as fast.
Common Blink mistakes
- Forgetting pinMode(): without setting the pin as OUTPUT, digitalWrite(LED_PIN, HIGH) only switches on the internal pull-up resistor, so an external LED stays off or glows faintly.
- Wrong pin number: the pin in your code must match the pin your LED is connected to.
- Missing delay(): without delay(), the LED toggles so fast you cannot see it blink.
- LED inserted backwards: the long leg (anode) must go to the positive side.
Project 2: Read a push button and control an LED
The Button LED project reads a push button on pin 2 and lights the LED while the button is held down.
Wiring
Standard 4-leg tactile buttons connect their legs in pairs. Use two diagonally opposite legs:
- Connect one leg of the push button to pin 2.
- Connect the diagonally opposite leg to GND.
- Connect an LED to pin 13 through a 220 Ω resistor, or use the built-in LED.
The internal pull-up resistor (see Digital input) replaces an external resistor for the button.
The Button LED code
const int BUTTON_PIN = 2;
const int LED_PIN = 13;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // Button with internal pull-up
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) { // Button pressed (LOW because of pull-up)
digitalWrite(LED_PIN, HIGH); // Turn LED on
} else {
digitalWrite(LED_PIN, LOW); // Turn LED off
}
}
How it works
Because of the pull-up, the pin reads LOW while the button is pressed (see Digital input). The if statement checks the button state on every pass through loop() and switches the LED accordingly.
As a next step, change the code so that each press toggles the LED instead of holding it on. To make it work, store the previous button state and react only when it changes from HIGH to LOW (edge detection). Button contacts also bounce for a few milliseconds, so ignore further changes for about 50 ms after each press (debouncing).
Project 3: Read a potentiometer and print to the Serial Monitor
The Analog Reader project reads the voltage from a potentiometer on pin A0 and prints the value to the Serial Monitor.
Wiring
- Connect the middle pin of the potentiometer to Arduino pin A0.
- Connect one outer pin of the potentiometer to 5 V.
- Connect the other outer pin to GND.
The Analog Reader code
const int SENSOR_PIN = A0;
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN); // Read analog value (0-1023)
Serial.print("Sensor Value: ");
Serial.println(sensorValue); // Print value to Serial Monitor
delay(250); // Short delay to keep output readable
}
How it works
A potentiometer is a resistor with a sliding contact (the wiper, the middle pin). With the outer pins at 5 V and GND, the wiper outputs a voltage between the two that depends on the knob position. This arrangement is called a voltage divider. As you turn the knob, the voltage on pin A0 changes between 0 V and 5 V. analogRead() converts that voltage into a number from 0 to 1023, and the sketch prints a new value every 250 ms.
The same analogRead() call works for sensors that output a voltage between 0 and 5 V, such as a light-dependent resistor (LDR) in a voltage divider, analog temperature sensors and soil moisture sensors. Ultrasonic distance sensors such as the HC-SR04 work differently: they report distance as the length of a digital pulse. The HC-SR04 tutorial and the DIY Arduino radar show how to read it.
Troubleshooting common errors
Connection and upload errors
| Problem | Cause | Fix |
|---|---|---|
| Wrong board selected | The IDE is set to a different board than the one connected | Open the Board & Port Selector and choose the connected board (e.g. Arduino Uno) |
| Wrong port | The IDE points to a different serial port | Select the correct port in the Board & Port Selector; unplug and replug the USB cable to identify the right port |
| Bad USB cable | Some USB cables are charge-only and carry no data | Use a cable that supports data transfer; try another cable if the board is not detected |
| Upload failed | Board not responding, port blocked by another program, or a module such as an HC-05 on pins 0 and 1, which the USB connection also uses | Close the Serial Monitor, disconnect modules from pins 0 and 1, press the reset button on the board and upload again |
| Board not detected | Missing USB driver or faulty connection. Clone boards (low-cost copies of the Uno) often use a CH340 or CP2102 USB-to-serial chip, which needs its own driver | Install the CH340 or CP2102 driver, try a different USB port or reinstall the Arduino IDE with its drivers |
Code and output errors
| Problem | Cause | Fix |
|---|---|---|
| Compile errors | Syntax mistakes in your code (missing semicolons, brackets, typos) | Read the orange error message in the console; it shows the line number and the type of error |
| Serial Monitor shows unreadable characters | Baud rate mismatch between code and Serial Monitor | The baud rate in Serial.begin() must match the dropdown at the bottom of the Serial Monitor |
| Serial Monitor is blank | Serial not started or wrong port selected | Check that Serial.begin() is in setup() and the right board and port are selected |
| LED does not light up | LED inserted backwards or missing resistor | Check the LED polarity (long leg to positive) and put a 220 Ω resistor in the circuit |
| “avrdude” error on upload | avrdude is the upload program the IDE runs in the background for boards with an AVR chip (the chip family of the ATmega328P), such as the Uno; this error means it could not communicate with the board | Check the USB connection, select the right board and port, and press reset before uploading |
If nothing in the tables helps, check the wiring again, restart the Arduino IDE and try a different USB port.
Frequently asked questions
Arduino sketches are written in C++. The Arduino framework provides ready-made functions such as digitalWrite(), analogRead() and Serial.println(), so you can start with a small part of the language.
Yes. Once a sketch is uploaded, the board runs on its own. Power it with 7 to 12 V on the power jack (the round barrel socket) or on the VIN pin (the voltage input pin). A 5 V USB power supply such as a phone charger or power bank also works. You need a computer only to write and upload new sketches. You can also do that in the browser with the Arduino Cloud.
What to learn next
On my OmArm Zero robotic arm, the Serial Monitor is the first tool I use. Before the first screw goes in, I send the command HOME at 115200 baud, and all six servos (five joints and the gripper) move to their 0-degree home position.
Next tutorials, in a suggested order:
- Microcontroller Programming Types Explained: the main approaches to programming microcontrollers, from graphical tools and C++ to Python and professional IDEs.
- Arduino Servo Control Guide: SG90, PWM, and PCA9685: control servo motors for robotic arms and pan-tilt systems.
- Ultrasonic Sensor Distance Measurement: measure distance without contact using the HC-SR04 sensor.
- Controlling DC Motors with L298N and Motor Shield: drive DC motors for robot cars and mobile platforms.
- Arduino Bluetooth Module Tutorial (HC-05/HC-06): add wireless Bluetooth control to your projects.
- Building a Line Following Robot for Beginners: combine sensors and motors into your first autonomous robot.
- DIY 6-DOF Robotic Arm with Bluetooth Control: a robotic arm with six degrees of freedom (6-DOF) that combines servo control, Bluetooth and the basics from this guide.

2 thoughts on “Arduino Programming Basics: Complete Beginner Guide to Your First Sketch”