Indice
- 4.1 Getting Your Materials and Circuit Ready
- 4.2 Building the Circuit and Connecting the LED
- 4.3 Your First Sketch: Turning On an LED
- 4.4 Uploading and Running the Program
- 4.5 Making the LED Blink
- 4.6 Understanding the Program Instruction by Instruction
- 4.7 Changing the Blinking Speed
- 4.8 Exercises and First Modifications
- Coming Up in the Next Chapter...
4.1 Getting Your Materials and Circuit Ready
Now that we’ve covered the basic principles of electronics and the fundamentals of programming, it’s time to put what we’ve learned into practice.
In this chapter, we’ll build our first project with the Arduino UNO R3: a circuit that can turn an LED on and off using a program.
It’s a simple exercise, but a very important one, because it will show us how software can control an electronic component physically wired to the board.
The Materials You’ll Need
To build the circuit, we’ll need the following components:
- Arduino UNO R3, the board that will run the program and control the LED.
- An LED (light-emitting diode), a diode that gives off light when current flows through it in the right direction.
- A 220 Ω or 330 Ω resistor, needed to limit the current flowing through the LED and protect it.
- A breadboard (also called a solderless breadboard), a board that lets us make electrical connections without soldering the components.
- Two or more jumper wires, to connect the components to each other and to the Arduino.
- A USB cable compatible with your board, to connect the Arduino to your computer and upload the program.
Before we start, it helps to lay all the components out on the table and check that we have everything we need.
Getting to Know the Components
The Arduino UNO R3 board is the heart of our project. Its microcontroller will run the instructions in our program and control a digital output, which is where we’ll connect the LED.
The LED is the component that lets us see the result of our program. Once the circuit is wired up correctly, we’ll be able to switch it on and off.
The resistor is essential because it stops too much current from flowing through the LED. We should never connect an LED straight to a power supply without a proper way of limiting the current.
The breadboard lets us build the circuit easily and change it whenever we like. Inside it there are metal contacts that electrically connect certain holes to each other, so to use it properly, it’s important to know how these connections are laid out.
Finally, the jumper wires let us make the connections between the Arduino, the breadboard and the electronic components.
How Will the Circuit Work?
The principle is simple: the Arduino will set a digital output to either a high or a low level. Depending on how we’ve wired things up, this will either let current flow through the LED or cut it off.
The resistor will be connected in series with the LED to limit the current passing through it. The circuit will also be connected to the Arduino’s GND terminal (ground), which is the common electrical reference.
For our first exercise, we’ll use digital pin 13. This way, we’ll learn how to control one specific output on the board from our program.
Before powering up the circuit, we’ll need to check that all the connections are correct and that the LED is connected with the right polarity.
In the next section, we’ll build the circuit step by step and write our first sketch to turn the LED on. After that, we’ll change the program to make it blink automatically.

