0
0
Arduinoprogramming~5 mins

Documentation and pin mapping in Arduino

Choose your learning style9 modes available
Introduction

Documentation and pin mapping help you keep track of which parts of your Arduino board connect to your project. This makes your work clear and easy to fix or change later.

When you connect sensors or buttons to your Arduino and want to remember which pin is used for each.
When sharing your project with friends or online so others understand your wiring.
When debugging your project to quickly find if a pin is connected correctly.
When planning a bigger project with many parts to avoid confusion.
When updating or expanding your project and you need to know existing connections.
Syntax
Arduino
// Define pin numbers with clear names
const int ledPin = 13;
const int buttonPin = 2;

Use const int to name pins so you don’t have to remember numbers.

Choose meaningful names that describe what the pin controls.

Examples
This sets ledPin to 13, so you can use ledPin in your code instead of 13.
Arduino
// Map LED to pin 13
const int ledPin = 13;
This names pin 2 as buttonPin for easy reference.
Arduino
// Map button to pin 2
const int buttonPin = 2;
Here, three pins are named for different parts: LED, button, and sensor.
Arduino
// Multiple pins mapped
const int ledPin = 13;
const int buttonPin = 2;
const int sensorPin = A0;
Sample Program

This program uses pin mapping to name the LED and button pins. It reads the button and turns the LED on or off accordingly, printing the status to the serial monitor.

Arduino
// Pin mapping example
const int ledPin = 13;      // LED connected to digital pin 13
const int buttonPin = 2;    // Button connected to digital pin 2

void setup() {
  pinMode(ledPin, OUTPUT);   // Set LED pin as output
  pinMode(buttonPin, INPUT); // Set button pin as input
  Serial.begin(9600);        // Start serial communication
}

void loop() {
  int buttonState = digitalRead(buttonPin); // Read button state
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH); // Turn LED on
    Serial.println("Button pressed - LED ON");
  } else {
    digitalWrite(ledPin, LOW);  // Turn LED off
    Serial.println("Button not pressed - LED OFF");
  }
  delay(500); // Wait half a second
}
OutputSuccess
Important Notes

Always comment your pin mappings to explain what each pin does.

Use const to prevent accidental changes to pin numbers.

Keep your pin mapping at the top of your code for easy access and updates.

Summary

Pin mapping gives names to Arduino pins to make your code easier to read and maintain.

Good documentation helps you and others understand your project wiring.

Always comment and organize your pin mappings clearly at the start of your code.