Basics of Arduino Coding and Wiring
Arduino is an open-source platform used for building electronic projects. It consists of a
physical programmable circuit board (microcontroller) and an integrated development
environment (IDE) for writing and uploading code.
1. Basics of Arduino Coding
1.1 Arduino Programming Language
Arduino uses a simplified version of C++.
1.2 Structure of an Arduino Sketch
An Arduino program consists of two main functions:
Setup Function
Used to initialize settings.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
Loop Function
Runs repeatedly after setup.
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
2. Arduino Wiring
2.1 Digital and Analog Pins
Arduino has digital and analog pins for different functionalities. Digital pins (0-13) can be
set as input or output, while analog pins (A0-A5) are used for reading sensor values.
2.2 Connecting Components
2.2.1 Connecting an LED
To connect an LED, use a resistor (330Ω-1kΩ) to limit current and prevent damage.
Connect the longer leg (anode) to a digital pin and the shorter leg (cathode) to GND.
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
delay(500);
}
2.2.2 Connecting a Button
A button can be used as an input device. When pressed, it connects the circuit. A pull-down
resistor (10kΩ) is recommended to ensure stable readings.
void setup() {
pinMode(2, INPUT);
}
void loop() {
int buttonState = digitalRead(2);
}
2.2.3 Connecting a Potentiometer
A potentiometer is an adjustable resistor used for variable input, like controlling brightness
or volume. It has three pins: connect the outer pins to 5V and GND, and the middle pin to an
analog input (A0).
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(100);
}