How to program an Arduino?
You “program” an Arduino by writing a sketch (a C/C++ program), compiling it, and uploading it to the board over USB. Let’s walk it from zero to blinking LED.
1. What you need
-
An Arduino board (Uno, Nano, Mega, etc.)
-
USB cable that matches your board (Uno: USB-B, newer: USB-C/micro-USB)
-
A computer (Windows / macOS / Linux)
-
Arduino IDE installed
If you don’t have the IDE yet, download it from the official Arduino site (Arduino IDE 2.x). Install it like any normal app.
2. Connect the Arduino
-
Plug the USB cable into the Arduino and your computer.
-
The power LED on the Arduino should turn on.
-
Windows may install a driver automatically; on macOS and Linux it usually just works.
3. Open the Arduino IDE and select your board
-
Start Arduino IDE.
-
Go to Tools → Board and choose your board, for example:
-
Arduino Uno -
Arduino Nano -
Arduino Mega or Mega 2560, etc.
-
-
Go to Tools → Port and pick the port that shows something like:
-
COM3 (Arduino Uno)on Windows -
/dev/ttyACM0or/dev/ttyUSB0on Linux -
/dev/cu.usbmodem…on macOS
-
If you only see one port, that’s probably it. If you see none, we’ll touch troubleshooting later.
4. Your first program: Blink
We’ll load the built-in Blink example, then look at the code.
-
In the IDE: File → Examples → 01.Basics → Blink
-
A new window with code opens. It should look like:
What this means
-
setup()runs once when the board powers up or resets. -
loop()runs over and over forever. -
LED_BUILTINis the on-board LED (on Uno it’s the LED on pin 13). -
pinMode(..., OUTPUT)tells Arduino we want to drive that pin. -
digitalWrite(..., HIGH/LOW)turns it on/off. -
delay(1000)pauses for 1000 ms (1 second).
5. Compile and upload
-
Click the ✔ Check button (Verify / Compile).
-
The IDE compiles your code. If you made no changes, it should pass.
-
-
Click the → Upload button.
-
The IDE compiles (again) and uploads over USB.
-
-
Watch the Arduino:
-
You’ll see the TX/RX LEDs flash during upload.
-
Then the built-in LED should start blinking once per second.
-
Boom — you just programmed an Arduino.
6. Change something (so it feels like your code)
Try making the LED blink faster:
Hit Upload again and watch the change.
7. Typical workflow after this
Every Arduino project is the same basic pattern:
-
Plan hardware (which pins go to LEDs, buttons, sensors, etc.).
-
In code:
-
In
setup(): configure pins (pinMode), start serial (Serial.begin(9600);), start libraries (like Wi-Fi, sensors, etc.). -
In
loop(): read inputs, do logic, control outputs.
-
-
Use libraries:
-
Example: for an I²C sensor, install its library from Sketch → Include Library → Manage Libraries…
-
Load example from File → Examples → [Library Name].
-
-
Combine multiple examples into your own sketch.
8. Quick troubleshooting tips
No COM port / board not found
-
Try another USB cable (some are power-only).
-
Try another USB port.
-
Check Tools → Board and Tools → Port.
-
On clones (especially Nanos with CH340), you might need a driver (CH340/CP2102).
Upload error: avrdude: stk500_getsync()
-
Wrong board type selected.
-
Wrong port selected.
-
Board “stuck” from a previous sketch using the serial at high speed → press RESET right before upload.
Code compiles but nothing happens
-
Check you used the right pin (e.g.
LED_BUILTINvs13). -
If driving external components, double-check wiring and ground.

评论
发表评论