4.2 Building the Circuit and Connecting the LED
Before we write the program, we need to build the circuit that will let the Arduino control the LED. Let’s start by getting our components ready and checking how they need to be connected.
How the Breadboard Works
The breadboard lets us connect electronic components without any soldering. Its holes aren’t all independent: beneath the surface there are metal contacts that electrically link certain groups of holes together.
In the central area, the holes are generally connected in groups of five along each column, while the central channel (or centre gap) electrically separates the two sides. Any side lines, often marked with + and − symbols, are normally used to distribute power (these are known as power rails), but their layout can vary depending on the breadboard model.
Before building the circuit, it’s important to know how the contacts are laid out on your own board.
Identifying the LED’s Polarity
An LED has two terminals (or leads), called the anode and the cathode, and it must be connected with the correct polarity.
On standard LEDs with leads of different lengths, you’ll generally find that:
- Anode: the longer lead.
- Cathode: the shorter lead, often also recognisable by the flat edge on the body of the LED.
When the LED is connected the right way round and a suitable current flows through it, it gives off light. If it’s connected backwards, it normally won’t light up.
Connecting the LED and the Resistor
For our circuit, we’ll use digital pin 13 on the Arduino UNO R3. The connections will be made in the following order:
Digital pin 13 → Resistor → LED anode → LED cathode → GND
The resistor can go either before or after the LED, as long as it’s connected in series with it: what matters is that the same current flows through both components.
For this exercise, we can use a 220 Ω or 330 Ω resistor. Its job is to limit the current flowing through the LED, so that a direct connection can’t damage it.
The Arduino’s GND terminal (ground) is the circuit’s electrical reference and completes the path for the current.
Double-Checking the Circuit
Before connecting the Arduino to the computer, let’s check the circuit carefully:
- The LED is inserted properly in the breadboard, and its leads aren’t accidentally connected to the same electrical contact.
- The resistor is connected in series with the LED.
- Digital pin 13 is connected to the branch of the circuit that includes the resistor and the LED.
- The LED’s cathode is connected to GND.
- There are no accidental connections between pin 13 and GND that could cause a short circuit.
At this point, the circuit is ready to be controlled by the program. However, the LED won’t necessarily light up until the Arduino sets pin 13 to the appropriate level.
In the next section, we’ll write our first sketch, configuring the pin as an output and using the digitalWrite() function to turn the LED on.

4.3 Your First Sketch: Turning On an LED
Now that we’ve prepared our materials and built the circuit, we can write our first Arduino program. The goal is simple: to turn the LED on and keep it on.
Drawing the Program’s Flowchart
First, let’s break the process down into its individual steps and work out the flow those steps need to follow.
In this first, simple case, there are only two steps:
- Set the LED’s pin as an output.
- Turn on the LED.
The flow is purely sequential from start to finish, with no branches, so a flowchart (or block diagram) might look something like this:

To write the program, we’ll use the Arduino IDE, the development environment we introduced in the previous chapter.
Writing the Program
Let’s open the Arduino IDE and enter this code:
/* Exercise N.04.3
Turn on the LED on pin 13 */
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT); // Set pin 13 as an output pin
digitalWrite(13, HIGH); // Turn on the LED on pin 13
}
void loop() {
// put your main code here, to run repeatedly:
}
Let’s save the project with a meaningful name, for example EsercizioN04.3.
What Does the Program Do?
Let’s go through the main instructions:
- void setup() contains the instructions that run only once, when the Arduino is powered on or restarted.
- pinMode(13, OUTPUT); configures digital pin 13 as an output, which lets the Arduino control the LED connected to this pin.
- digitalWrite(13, HIGH); sets pin 13 to a logic high level, supplying a voltage of about 5 V relative to GND on the Arduino UNO R3. With the circuit connected correctly, current flows through the resistor and the LED, and the LED lights up.
- void loop() contains the instructions that the Arduino repeats continuously. In this first program, the block is empty because we don’t need to repeat any operation: the pin simply stays set to high.
Notice also that every instruction ends with a semicolon (;), while the curly brackets (or curly braces) { } mark out blocks of instructions.
Why Does the LED Stay On?
The digitalWrite(13, HIGH); instruction runs during the initialisation phase, inside setup(). Once the pin has been set to high, the Arduino keeps it in that state until the program or the circuit changes it, or until the board is restarted.
So the program doesn’t need to keep sending the instruction to keep the LED on: it’s enough to set the output correctly.
If we’ve also connected an external LED to pin 13, the built-in LED on the board, marked with the letter L, will normally light up too, because it’s connected to the same output.
In the next section, we’ll see how to verify the program, upload it to the Arduino board and check the result.
4.4 Uploading and Running the Program
Now that we’ve written our first sketch, we can transfer it to the Arduino board and check whether the LED lights up as expected.
Verifying the Program
Before transferring the code, it’s good practice to check that it’s been written correctly.
In the Arduino IDE, we’ll find the Verify button, usually shown as a tick (or checkmark) symbol. Clicking this button makes the development environment check the code and compile it, turning it into a form the microcontroller can run.
If the program contains syntax errors, the IDE will display a message to help us find them. If verification completes successfully, we can go ahead and upload.
It’s important to remember that a successful compilation doesn’t guarantee that the program will work exactly as we want: the circuit connections and the logic of the code need to be right too.
Connecting the Arduino to the Computer
Let’s connect the Arduino board to the computer using a suitable USB cable. The board will receive power and be able to communicate with the development environment.
In the Arduino IDE, we need to select the correct board, in our case Arduino UNO, and the communication port it’s connected to. The name of the port may vary depending on your computer and how the board is connected.
If the board isn’t listed among the available ones, let’s check the USB cable, the connection and the IDE settings.
Uploading the Program
Let’s click the Upload button, usually shown as a right-pointing arrow.
The IDE verifies and compiles the program, then transfers it to the Arduino board. During this process, some of the LEDs on the board may blink: that’s perfectly normal.
Once the upload has finished, the microcontroller starts running the program. If the circuit has been built correctly, the external LED connected to pin 13 lights up. The built-in LED, marked with the letter L, will normally light up too.
If the LED Doesn’t Light Up
If the result isn’t what we expected, don’t panic: let’s check a few key points.
- Connections: let’s check that pin 13 is connected to the branch made up of the resistor and the LED, and that the LED’s cathode is connected to GND.
- Polarity: let’s check that the LED is the right way round.
- Resistor: let’s check that it’s connected in series with the LED.
- Program: let’s check that the pinMode(13, OUTPUT); and digitalWrite(13, HIGH); instructions are there.
- Board and port: let’s make sure we’ve selected the correct board and port in the IDE.
If the built-in LED lights up but the external one doesn’t, the program is probably working, and the problem may lie in the external circuit.
And that’s our first experiment complete: the Arduino runs a program and controls a digital output to turn on an LED.
In the next section, we’ll modify the program to get an even more interesting result: making the LED blink automatically, alternating between on and off periods.

