Think of it as a tiny, dedicated computer on a single chip.
Your Arduino board is a user-friendly way to program and use a microcontroller.
Take a moment to look at the board in front of you. Let's identify the key parts.
The Brain: The large black chip is the ATmega328P microcontroller.
The Power/Program Port: The USB port you're connected to.
The Integrated Development Environment is where you'll write, check, and upload your code.
Open it up now. Let's make sure it's set up for your board.
Before you can upload code, you need to tell the IDE what you're using.
This is the most common point of failure. If you can't upload, always check these two settings first!
1. Verify (✓): Compiles your code. It checks for syntax errors, just like in C++. It doesn't send anything to the board.
2. Upload (→): First, it verifies your code. If there are no errors, it sends the compiled program to your Arduino board to run.
Good news! You've been learning C++ for 6 weeks. The Arduino language is just a set of C++ functions and libraries.
This means you already know:
The main difference is the program structure.
// You know all this!
int count = 0;
for (int i = 0; i < 10; i++) {
count += i;
}
if (count > 40) {
// do something
}
Every Arduino program (called a "sketch") must have two functions:
setup()
loop()
setup()
finishes.// This code runs only one time
void setup() {
// Put your setup code here
}
// This code runs over and over, forever!
void loop() {
// Put your main code here
}
Every Arduino Uno has a built-in Surface-Mount LED labeled 'L'.
This LED is internally connected to Digital Pin 13. By controlling Pin 13, we can control this LED. It's the perfect way to test if our code is working!
To control a pin (like the one for our LED), we need to know three basic commands.
pinMode(pinNumber, MODE)
setup()
.
digitalWrite(pinNumber, VALUE)
delay(milliseconds)
In our setup()
function, we must declare Pin 13 as an output pin, because we want to send a signal *out* to the LED.
The Arduino also has a built-in variable, LED_BUILTIN
, which is a convenient way to refer to Pin 13.
Using LED_BUILTIN
makes our code more portable to other types of Arduino boards.
void setup() {
// Set the built-in LED pin (13) as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// Main code will go here...
}
Inside our infinite loop()
, we will perform a sequence of actions:
Then, because it's in a loop, this sequence will repeat forever!
void loop() {
// Turn the LED on (HIGH is the voltage level)
digitalWrite(LED_BUILTIN, HIGH);
// Wait for a second
delay(1000);
// Turn the LED off by making the voltage LOW
digitalWrite(LED_BUILTIN, LOW);
// Wait for a second
delay(1000);
}
Time to get your hands dirty!
Type the complete code on the right into your Arduino IDE. You can also find it under File > Examples > 01.Basics > Blink.
Once you've typed it in:
Watch your board. The 'L' LED should start blinking!
// The complete "Blink" sketch
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
delay(1000); // wait for a second
}
Congratulations, you're now a microcontroller programmer!
Let's experiment. Try to make the following changes to your code and upload again after each one:
delay()
calls?This is the core of physical computing: write code, upload, and see the result in the real world.
Now that you've used a pin, let's understand the different groups of pins on your board.
We'll look at each section one by one.
These pins don't run code, they provide electricity.
These are special "listening" pins.
These are the general-purpose workhorses.
Notice the tilde symbol (~) next to some of the digital pins?
These are special digital pins that can simulate an analog output. This is called Pulse Width Modulation (PWM).
analogWrite(pin, value)
where value is 0-255.How can we see what our Arduino is thinking? Or print the value of a variable for debugging?
The Serial Monitor is a text window in the Arduino IDE that lets the Arduino send messages back to your computer over the USB cable.
It is an absolutely essential tool for debugging your code!
You can open it by clicking the magnifying glass icon in the top-right of the IDE.
To use it, you need two new commands:
Serial.begin(9600);
setup()
function.Serial.println("Hello!");
Serial.println(myVariable);
void setup() {
// Start serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
// Print a message and wait a second
Serial.println("Hello from my Arduino!");
delay(1000);
}
Let's combine what we know.
Write a sketch that:
counter
and initializes it to 0.setup()
, initializes the Serial Monitor.loop()
, it prints the current value of counter
, increments counter
by 1, and waits for half a second.Upload the code and open the Serial Monitor. You should see a list of numbers counting up.
Let's combine what we know.
Write a sketch that:
counter
and initializes it to 0.setup()
, initializes the Serial Monitor.loop()
, it prints the current value of counter
, increments counter
by 1, and waits for half a second.Upload the code and open the Serial Monitor. You should see a list of numbers counting up.
// Your counting code here!
int counter = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Count: ");
Serial.println(counter);
counter++; // or counter = counter + 1;
delay(500);
}
The Serial
object is your powerful communication link to the computer.
We've used Serial.println()
which prints data and then adds a new line character (like hitting Enter).
There's also Serial.print()
which prints data without moving to the next line.
This is extremely useful for building up messages on a single line before finishing with a println()
.
int sensorValue = 123;
void loop() {
// Use print() to build the line
Serial.print("Sensor Value: ");
Serial.print(sensorValue);
Serial.print(" | Time: ");
// Use println() to finish the line
Serial.println(millis());
delay(1000);
// Output looks like:
// Sensor Value: 123 | Time: 4567
}
You can print almost any data type: int
, float
, char
, and strings.
Serial.print()
can also take a second argument to change the number base for integer types.
int value = 65;
void setup() {
Serial.begin(9600);
Serial.print("Decimal: ");
Serial.println(value, DEC); // Prints "65"
Serial.print("Hex: ");
Serial.println(value, HEX); // Prints "41"
Serial.print("Binary: ");
Serial.println(value, BIN); // Prints "1000001"
Serial.print("Character: ");
Serial.println((char)value); // Prints "A"
}
void loop() {}
Your Arduino can also receive data sent from the Serial Monitor. This makes your projects interactive!
Serial.available()
if
statement: if (Serial.available() > 0)
Serial.read()
Serial.available()
check.void loop() {
// Is there any data waiting for us?
if (Serial.available() > 0) {
// Yes! Let's read the first character.
char incomingByte = Serial.read();
// Now we can do something with it...
Serial.print("I received: ");
Serial.println(incomingByte);
}
}
This sketch demonstrates two-way communication.
It waits for you to type something into the Serial Monitor and press Send.
It then reads what you sent, one character at a time, and "echoes" it back to the monitor.
This is a fundamental pattern for creating command-based projects.
void setup() {
Serial.begin(9600);
Serial.println("Send me a message and I will echo it!");
}
void loop() {
// if there's any serial data available
if (Serial.available() > 0) {
// read the incoming character:
char incomingChar = Serial.read();
// echo it back to the serial monitor:
Serial.print("You sent: ");
Serial.println(incomingChar);
}
}
setup()
function (runs once) and a loop()
function (runs forever).LED_BUILTIN
).pinMode()
, digitalWrite()
, and delay()
.