Example-1
//Turn ON/OFF External LED (External LED interfacing with Arduino UNO)
const int LED_PIN = 3;
void setup()
{
// put your setup code here, to run once:
pinMode(LED_PIN, OUTPUT);
[Link](9600);
}
void loop()
{
// put your main code here, to run repeatedly:
digitalWrite(LED_PIN, HIGH); // turn LED on (output 5V)
[Link]("LED is turned ON");
delay(1000); // wait for one second
digitalWrite(LED_PIN, LOW); // turn LED off (output 0V)
[Link]("LED is turned OFF");
delay(2000); // 2 second delay
}
Example-2
//Fading an External LED with the concept of PWM (Use of PWM pins ~)-LED brightness
control.
const int LED_OUTPUT_PIN = 3;
const int DELAY_MS = 5; // delay between each fade value
const int MAX_ANALOG_OUT = 255; // the max analog output on the Uno is 255
void setup()
{
// set the LED pin to an output
pinMode(LED_OUTPUT_PIN, OUTPUT);
}
void loop()
{
// fade on
for(int i = 0; i <= MAX_ANALOG_OUT; i += 1)
{
analogWrite(LED_OUTPUT_PIN, i);
delay(DELAY_MS);
}
//fade off
for(int i = MAX_ANALOG_OUT; i >= 0; i -= 1)
{
analogWrite(LED_OUTPUT_PIN, i);
delay(DELAY_MS);
}
}
Example-3
//IR Sensor interfacing with Arduino UNO
int IRSensor = 9; // connect ir sensor module to Arduino pin 9
int LED = 13; // conect LED to Arduino pin 13
void setup()
{
[Link](9600); // Init Serila at 115200 Baud
[Link]("Serial Working"); // Test to check if serial is working or not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
}
void loop()
{
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
[Link](sensorStatus);
if (sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the Led
digitalWrite(LED, LOW); // LED LOW
[Link]("No Obstacle!"); // print No Obstacle! on the serial monitor
window
}
else
{
//else turn on the LED
digitalWrite(LED, HIGH); // LED High
[Link]("Obstacle Detected!"); // print Obstacle Detected! on the serial
monitor window
}
delay(200);
}
Example-4
//Smoke Sensor (MQ-2) interfacing with Arduino UNO
int redLed = 12;
int greenLed = 11;
int buzzer = 10;
int smokeA0 = A5;
int sensorThres = 10; // Set Sensor threshold value
void setup()
{
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(smokeA0, INPUT);
[Link](9600);
}
void loop()
{
int analogSensor = analogRead(smokeA0);
Serial.p rint("Pin A0: ");
[Link](analogSensor);
if (analogSensor > sensorThres) // Checks if it has reached the threshold value
or not
{
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
tone(buzzer, 1000, 200);
}
else
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
noTone(buzzer);
}
delay(500);
}
Example-5
//Soil Moisture Sensor module interfacing with Arduino Uno
int sensor_pin = A0;
int output_value ;
void setup()
{
[Link](9600);
[Link]("Reading From the Sensor ...");
delay(2000);
}
void loop()
{
output_value= analogRead(sensor_pin);
output_value = map(output_value,1023,10,0,100); //Set the min and max sensor
threshold value for wet and dry soil carefully
[Link]("Mositure : ");
[Link](output_value);
[Link]("%");
delay(1000);
}
Example-6
//Ultra sonic sensor (HC-SR04) interfacing with Arduino UNO for Distance
measurement
const int trigPin = 4; // Trigger pin of the ultrasonic sensor (connected to
Arduino digital pin 4)
const int echoPin = 5; // Echo pin of the ultrasonic sensor (connected to Arduino
digital pin 5)
void setup()
{
[Link](9600); // Initialize serial communication
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
// Trigger the ultrasonic sensor by sending a 10us pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);// Measure the time it takes for the echo
to return
// Calculate the distance in centimeters
// Speed of sound in air at room temperature is approximately 343 meters/second
or 0.0343 cm/microsecond
// Divide the duration by 2 to account for the time it takes for the sound pulse
to travel to the object and back
int distance = duration * 0.0343 / 2;
// Print the distance in cm on to the Serial Monitor
[Link]("Distance: ");
[Link](distance);
[Link](" cm");
delay(1000); // Delay for readability (adjust as needed)
}
Example-7
//7 Segment display (Common Anode type) interfacing with ARDUINO UNO
const int a=2;
const int b=3;
const int c=4;
const int d=5;
const int e=6;
const int f=7;
const int g=8;
void setup ( )
{
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
}
void loop( )
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW); // 0 //
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
delay(2000);
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH); // 1 //
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
delay(2000);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,LOW); // 2 //
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
delay(2000);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW); // 3 //
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
delay(2000);
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH); // 4 //
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
delay(2000);
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,LOW);
digitalWrite(d,LOW); // 5 //
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
delay(2000);
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,LOW);
digitalWrite(d,LOW); // 6 //
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
delay(2000);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH); // 7 //
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
delay(2000);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW); // 8 //
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
delay(2000);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW); // 9 //
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
delay(2000);
}
Example-8
//Push botton interfacing with Arduino UNO
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
[Link](9600);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
[Link]( buttonState);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
delay(500);
}
Example-9
//HC-05 Bluetooth module with Arduino UNO
char Incoming_value;
void setup ()
{
[Link](9600);
pinMode(13,OUTPUT);
}
void loop ()
{
if ([Link] ()>0)
{
Incoming_value=[Link]();
[Link](Incoming_value);
[Link]("\n");
if (Incoming_value=='1')
digitalWrite(13,HIGH);
else if (Incoming_value=='2')
digitalWrite(13,LOW);
}
}
Example-10
//LCD with I2C module interfacing to Arduino UNO
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars
and 2 line display
void setup()
{
[Link](); // initialize the lcd
// Print a message on to the LCD.
[Link]();
[Link](3,0);
[Link]("Hello, world!");
[Link](2,1);
[Link]("BITM ECE");
}
void loop()
{
}
Example-11
//Turn TONE/NO TONE using Buzzar interfacing with Arduino UNO
int piezopin=13;
//Pin connected to buzzer
int buzzer = 5;
void setup()
{
// Defines the Buzzer pin as output
pinMode(buzzer,OUTPUT);
}
void loop()
{
// Sounds the buzzer at the frequency relative to the note C in Hz
tone(buzzer,261);
// Waits some time to turn off
delay(1000);
//Turns the buzzer off
noTone(buzzer);
delay(1000);
}}
void loop(){
tone(piezopin,1000,500);
}