4.5 Making the LED Blink
Now that we know how to turn on an LED with Arduino, we can modify the program to make it blink automatically, alternating between periods when it’s on and periods when it’s off.
Drawing the Program’s Flowchart
To achieve this, we need to give the Arduino four basic instructions:
- Turn on the LED.
- Wait for one second.
- Turn off the LED.
- Wait for one second, then repeat the sequence.

Writing the New Program
Let’s replace the previous code with this sketch:
/* Exercise N.04.5
Blink the LED on pin 13 at 1-second intervals */
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT); // Set pin 13 as an output pin
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13, HIGH); // Turn on the LED on pin 13
delay(1000); // Wait 1 second
digitalWrite(13, LOW); // Turn off the LED on pin 13
delay(1000); // Wait 1 second
}
Once we’ve entered the code, we click Verify and then Upload, as we saw in the previous section. If the circuit is connected correctly, the LED will come on for one second, go off for one second and keep repeating this sequence.
How Does the Program Work?
Let’s look at the instructions we’ve added.
- digitalWrite(13, HIGH); sets pin 13 to a logic high level, turning the LED on.
- delay(1000); temporarily pauses the execution of the following instructions for 1000 milliseconds, which is one second.
- digitalWrite(13, LOW); sets pin 13 to a logic low level, turning the LED off.
- The second delay(1000); waits another second before the program starts the sequence again.
Notice that the pinMode(13, OUTPUT); instruction has stayed inside setup(), because the pin only needs to be configured once, at start-up.
The instructions that control the LED, on the other hand, are inside loop(). This block runs continuously, so when the Arduino reaches the end of the sequence, it automatically starts again from the beginning.
We don’t need to write an explicit instruction to repeat the program: it’s the way loop() works that does it for us.
What Have We Learned?
In this simple example, we’ve used three basic instructions:
- pinMode() to configure a pin;
- digitalWrite() to set a digital output;
- delay() to add a pause.
We’ve also seen how a sequence of instructions placed inside loop() can be repeated continuously, creating automatic behaviour.
In the next section, we’ll analyse the program instruction by instruction, to better understand the role of each element and learn how to modify it with confidence.
4.6 Understanding the Program Instruction by Instruction
We’ve written a program that can make an LED blink, but to really learn how to program, it’s important to understand why each instruction is there and what effect it has.
Let’s take another look at the code we used in the previous section:
Configuring the Pin
void setup() {
pinMode(13, OUTPUT);
}
The setup() function runs only once, when the Arduino starts up or is restarted.
Inside it, we find the pinMode(13, OUTPUT) instruction, which configures digital pin 13 as an output. This way, the Arduino can control the electrical level of the pin and drive the connected LED.
The number 13 identifies the pin being used, while OUTPUT tells the Arduino that it should work as an output.
Turning the LED On and Waiting
Unlike the instructions in the setup() function, the instructions inside the loop() function are repeated from the beginning each time the end is reached, in an infinite loop.
Inside this function, we find:
digitalWrite(13, HIGH);
delay(1000);
The first instruction sets pin 13 to a logic high level (HIGH), normally bringing it to about 5 V relative to GND on the Arduino UNO R3. Current can then flow through the resistor and the LED, which lights up if the circuit is connected correctly.
The second instruction, delay(1000);, adds a pause of 1000 milliseconds (one second) before moving on to the next instructions.
The value in brackets (parentheses) sets the length of the pause: for example, delay(500); is half a second.
Turning the LED Off and Waiting Again
digitalWrite(13, LOW);
delay(1000);
The digitalWrite(13, LOW); instruction sets pin 13 to a logic low level, normally close to 0 V relative to GND. In this circuit, the LED turns off because there’s no longer a big enough potential difference (voltage) to make the current needed to light it flow.
Next, the second delay(1000); adds another one-second pause.
At this point, the loop() block has reached its end. The Arduino therefore goes back to the start of the block and repeats the same sequence: on, wait, off and wait again.
The Role of Brackets and Semicolons
In the code, we’ve come across a few elements that are important to recognise:
- The round brackets (parentheses) () contain the parameters passed to functions, such as the pin number or the length of the pause.
- The curly brackets (curly braces) { } mark out the blocks of instructions belonging to setup() and loop().
- The semicolon ; marks the end of a single instruction.
Forgetting a semicolon or a bracket can cause an error during compilation.
This example helps us understand a fundamental principle: a program controls the behaviour of a device through precise instructions, carried out in a set order.
In the next section, we’ll change the waiting times to alter the blinking speed and see how a simple change to the code can produce different behaviour.

