C is a procedural programming language developed in the 1970s for low-level system programming. It is widely used for developing firmware and portable applications. C++ is an extension of C that includes object-oriented features like classes and objects, allowing structured and reusable code — ideal for embedded systems.
Header files (.h
) in Arduino contain predefined functions and macros. They help organize code and reuse functionality. For example:
#include <Wire.h>
allows I2C communication.
#include <Wire.h>
– I2C communication#include <SPI.h>
– SPI protocol#include <LiquidCrystal.h>
– LCD display#include <Servo.h>
– Servo motor#include <SoftwareSerial.h>
– Additional serial ports#include <Adafruit_Sensor.h>
– Sensor base class#include <DHT.h>
– DHT11/DHT22 sensor#include <EEPROM.h>
– Memory storage#include <WiFi.h>
– Wi-Fi (ESP32/ESP8266)#include <BluetoothSerial.h>
– Bluetooth (ESP32)main()
and Return Type?
The main()
function is the entry point of any standard C/C++ program. It tells the processor where to start execution.
In Arduino, however, we use setup()
and loop()
functions instead. Internally, Arduino wraps your code in a main()
function.
void setup() { pinMode(13, OUTPUT); // Set pin 13 as output } void loop() { digitalWrite(13, HIGH); // Turn ON LED delay(1000); // Wait 1 second digitalWrite(13, LOW); // Turn OFF LED delay(1000); // Wait 1 second }
This code blinks the onboard LED connected to pin 13 every second.
void setup() { Serial.begin(9600); // Start serial communication at 9600 baud } void loop() { Serial.println("Hello from miniBotix!"); // Print message delay(1000); // Wait 1 second }
This program sends text to the Serial Monitor every second.
Open Serial Monitor in Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor) to view the messages.
#define sensor A0 #define buzzer 9 void setup() { pinMode(buzzer, OUTPUT); Serial.begin(9600); } void loop() { int temp = analogRead(sensor); // Read analog value float celsius = (temp * 5.0 * 100) / 1024; Serial.println(celsius); // Display temperature if (celsius > 35) { digitalWrite(buzzer, HIGH); // Buzzer ON } else { digitalWrite(buzzer, LOW); // Buzzer OFF } delay(1000); }
This code reads temperature from an analog sensor and activates a buzzer when the temperature crosses 35°C. It also prints temperature to the Serial Monitor.