Explore our latest Arduino innovations and Source Code.
Arduino source code for Object detecton .
// Pin definitions
const int irSensorPin = 2; // IR sensor output connected to digital pin 2
const int ledPin = 13; // Onboard LED or external LED on pin 13
void setup() {
pinMode(irSensorPin, INPUT); // Set IR sensor pin as input
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Start serial communication
}
void loop() {
int sensorValue = digitalRead(irSensorPin); // Read IR sensor output
if (sensorValue == LOW) {
// Object detected
Serial.println("Object Detected!");
digitalWrite(ledPin, HIGH); // Turn on LED
} else {
// No object
Serial.println("No Object.");
digitalWrite(ledPin, LOW); // Turn off LED
}
delay(200); // Small delay for stability
}
Arduino source code to run a 7 Segment display.
// Pin Definitions for Common Anode 7-Segment Display
const int segA = 2;
const int segB = 3;
const int segC = 4;
const int segD = 5;
const int segE = 6;
const int segF = 7;
const int segG = 8;
// Array to represent digits 0-9 for 7-segment display
// The segments are in the order: {A, B, C, D, E, F, G}
int digits[10][7] = {
{LOW, LOW, LOW, LOW, LOW, LOW, HIGH}, // 0
{HIGH, LOW, LOW, HIGH, HIGH, HIGH, HIGH}, // 1
{LOW, LOW, HIGH, LOW, LOW, HIGH, LOW}, // 2
{LOW, LOW, LOW, LOW, HIGH, HIGH, LOW}, // 3
{HIGH, LOW, LOW, HIGH, HIGH, LOW, LOW}, // 4
{LOW, HIGH, LOW, LOW, HIGH, LOW, LOW}, // 5
{LOW, HIGH, LOW, LOW, LOW, LOW, LOW}, // 6
{LOW, LOW, LOW, HIGH, HIGH, HIGH, HIGH},// 7
{LOW, LOW, LOW, LOW, LOW, LOW, LOW}, // 8
{LOW, LOW, LOW, LOW, HIGH, LOW, LOW} // 9
};
void setup() {
// Set the 7-segment display pins as output
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
}
void loop() {
for (int i = 0; i < 10; i++) {
displayDigit(i); // Display the digit on the 7-segment display
delay(1000); // Wait for 1 second
}
}
// Function to display a digit on the 7-segment display
void displayDigit(int num) {
// Loop through each segment and set its state based on the digit
digitalWrite(segA, digits[num][0]);
digitalWrite(segB, digits[num][1]);
digitalWrite(segC, digits[num][2]);
digitalWrite(segD, digits[num][3]);
digitalWrite(segE, digits[num][4]);
digitalWrite(segF, digits[num][5]);
digitalWrite(segG, digits[num][6]);
}
Arduino source code for Rain sensor Project.
// Pin definitions
#define RAIN_SENSOR_PIN 2 // Digital output from rain sensor
#define LED_PIN 13 // LED pin
void setup() {
pinMode(RAIN_SENSOR_PIN, INPUT); // Rain sensor input
pinMode(LED_PIN, OUTPUT); // LED output
Serial.begin(9600); // Optional: Serial monitor
}
void loop() {
int rainState = digitalRead(RAIN_SENSOR_PIN);
if (rainState == LOW) { // LOW = Rain detected (depends on sensor)
digitalWrite(LED_PIN, HIGH); // Turn LED on
Serial.println("Rain detected!");
} else {
digitalWrite(LED_PIN, LOW); // Turn LED off
Serial.println("No rain");
}
delay(500); // Check every 0.5 seconds
}
Arduino source code for Soil Moisture Sensor.
// Soil Moisture Indicator using LED
// Reads analog input from soil sensor and turns on LED when soil is dry
int sensorPin = A0; // Analog input pin for soil sensor
int ledPin = 13; // Digital output pin for LED
int sensorValue = 0; // Variable to store sensor reading
int threshold = 500; // Threshold value (adjust based on your sensor & soil type)
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Start serial monitor
}
void loop() {
sensorValue = analogRead(sensorPin); // Read analog value from sensor
Serial.print("Soil Moisture: ");
Serial.println(sensorValue); // Print sensor value for calibration
// When soil is dry (sensorValue > threshold)
if (sensorValue > threshold) {
digitalWrite(ledPin, HIGH); // Turn LED ON
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
}
delay(1000); // Wait 1 second before next reading
}
Arduino source code for Flame Sensor.
// Flame Sensor with LED and Buzzer Alert
// Pin Definitions
int flameSensor = 2; // Digital pin connected to flame sensor output
int buzzer = 8; // Buzzer pin
int led = 13; // LED pin
int flameState = HIGH; // HIGH means no flame detected initially
void setup() {
pinMode(flameSensor, INPUT); // Flame sensor input
pinMode(buzzer, OUTPUT); // Buzzer output
pinMode(led, OUTPUT); // LED output
Serial.begin(9600); // Start serial communication
Serial.println("Flame Sensor Test Started...");
}
void loop() {
flameState = digitalRead(flameSensor); // Read flame sensor output
if (flameState == LOW) { // Flame detected (active LOW)
Serial.println("🔥 Flame detected!");
digitalWrite(buzzer, HIGH); // Turn on buzzer
digitalWrite(led, HIGH); // Turn on LED
delay(100); // Small delay
} else {
Serial.println("✅ No flame detected");
digitalWrite(buzzer, LOW); // Turn off buzzer
digitalWrite(led, LOW); // Turn off LED
delay(500);
}
}
Arduino source code for DHT 11 Sensor.
#include "DHT.h" //replace the " " to <> before and after of DHT.h and install the library
// Define DHT pin and type
#define DHTPIN 2 // DHT11 data pin connected to Arduino digital pin 2
#define DHTTYPE DHT11 // DHT 11 sensor
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHT11 Temperature and Humidity Sensor");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Reading temperature and humidity
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Default in °C
// float temperatureF = dht.readTemperature(true); // Use for Fahrenheit
// Check if readings are valid
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display readings on Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}
Arduino source code to connect OLED Display
#include "Wire.h" //replace the " " to <> before and after of wire.h
#include "Adafruit_GFX.h" //replace the " " to <> before and after of Adafruit_GFX.h and install the library
#include "Adafruit_SSD1306.h" //replace the " " to <> before and after of Adafruit_SSD1306.h and install the library
// OLED display width and height, in pixels
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Create display object (for I2C, default address 0x3C)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
// Initialize the display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is the default I2C address
Serial.begin(9600);
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Set text color and size
display.setTextSize(2); // Text size (1 = small, 2 = medium, etc.)
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 25); // Position on screen (x, y)
// Print text
display.println("Hello,");
display.println("World!");
// Display the text
display.display();
}
void loop() {
// Nothing to do here
}
Arduino source code for Gas Sensor.
// Pin definitions
int mq2Pin = A0; // MQ2 sensor analog pin
int ledPin = 13; // LED pin (built-in LED on most Arduino boards)
int threshold = 500; // Adjust this value based on your environment
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int sensorValue = analogRead(mq2Pin);
Serial.print("MQ2 Value: ");
Serial.println(sensorValue);
if (sensorValue > threshold) {
digitalWrite(ledPin, HIGH); // Turn LED ON
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
}
delay(1000);
}
Arduino source code for Object Counting using an IR Sensor(1).
// IR sensor pin
int irPin = 2;
// 7-segment pins: a, b, c, d, e, f, g
int segPins[7] = {3, 4, 5, 6, 7, 8, 9};
// Number patterns for COMMON ANODE
// 0 = ON, 1 = OFF
byte numbers[10][7] = {
{0,0,0,0,0,0,1}, // 0
{1,0,0,1,1,1,1}, // 1
{0,0,1,0,0,1,0}, // 2
{0,0,0,0,1,1,0}, // 3
{1,0,0,1,1,0,0}, // 4
{0,1,0,0,1,0,0}, // 5
{0,1,0,0,0,0,0}, // 6
{0,0,0,1,1,1,1}, // 7
{0,0,0,0,0,0,0}, // 8
{0,0,0,0,1,0,0} // 9
};
int count = 0;
bool lastState = HIGH;
void setup() {
pinMode(irPin, INPUT);
for (int i = 0; i < 7; i++) {
pinMode(segPins[i], OUTPUT);
}
displayNumber(count);
}
void loop() {
bool currentState = digitalRead(irPin);
// Detect object (HIGH → LOW)
if (lastState == HIGH && currentState == LOW) {
count++;
if (count > 9) count = 0;
displayNumber(count);
delay(300); // debounce
}
lastState = currentState;
}
void displayNumber(int num) {
for (int i = 0; i < 7; i++) {
digitalWrite(segPins[i], numbers[num][i]);
}
}
Arduino source code for Object Counting Using an IR Sensor (2).
// IR sensor pins
int irInc = 2; // Increment sensor
int irDec = 10; // Decrement sensor
// 7-segment pins: a b c d e f g
int segPins[7] = {3, 4, 5, 6, 7, 8, 9};
// Common Anode number patterns
// 0 = ON, 1 = OFF
byte numbers[10][7] = {
{0,0,0,0,0,0,1}, // 0
{1,0,0,1,1,1,1}, // 1
{0,0,1,0,0,1,0}, // 2
{0,0,0,0,1,1,0}, // 3
{1,0,0,1,1,0,0}, // 4
{0,1,0,0,1,0,0}, // 5
{0,1,0,0,0,0,0}, // 6
{0,0,0,1,1,1,1}, // 7
{0,0,0,0,0,0,0}, // 8
{0,0,0,0,1,0,0} // 9
};
int count = 0;
bool lastIncState = HIGH;
bool lastDecState = HIGH;
void setup() {
pinMode(irInc, INPUT);
pinMode(irDec, INPUT);
for (int i = 0; i < 7; i++) {
pinMode(segPins[i], OUTPUT);
}
displayNumber(count);
}
void loop() {
bool incState = digitalRead(irInc);
bool decState = digitalRead(irDec);
// Increment logic
if (lastIncState == HIGH && incState == LOW) {
count++;
if (count > 9) count = 9;
displayNumber(count);
delay(300);
}
// Decrement logic
if (lastDecState == HIGH && decState == LOW) {
count--;
if (count < 0) count = 0;
displayNumber(count);
delay(300);
}
lastIncState = incState;
lastDecState = decState;
}
void displayNumber(int num) {
for (int i = 0; i < 7; i++) {
digitalWrite(segPins[i], numbers[num][i]);
}
}