4.7 Changing the Blinking Speed
We can change how fast the LED blinks by changing the values in the delay() instructions, without touching the circuit.
Remember that delay() expresses the waiting time in milliseconds: 1000 is one second, while 500 is half a second.
For example, to make the LED blink faster, we can use this code:
/* Esercizio N.04.7
Fai lampeggiare il led sul pin 13 con intervalli di 0,2 secondi */
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT); // Inizializza il pin 13 come pin di Output
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13, HIGH); // Accendi il led sul pin 13
delay(200); // Attendi 200 millisecondi
digitalWrite(13, LOW); // Spegni il led sul pin 13
delay(200); // Attendi 200 millisecondi
}
In this case, the LED stays on for 200 milliseconds and off for another 200 milliseconds, completing one cycle in about 400 milliseconds.
The shorter the waiting time, the faster the blinking; the longer the waiting time, the slower it will be.
We can also use different values for the on and off times. For example, if we set delay(1000); after turning the LED on and delay(300); after turning it off, the LED will stay on for longer than it stays off.
Using the Built-In LED on the Board
The Arduino UNO R3 board has a small built-in LED, marked with the letter L and connected to digital pin 13. This means we can use the same program to make it blink, without connecting an external LED or building a circuit on the breadboard.
All we need to do is connect the Arduino to the computer and upload the sketch. This is very handy for trying out and tweaking programs, even when we don’t have any external components on hand.
In the next section, we’ll put what we’ve learned into practice with a few simple exercises.
4.8 Exercises and First Modifications
Now that we’ve built our first program, we can consolidate what we’ve learned by changing the code and observing the results.
For these exercises, we can use the external LED connected to pin 13 or, if we prefer, the built-in LED on the Arduino UNO R3 board.
Exercise 1 – Changing the Blinking Speed
Modify the program so that the LED stays on for half a second and off for half a second.
Hint: change the values in the delay() instructions.
Exercise 2 – Creating an Uneven Blink
Modify the program so that the LED stays on for one second and off for two seconds.
Goal: use different waiting times for turning the LED on and off.
Exercise 3 – Making Three Quick Blinks
Modify the program so that the LED blinks three times quickly, then stays off for two seconds before repeating the sequence.
Hint: use delay(200); for the short pauses and delay(2000); for the final pause. You’ll need to organise the instructions so that the LED turns on and off three times before the longer pause.
Exercise 4 – Modifying the Program Without Looking at the Example
Try writing a program from scratch that performs this sequence:
- Turns the LED on for two seconds.
- Turns it off for half a second.
- Repeats the sequence continuously.
Try to write the code without copying the earlier examples. If you need to, go back over the explanations of setup(), loop(), pinMode(), digitalWrite() and delay().
Chapter Conclusion
With these first experiments, we’ve learned how to write an Arduino program, configure a digital output, control an LED and change the behaviour of the circuit through code.
We’ve also discovered that a program runs in a precise sequence and that the loop() function lets us repeat instructions continuously.
This knowledge is the foundation for our next projects. In the following chapters, we’ll learn how to use push buttons and sensors, so that the Arduino doesn’t just run a preset sequence but can react to what the user does and to changes in its environment.
Coming Up in the Next Chapter...
So far, we’ve used the Arduino to give commands to the LED. In the next chapter, Chapter 5 - Arduino Digital Inputs: Controlling an LED with a Push Button, we’ll do the opposite: the Arduino will learn to listen to what’s happening outside the board.
We’ll connect a push button and find out how to make sure the Arduino knows for certain when we press it and when we release it. Then we’ll write our first interactive program: pressing the button will turn the LED on, and releasing it will turn the LED off. We’ll also teach the Arduino to make a simple decision: “if the button is pressed, do this; otherwise, do that.”
We’ll also learn how to track down errors methodically (in other words, how to troubleshoot and debug), checking one step at a time. Finally, we’ll try changing the circuit with a few exercises and a small challenge.
If you’re curious to see where this journey will take us, have a look at the page Beginner's Guide to Robotics with Arduino: there you’ll find the full contents list, from the first chapter to the final project, and you can choose whether to carry on in order or go back and review a topic.
If you’ve found this course useful and would like to explore other robotics or hobby electronics projects, you can keep reading by visiting the other content in the Electronics and Robotics section of the site, where you’ll find plenty more practical guides and step-by-step explanations.
I’d like to point out that some of the images in the course were generated by artificial intelligence (AI), and I apologise for any inaccuracies they may contain. The course itself was also written with the help of AI, but the structure is the result of my own experience: I’m a retired programmer and, over the course of my career, I’ve taught several courses. So I chose the topics, organised the whole structure of the course and reviewed the text myself.
I care a great deal about the quality and originality of the content I publish. That’s why I’d prefer it not to be copied or republished elsewhere. If you think this guide could be useful to other people, though, I’ll be really happy if you share the link to the page: it’s the right way to spread the information and to support the work behind every article.
Thank you
Luciano (Capitan Farloc)

English (United Kingdom)
Italiano (Italia)