Use this guide for quick reference on Arduino syntax, pin functions, and common circuit logic. The most important rule is to always define your pin modes in the setup function before trying to read or write to them.
What are the Basic Arduino Functions?
Every Arduino sketch requires two primary functions to run. The setup() function runs once when the board powers up or resets. Use this area to initialize variables, set pin modes, and start serial communication. The loop() function runs continuously after the setup finishes. This is where you place the main logic of your program, such as reading a sensor and updating an LED.
To control a pin, you use pinMode(pin, mode). The pin is the digital number on the board, and the mode is either INPUT, OUTPUT, or INPUT_PULLUP. Using INPUT_PULLUP activates an internal resistor that keeps the pin at a high voltage until a button pulls it to ground, which prevents the pin from "floating" and giving random readings.
Writing to a pin involves digitalWrite(pin, value), where the value is HIGH (5V or 3.3V) or LOW (0V). Reading a digital pin uses digitalRead(pin), which returns either HIGH or LOW. For analog signals, analogRead(pin) converts a voltage between 0V and 5V into a number between 0 and 1023. This 10-bit resolution allows you to detect small changes in voltage from a potentiometer or light sensor.
How Do You Handle Timing and Communication?
The delay(ms) function pauses the program for a specific number of milliseconds. For example, delay(1000) stops everything for 1 second. While simple, this function blocks all other code from running, meaning the board cannot read a button press while it is delaying. For non-blocking timing, use the millis() function. This returns the number of milliseconds passed since the board started. By storing the current time in a variable and subtracting it from the previous time, you can create a timer that allows other code to run simultaneously.
Communication with your computer happens via the Serial Monitor. Start this in setup() using Serial.begin(9600), where 9600 is the baud rate or speed of communication. Use Serial.print("Text") to send data to the screen. If you want the next piece of data to start on a new line, use Serial.println("Text"). This is the primary way to debug your code by printing variable values to see exactly what the board is thinking.
To control devices that require a range of power, such as dimming an LED or controlling a servo motor, use analogWrite(pin, value). This is not true analog output but Pulse Width Modulation (PWM). It toggles the pin on and off very quickly. The value ranges from 0 (always off) to 255 (always on). Only pins marked with a tilde symbol (~) on the board support PWM.
Which Variables and Data Types Should You Use?
Choosing the right data type saves memory, which is very limited on Arduino boards. Use bool for true or false values. Use int for whole numbers, typically ranging from -32,768 to 32,767. If you only need positive numbers up to 65,535, unsigned int is more efficient.
For very small numbers (0 to 255), use byte. This is ideal for PWM values or pin numbers. For numbers with decimals, use float. Be aware that floats take more processing power and memory than integers. If you are working with very large numbers that exceed 32,767, use long, which can handle values up to about 2 billion.
Constants should be defined using const int or #define. For example, const int ledPin = 13; tells the compiler that the value of ledPin will never change. This allows the board to optimize memory usage. Using a named constant instead of a raw number makes your code easier to read and modify later.
How Do You Structure Control Logic?
Control flow allows your board to make decisions. The if (condition) { } statement executes code only if the condition is true. You can expand this with else if for multiple possibilities and else for a default action. Use comparison operators like == for equality, != for not equal, > for greater than, and < for less than.
To combine multiple conditions, use logical operators. The AND operator && requires both conditions to be true. The OR operator || requires only one to be true. The NOT operator ! reverses the state of a boolean. For example, if (buttonState == LOW && timerActive == true) ensures the action only happens when the button is pressed and the timer is running.
Loops are used for repeating tasks. A for loop is best when you know exactly how many times to repeat, such as blinking an LED five times. A while loop continues as long as a condition remains true. Be careful with while loops, as a condition that never becomes false will cause the board to freeze in an infinite loop.
What Causes Circuits to Fail or Boards to Crash?
A common mistake is forgetting the current-limiting resistor for LEDs. Connecting an LED directly to a digital pin can draw too much current, which may burn out the LED or damage the Arduino pin. A resistor of about 220 ohms to 1k ohms is typically used to keep the current safe.
Floating pins occur when an input pin is not connected to either 5V or Ground. In this state, electrical noise from the air can cause digitalRead() to flip randomly between HIGH and LOW. To fix this, use a pull-up or pull-down resistor. A pull-up resistor connects the pin to 5V, ensuring it stays HIGH until a button pulls it to Ground.
Power issues often arise when using servos or motors. These components draw more current than the Arduino's 5V pin can provide. Powering a motor directly from the board can cause the voltage to drop, which resets the microcontroller. Use an external power supply for motors and connect the ground of the external supply to the ground of the Arduino to create a common reference point.
Short circuits happen when a power wire touches a ground wire directly. This can cause the board to overheat or permanently fail. Always double-check your wiring before plugging the USB cable into your computer. If the board feels hot to the touch, unplug it immediately and check for misplaced wires or solder bridges.