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

  1. Plug the USB cable into the Arduino and your computer.

  2. The power LED on the Arduino should turn on.

  3. Windows may install a driver automatically; on macOS and Linux it usually just works.


3. Open the Arduino IDE and select your board

  1. Start Arduino IDE.

  2. Go to Tools → Board and choose your board, for example:

    • Arduino Uno

    • Arduino Nano

    • Arduino Mega or Mega 2560, etc.

  3. Go to Tools → Port and pick the port that shows something like:

    • COM3 (Arduino Uno) on Windows

    • /dev/ttyACM0 or /dev/ttyUSB0 on 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.

  1. In the IDE: File → Examples → 01.Basics → Blink

  2. A new window with code opens. It should look like:

// Blink: Turns an LED on for one second, then off for one second, repeatedly. void setup() { // put your setup code here, to run once: pinMode(LED_BUILTIN, OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(LED_BUILTIN, HIGH); // LED on delay(1000); // wait 1 second digitalWrite(LED_BUILTIN, LOW); // LED off delay(1000); // wait 1 second }

What this means

  • setup() runs once when the board powers up or resets.

  • loop() runs over and over forever.

  • LED_BUILTIN is 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

  1. Click the ✔ Check button (Verify / Compile).

    • The IDE compiles your code. If you made no changes, it should pass.

  2. Click the → Upload button.

    • The IDE compiles (again) and uploads over USB.

  3. 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:

void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(200); // 0.2 seconds digitalWrite(LED_BUILTIN, LOW); delay(200); }

Hit Upload again and watch the change.


7. Typical workflow after this

Every Arduino project is the same basic pattern:

  1. Plan hardware (which pins go to LEDs, buttons, sensors, etc.).

  2. 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.

  3. Use libraries:

    • Example: for an I²C sensor, install its library from Sketch → Include Library → Manage Libraries…

    • Load example from File → Examples → [Library Name].

  4. 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_BUILTIN vs 13).

  • If driving external components, double-check wiring and ground.

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How To Connect Stm32 To PC?

How do you set up ADC (Analog-to-Digital Converter) in STM32?