← Back to site

Documentation

MCU Programming Basics

Learn the fundamental concepts of microcontroller programming with practical examples.

Development Environment Setup

Arduino IDE

The most beginner-friendly option for getting started with microcontroller programming.

PlatformIO

For more advanced development with better debugging capabilities.

Basic Program Structure

Every MCU program follows this basic structure:

Example Arduino Program

// Include necessary libraries
#include <Arduino.h>

// Global variables and constants
const int LED_PIN = 13;
int sensorValue = 0;

// Setup function - runs once at startup
void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  pinMode(A0, INPUT);
}

// Loop function - runs continuously
void loop() {
  sensorValue = analogRead(A0);
  
  if (sensorValue > 512) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
  
  Serial.println(sensorValue);
  delay(100);
}

Digital I/O Operations

Digital Output

Control LEDs, relays, and other digital devices:

digitalWrite(pin, HIGH);  // Turn on (3.3V or 5V)
digitalWrite(pin, LOW);   // Turn off (0V)

Digital Input

Read button states and digital sensors:

int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
  // Button is pressed
}

Best Practices

  • Use meaningful variable names
  • Comment your code extensively
  • Break complex functions into smaller ones
  • Use constants for pin definitions
  • Avoid long delays in main loop