Knowing these functions by heart gets you ahead of 95% of Arduino programmers.
If you’re a STEM senior high school student or a freshman engineering student just starting out with Arduino, you’ve probably noticed something: almost every tutorial uses the same handful of functions over and over again. That’s not a coincidence — these functions are the building blocks of nearly every Arduino project, from blinking an LED to building a full robot.
This post walks through the 10 functions you’ll use the most, what they do, and simple examples of each. Once these feel natural, you’ll be able to read (and write) almost any beginner-to-intermediate Arduino sketch.
setup()
setup()Every Arduino sketch needs this function. It runs once, right when your board powers on or resets, and it’s where you configure things — like setting pin modes or starting serial communication.
void setup() {
pinMode(13, OUTPUT);
}
Why it matters: Think of setup() as your project’s “getting ready” phase. Anything that should happen only once — not repeatedly — belongs here.
loop()
If setup() runs once, loop() runs forever, over and over, for as long as the board has power. This is where your actual program logic lives.
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
Why it matters: Almost everything your Arduino “does” while running happens inside loop(). Understanding that it repeats endlessly is key to understanding how Arduino programs behave differently from a normal C++ program that runs once and stops.
pinMode()
This function tells the Arduino how a specific pin should behave — as an input (reading a signal) or an output (sending a signal).
pinMode(13, OUTPUT); // Pin 13 will send signals out pinMode(2, INPUT); // Pin 2 will read signals in
Why it matters: Skipping this step is one of the most common beginner mistakes. If you forget to set a pin’s mode, it may not behave the way you expect.
digitalWrite()
Used to set a digital pin to either HIGH (on, usually 5V or 3.3V) or LOW (off, 0V). This is how you control things like LEDs, buzzers, and relays.
digitalWrite(13, HIGH); // Turn the LED on digitalWrite(13, LOW); // Turn the LED off
Why it matters: This is the “on/off switch” function — foundational for controlling any simple electronic component.
digitalRead()
This function reads whether a digital pin is HIGH or LOW. It’s commonly used with push buttons, switches, and digital sensors.
int buttonState = digitalRead(2);
if (buttonState == HIGH) {
// Button is pressed
}
Why it matters: It’s your Arduino’s way of “listening” for a yes/no, on/off type of input from the physical world.
analogRead()
Unlike digitalRead(), which only sees HIGH or LOW, analogRead() reads a range of values (0–1023) from analog pins. This is essential for sensors like potentiometers, temperature sensors, and light sensors.
int sensorValue = analogRead(A0);
Why it matters: The real world isn’t just on/off — temperature, light, and sound all vary continuously. analogRead() lets your Arduino sense that variation.
analogWrite()
This function outputs a simulated analog signal using PWM (Pulse Width Modulation) on compatible pins (usually marked with a ~). It’s commonly used to dim LEDs or control motor speed.
analogWrite(9, 128); // About 50% brightness (0-255 range)
Why it matters: It bridges the gap between simple on/off control and true analog control, which is important once your projects get more advanced (like speed-controlled motors or fading lights).
delay()
Pauses your program for a set number of milliseconds. It’s one of the first functions beginners learn, usually through the classic “blink an LED” project.
delay(1000); // Pause for 1 second
Why it matters: It’s simple and intuitive, but be careful — delay() pauses your entire program, which can cause issues in more advanced projects where multiple things need to happen at once (that’s where millis() comes in later).
Serial.begin() and Serial.print() / Serial.println()
Serial.begin() starts communication between your Arduino and your computer (usually through the Serial Monitor), while Serial.print() and Serial.println() send data to be displayed there.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Hello from Arduino!");
delay(1000);
}
Why it matters: This is your debugging lifeline. When your code isn’t behaving as expected, printing values to the Serial Monitor is often the fastest way to figure out what’s actually happening.
map()
This function re-scales a number from one range to another — extremely useful when converting sensor readings into more meaningful values.
int sensorValue = analogRead(A0); // Range: 0-1023 int mappedValue = map(sensorValue, 0, 1023, 0, 255); // Rescaled to 0-255
Why it matters: Sensors and outputs often use different numeric ranges. map() saves you from doing manual conversion math every time, which is especially handy in projects involving analog sensors and PWM outputs.
Putting It All Together
Here’s a simple example that uses several of these functions together — reading a potentiometer and using it to control the brightness of an LED, while also printing the values to the Serial Monitor:
int potPin = A0;
int ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(potPin);
int brightness = map(sensorValue, 0, 1023, 0, 255);
analogWrite(ledPin, brightness);
Serial.print("Sensor: ");
Serial.print(sensorValue);
Serial.print(" | Brightness: ");
Serial.println(brightness);
delay(100);
}
Notice how pinMode(), analogRead(), map(), analogWrite(), Serial.begin(), Serial.print(), and delay() all show up in this one short program? That’s exactly why mastering these 10 functions early pays off — they’re not isolated tricks, they’re the vocabulary you’ll use in almost every project you build.
Final Tips for Beginners
- Experiment, don’t just read. Type these examples into the Arduino IDE and upload them to a real board (or a simulator like Tinkercad Circuits) to see them in action.
- Break things on purpose. Try changing values, removing a
pinMode()call, or swappingHIGHandLOWto see what happens. Mistakes teach you more than perfect code does. - Use the Serial Monitor often. It’s the easiest way to understand what your program is actually doing behind the scenes.
Once these 10 functions feel like second nature, you’ll be ready to explore more advanced topics like interrupts, libraries, and non-blocking timing with millis() — but that’s a topic for another post.