60 Projects
60 Projects
Projects
BY- YUVRAJ.R
Light Automation:
const int MOTION_SENSOR_PIN = 10;
const int LED_PIN = 6;
int motionStateCurrent = LOW;
int motionStatePrevious = LOW;
void setup() {
Serial.begin(9600);
pinMode(10, INPUT);
pinMode(6, OUTPUT);
}
void loop() {
motionStatePrevious = motionStateCurrent;
motionStateCurrent = digitalRead(10); // read new state
if (motionStatePrevious == LOW && motionStateCurrent
== HIGH) {
change: LOW -> HIGH
Serial.println("Motion detected!");
digitalWrite(6, HIGH); // turn on
}
else
if (motionStatePrevious == HIGH && motionStateCurrent
== LOW) {
change: HIGH -> LOW
Serial.println("Motion stopped!");
digitalWrite(6, LOW); // turn off
}
}
Blink:
Code:
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000); // Wait for 1000
millisecond(s)
digitalWrite(13, LOW);
delay(1000); // Wait for 1000
millisecond(s)
}
Servo Motor:
#include <Servo.h>
int pos = 0;
Servo servo_9;
void setup()
{
servo_9.attach(9, 500, 2500);
}
void loop()
{
for (pos = 0; pos <= 180; pos += 1) {
servo_9.write(pos);
Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
lcd_1.begin(16, 2);
lcd_1.print("hello world");
}
void loop()
{
lcd_1.setCursor(0, 1);
lcd_1.print(seconds);
lcd_1.setBacklight(1);
delay(500); // Wait for 500 millisecond(s)
lcd_1.setBacklight(0);
delay(500); // Wait for 500 millisecond(s)
seconds += 1;
}
Button:
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Potentiometer:
const int potPin = A0;
const int ledPin = 9;
int potValue = 0;
int brightness = 0;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
potValue = analogRead(potPin);
analogWrite(ledPin, brightness);
delay(10);
}
Photoresistor with Buzzer:
const int photoPin = A0; // the number of the photoresistor
pin
const int buzzerPin = 9; // the number of the buzzer pin
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
delay(5000); // wait for 5 seconds
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
delay(2000); // wait for 2 seconds
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
delay(5000); // wait for 5 seconds
LED Fade:
const int ledPin = 13;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// fade in the LED:
for (int brightness = 0; brightness < 256; brightness++)
{
analogWrite(ledPin, brightness);
delay(10);
}
lcd_1.print("Temprature");
lcd_2.begin(16, 1);
lcd_2.print("Temprature");
}
void loop()
{
lcd_1.setCursor(0, 1);
lcd_1.print(seconds);
lcd_1.setBacklight(1);
delay(500);
lcd_1.setBacklight(0);
delay(500);
seconds += 1;
}
Soil Moisture with DC motor:
const int soilMoisturePin = A0;
const int motorPin = 2;
void setup() {
pinMode(A0, INPUT);
pinMode(2, OUTPUT);
}
void loop() {
int soilMoistureValue = analogRead(A0);
int soilMoisturePercentage = map(soilMoistureValue,
0, 1023, 0, 100);
void loop() {
// Turn the vibrator on
digitalWrite(vibratorPin, HIGH);
delay(1000); // Vibrate for 1 second
void setup() {
pixels.begin();
}
void loop() {
setColor();
for (int i=0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(redColor, greenColor,
blueColor));
pixels.show();
delay(delayval);
}
}
void setColor(){
redColor = random(0, 255);
greenColor = random(0,255);
blueColor = random(0, 255);
}
LCD with Potentiometer:
#include <LiquidCrystal.h>
int seconds = 0;
void setup()
{
lcd_1.begin(16, 2); // Set up the number of
columns and rows on the LCD.
lcd_1.print("hello world!");
}
void loop()
{
lcd_1.setCursor(0, 1);
lcd_1.print(seconds);
delay(1000); // Wait for 1000 millisecond(s)
seconds += 1;
}
Force Sensor:
const int forceSensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue =
analogRead(forceSensorPin);
float force = sensorValue * (5.0 / 1023.0);
Serial.print("Force: ");
Serial.print(force);
Serial.println(" V");
delay(100);
}
Force Sensor with LED:
const int forcePin = A0;
const int ledPins[] = {2, 3, 4, 5, 6};
void setup() {
pinMode(forcePin, INPUT);
for (int i = 0; i < 5; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
int forceValue = analogRead(forcePin);
int ledIndex = map(forceValue, 0, 1023, 0, 5);
delay(50);
}
Multiple Tones:
const int speakerPin = 8 ;
const int speakerPin = 7 ;
const int speakerPin = 6;
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
tone(8, 200);
delay(500); // Play the tone for 500ms
tone(7, 600);
delay(500); // Play the tone for 500ms
tone(6, 1000);
delay(500); // Play the tone for 500ms
}
Light Range Sensor:
const int buzzerPin = 9; delay(100); // Take a reading every 100ms
const int trigPin = 10; }
const int echoPin = 11;
const int lightPin = A0; int readDistance() {
// Send a pulse to the ultrasonic sensor
void setup() { digitalWrite(trigPin, LOW);
pinMode(buzzerPin, OUTPUT); delayMicroseconds(2);
pinMode(trigPin, OUTPUT); digitalWrite(trigPin, HIGH);
pinMode(echoPin, INPUT); delayMicroseconds(10);
pinMode(lightPin, INPUT); digitalWrite(trigPin, LOW);
}
// Read the echo pulse
void loop() { int duration = pulseIn(echoPin, HIGH);
int distance = readDistance();
// Calculate the distance
int lightIntensity = int distance = duration * 0.034 / 2;
analogRead(lightPin);
return distance;
if (distance < 10 && lightIntensity }
< 500) {
tone(buzzerPin, 1000);
delay(500);
noTone(buzzerPin);
}
Digital Clock:
#include <LiquidCrystal.h>
#include <Wire.h>
#include <DS3231.h>
const int lcdRs = 12, lcdEn = 11, lcdD4 = 5, lcdD5 = 4, lcdD6 = 3, lcdD7 = 2;
LiquidCrystal_I2C lcd(lcdRs, lcdEn, lcdD4, lcdD5, lcdD6, lcdD7);
DS3231 rtc;
void setup() {
lcd.begin(20, 4);
lcd.setCursor(0, 0);
lcd.print("Digital Clock");
Wire.begin();
rtc.begin();
}
void loop() {
DateTime now = rtc.now();
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
lcd.setCursor(0, 2);
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
delay(1000);
}
Flex Sensor:
const int flexSensorPin = A0;
const int fixedResistor = 10000;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(flexSensorPin);
float resistance = sensorValue * (fixedResistor /
1023.0);
float bendAngle = map(resistance, 0,
fixedResistor, 0, 90);
Serial.print("Bend Angle: ");
Serial.print(bendAngle);
Serial.println(" degrees");
delay(100);
}
Smoke Detector:
const int smokePin = A0; // smoke sensor pin
const int ledPin =A1; // LED pin
void setup() {
pinMode(A0, INPUT);
pinMode(A1, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
if (sensorValue > 400) {
digitalWrite(A1, HIGH);
} else {
digitalWrite(A1, LOW);
}
delay(100);
}
Disco Neopixel:
#include <Adafruit_NeoPixel.h> void loop() {
// Jewel mode
#define PIN_JEWEL 6 for (int i = 0; i < NUM_PIXELS_JEWEL; i++) {
#define PIN_NEOPIXEL1 7 jewel.setPixelColor(i, jewel.Color(random(255),
#define PIN_NEOPIXEL2 8 random(255), random(255)));
#define PIN_NEOPIXEL_RING 9 }
jewel.show();
#define NUM_PIXELS_JEWEL 7 delay(50);
#define NUM_PIXELS_NEOPIXEL1 1
#define NUM_PIXELS_NEOPIXEL2 1 // NeoPixel 1 mode
#define NUM_PIXELS_NEOPIXEL_RING 6 neopixel1.setPixelColor(0,
Adafruit_NeoPixel jewel = neopixel1.Color(random(255), random(255),
Adafruit_NeoPixel(NUM_PIXELS_JEWEL, PIN_JEWEL, NEO_GRB random(255)));
+ NEO_KHZ800); neopixel1.show();
Adafruit_NeoPixel neopixel1 = delay(50);
Adafruit_NeoPixel(NUM_PIXELS_NEOPIXEL1, PIN_NEOPIXEL1,
NEO_GRB + NEO_KHZ800); // NeoPixel 2 mode
Adafruit_NeoPixel neopixel2 = neopixel2.setPixelColor(0,
Adafruit_NeoPixel(NUM_PIXELS_NEOPIXEL2, PIN_NEOPIXEL2, neopixel2.Color(random(255), random(255),
NEO_GRB + NEO_KHZ800); random(255)));
Adafruit_NeoPixel neopixelRing = neopixel2.show();
Adafruit_NeoPixel(NUM_PIXELS_NEOPIXEL_RING, delay(50);
PIN_NEOPIXEL_RING, NEO_GRB + NEO_KHZ800);
// NeoPixel Ring mode
void setup() { for (int i = 0; i < NUM_PIXELS_NEOPIXEL_RING;
jewel.begin(); i++) {
jewel.setBrightness(50); neopixelRing.setPixelColor(i,
neopixel1.begin(); neopixelRing.Color(sin(millis() * 0.001 + i * 0.1) *
neopixel1.setBrightness(50); 128 + 128, sin(millis() * 0.001 + i * 0.1 + 2 * PI /
neopixel2.begin(); 3) * 128 + 128, sin(millis() * 0.001 + i * 0.1 + 4 *
neopixel2.setBrightness(50); PI / 3) * 128 + 128));
neopixelRing.begin(); }
neopixelRing.setBrightness(50);
Servo, LCD, Ultrasonic:
void loop() {
#include <LiquidCrystal.h>
int potValue =
#include <Servo.h>
analogRead(potPin);
int angle = map(potValue, 0,
const int lcdRs = 12, lcdEn = 11, lcdD4 =
1023, 0, 180);
5, lcdD5 = 4, lcdD6 = 3, lcdD7 = 2;
servo.write(angle);
LiquidCrystal_I2C lcd(lcdRs, lcdEn, lcdD4,
lcdD5, lcdD6, lcdD7);
// Read ultrasonic sensor value
digitalWrite(trigPin, HIGH);
const int servoPin = 9;
delayMicroseconds(10);
Servo servo;
digitalWrite(trigPin, LOW);
int duration = pulseIn(echoPin,
const int trigPin = 10;
HIGH);
const int echoPin = 11;
int distance = duration * 0.034 /
2;
const int potPin = A0;
// Display distance on LCD
void setup() {
lcd.setCursor(0, 1);
lcd.begin(20, 4);
lcd.print("Distance: ");
lcd.setCursor(0, 0);
lcd.print(distance);
lcd.print("Servo, LCD, Ultrasonic");
lcd.print(" cm");
servo.attach(servoPin);
pinMode(trigPin, OUTPUT);
delay(100);
pinMode(echoPin, INPUT);
}
pinMode(potPin, INPUT);
}
Different Servo Rotations:
#include <Servo.h> void loop() {
val1 =
Servo myservo1; analogRead(potpin1);
Servo myservo2; val1 = map(val1, 0, 1023,
Servo myservo3; 0, 180
Servo myservo4; myservo1.write(val1);
int potpin1 = 0;
int potpin2 = 1; val2 =
int potpin3 = 2; analogRead(potpin2);
int potpin4 = 3; val2 = map(val2, 0, 1023,
int val1; 0, 180);
int val2; myservo2.write(val2);
int val3; val3 =
int val4; analogRead(potpin3);
void setup() { val3 = map(val3, 0, 1023,
0, 180);
myservo1.attach(9); myservo3.write(val3);
val4 =
myservo2.attach(6); analogRead(potpin4);
val4 = map(val4, 0, 1023,
myservo3.attach(5); 0, 180);
myservo4.write(val4);
RGB with Buzzer:
const int redPin = 9;
const int greenPin = 10; } else {
const int bluePin = 11; // Blue
const int buzzerPin = 12; analogWrite(redPin, 0);
const int potPin = A0; analogWrite(greenPin, 0);
analogWrite(bluePin, brightness);
void setup() { }
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT); if (potValue < 341) {
pinMode(bluePin, OUTPUT); tone(buzzerPin, 200, 100);
pinMode(buzzerPin, OUTPUT); } else if (potValue < 682) {
pinMode(potPin, INPUT); tone(buzzerPin, 400, 100);
} } else {
tone(buzzerPin, 600, 100);
void loop() { }
int potValue = analogRead(potPin);
int brightness = map(potValue, 0, 1023, delay(100);
0, 255); }
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
digitalWrite(buzzerPin, HIGH);
digitalWrite(ledPin, HIGH);
delay(5000); // Alarm duration (5
seconds)
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
}
delay(50); // Debounce delay
}
Rock Paper Scissors Game:
#include<LiquidCrystal.h void setup() delay(100); case 3:
> { noTone(12); tone(12,131);
#include <Servo.h> lcd.begin(16,2); servo_3.write(120); delay(100);
lcd.setCursor(0,0); lcd.setCursor(0,0); noTone(12);
LiquidCrystal lcd.print(" Rock Paper and lcd.print(" It's...."); servo_9.write(120);
lcd(A0,A1,A2,A3,A4,A5); "); lcd.setCursor(0,1); lcd.setCursor(0,0);
volatile long A; lcd.setCursor(0,1); lcd.print(" lcd.print(" It's....");
lcd.print(" Scissor Game "); Stone...."); lcd.setCursor(0,1);
float delay(4000); digitalWrite(red1, lcd.print("
checkdistance_11_10() lcd.clear(); HIGH); Scissor....");
{ A = 0; delay(1000); digitalWrite(red3,
digitalWrite(11, LOW); pinMode(11, OUTPUT); servo_3.write(179); HIGH);
delayMicroseconds(2); pinMode(10, INPUT); digitalWrite(red1, LOW); delay(1000);
digitalWrite(11, HIGH); pinMode(12, OUTPUT); lcd.clear(); servo_9.write(179);
delayMicroseconds(10); pinMode(red1, OUTPUT); delay(500); digitalWrite(red3,
digitalWrite(11, LOW); pinMode(red2, OUTPUT); break; LOW);
float distance = pinMode(red3, OUTPUT); case 2: lcd.clear();
pulseIn(10, HIGH) / 58.00; servo_3.attach(3); // stone tone(12,131); delay(500);
delay(10); servo_6.attach(6); // paper delay(100); break;
return distance; servo_9.attach(9); // noTone(12); }
} scissor servo_6.write(120); }
servo_3.write(179); lcd.setCursor(0,0); }
Servo servo_3; servo_6.write(179); lcd.print(" It's....");
Servo servo_6; servo_9.write(179); lcd.setCursor(0,1);
Servo servo_9; } lcd.print(" Paper....");
void loop() digitalWrite(red2,
int red1 = 2; { HIGH);
int red2 = 4; if (checkdistance_11_10() < delay(1000);
int red3 = 5; 10) { servo_6.write(179);
int speaker = 12; A = random(0, 4); digitalWrite(red2, LOW);
switch (A) { lcd.clear();
case 1: delay(500);
Range Graph:
const int trigPin = 2;
if (thisLed < ledLevel) {
const int echoPin = 3;
digitalWrite(ledPins[thisLed],
const int ledCount = 8;
HIGH);
int ledPins[] = {4, 5, 6, 7, 8, 9, 10, 11};
} else { // turn off all pins higher
void setup() {
than the ledLevel:
pinMode(trigPin, OUTPUT);
digitalWrite(ledPins[thisLed],
pinMode(echoPin, INPUT);
LOW);
for (int thisLed = 0; thisLed < ledCount;
}
thisLed++) {
}
pinMode(ledPins[thisLed], OUTPUT);
delay(50);
}
}
Serial.begin(9600);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
int ledLevel = map(distance, 0, 400, 0,
ledCount);
for (int thisLed = 0; thisLed < ledCount;
thisLed++) {
Bidirectional Display:
#include <LiquidCrystal.h>
int in = 15; ppl = constrain(ppl, 0, 50);
int inpr = 16; lcd.setCursor(0, 0);
int out = 14; lcd.print("PEOPLE IN:");
int outpr = 17; lcd.setCursor(11, 0);
int ppl = 0; lcd.print(ppl);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); if (ppl >= 20) {
bool pi = 0; lcd.setCursor(0, 1);
bool po = 0; lcd.print("PLEASE WAIT");
void setup() { delay(1000);
pinMode(15, INPUT); }
pinMode(14, INPUT); if (ppl <= 19) {
pinMode(16, OUTPUT); lcd.setCursor(0, 1);
pinMode(17, OUTPUT); lcd.print("PLEASE VISIT");
lcd.begin(16, 2); delay(1000);
} }
void loop() { }
lcd.clear();
digitalWrite(outpr, HIGH);
digitalWrite(inpr, HIGH);
pi = digitalRead( in );
po = digitalRead(out);
if (pi == 1) {
ppl--;
delay(500);
} else if (po == 1) {
ppl++;
delay(500);
}
Home Lighting:
#include <Adafruit_NeoPixel.h>
#define PIN 3
#define NUMPIXELS 12
float celsius;
int temp = A1;
void setup(){
pinMode(temp,INPUT);
}
void loop(){
celsius =
analogRead(temp)*0.004882814;
celsius = (celsius - 0.5) * 100.0;
lcd.setCursor(0,1);
lcd.print("Temp: ");
lcd.print(celsius);
lcd.print(" C");
delay(1000);
lcd.clear();
}
Smoke and Temperature
with Buzzer indicator:
const int smokePin = 2; // Smoke sensor pin
const int tempPin = 3; // Temperature sensor
pin
const int buzzerPin = 4; // Buzzer pin
void setup() {
pinMode(smokePin, INPUT);
pinMode(tempPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
int smokeValue = digitalRead(smokePin);
float temperature = getTemperature(tempPin);
void loop() {
int switch1State =
digitalRead(switch1Pin);
int switch2State =
digitalRead(switch2Pin);
Buzzer with PIR Sensor:
const int pirPin = 13; // PIR sensor pin
const int buzzerPin = 3; // Buzzer pin
const int relayPin = 2; // Relay pin
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
tone(buzzerPin, 1000, 200); // Buzz for 200ms
digitalWrite(relayPin, HIGH); // Turn on the light bulb
delay(500); // Wait for 500ms
} else {
digitalWrite(relayPin, LOW); // Turn off the light bulb
}
}
Drone:
const int pwm = 9 ; digitalWrite(in_1,LOW) ;
const int in_1 = 3 ; digitalWrite(in_2,HIGH) ;
const int in_2 = 2 ; delay(3000) ;
void loop()
{
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,LOW) ;
analogWrite(pwm,255) ;
delay(3000) ;
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(1000) ;
Temperature Controlled By
Fan:
const int tempPin = 2; // Temperature lcd.setCursor(0, 0); // Set cursor to first
sensor pin row
const int relayPin = 3; // Relay pin lcd.print("Temperature: ");
const int lcdRS = 4; // LCD RS pin lcd.print(temperature);
const int lcdEN = 5; // LCD EN pin lcd.print(" C");
const int lcdD4 = 6; // LCD D4 pin
const int lcdD5 = 7; // LCD D5 pin if (temperature > 25) {
const int lcdD6 = 8; // LCD D6 pin digitalWrite(relayPin, HIGH);
const int lcdD7 = 9; // LCD D7 pin lcd.setCursor(0, 1);
lcd.print("Fan: ON");
#include <LiquidCrystal.h> } else {
digitalWrite(relayPin, LOW);
LiquidCrystal_I2C lcd(lcdRS, lcdEN, lcd.setCursor(0, 1);
lcdD4, lcdD5, lcdD6, lcdD7); lcd.print("Fan: OFF");
}
void setup() { delay(1000); // Wait for 1 second
pinMode(tempPin, INPUT); }
pinMode(relayPin, OUTPUT);
lcd.begin(16, 2); // Initialize LCD float getTemperature(int tempPin) {
display return temperatureValue;
} }
void loop() {
float temperature =
getTemperature(tempPin);
Car Parking Alerter:
const int ultrasonic1Trig = 2; void loop() {
const int ultrasonic1Echo = 3; int distance1 = getDistance(ultrasonic1Trig,
const int ultrasonic2Trig = 4; ultrasonic1Echo);
const int ultrasonic2Echo = 5; int distance2 = getDistance(ultrasonic2Trig,
ultrasonic2Echo);
const int ultrasonic3Trig = 6;
int distance3 = getDistance(ultrasonic3Trig,
const int ultrasonic3Echo = 7;
ultrasonic3Echo);
const int potPin = A0; int potValue = analogRead(potPin);
const int lcdRS = 8; lcd.setCursor(0, 0); // Set cursor to first row
const int lcdEN = 9; lcd.print("Distance 1: ");
const int lcdD4 = 10; lcd.print(distance1);
const int lcdD5 = 11; lcd.print(" cm");
const int lcdD6 = 12; lcd.setCursor(0, 1); // Set cursor to second
const int lcdD7 = 13; row
lcd.print("Distance 2: ");
#include <LiquidCrystal.h> lcd.print(distance2);
lcd.print(" cm");
LiquidCrystal_I2C lcd(lcdRS, lcdEN,
lcd.print(" Distance 3: ");
lcdD4, lcdD5, lcdD6, lcdD7);
lcd.print(distance3);
void setup() { lcd.print(" cm");
pinMode(ultrasonic1Trig, OUTPUT); lcd.setCursor(0, 2); // Set cursor to third row
pinMode(ultrasonic1Echo, INPUT); lcd.print("Pot Value: ");
pinMode(ultrasonic2Trig, OUTPUT); lcd.print(potValue);
pinMode(ultrasonic2Echo, INPUT);
pinMode(ultrasonic3Trig, OUTPUT); delay(1000); // Wait for 1 second
pinMode(ultrasonic3Echo, INPUT); }
pinMode(potPin, INPUT); int getDistance(int trigPin, int echoPin) {
lcd.begin(16, 2); digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
}
digitalWrite(trigPin,
Smart Blind Stick:
void setup()
if (dis2<100)
{
{
pinMode(12, OUTPUT);
tone(12, 700);
}
}
long duration1, dis1,duration2,
else
dis2,duration3, dis3;
{
void loop()
noTone(12);
{
}
noTone(12);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);
digitalWrite(4, LOW);
digitalWrite(0, LOW);
delayMicroseconds(2);
delayMicroseconds(2);
digitalWrite(4, HIGH);
digitalWrite(0, HIGH);
delayMicroseconds(2);
delayMicroseconds(2);
digitalWrite(4, LOW);
digitalWrite(0, LOW);
pinMode(5, INPUT);
pinMode(1, INPUT);
duration3=pulseIn(5, HIGH);
duration1=pulseIn(1, HIGH);
dis3= (duration3*0.034)/2;
dis1= (duration1*0.034)/2;
if (dis1<100)
{tone(12, 500)}
if (dis3<100)
else
{
{ noTone(12); }
tone(12, 700);
pinMode(2, OUTPUT);
}
digitalWrite(2, LOW);
else
delayMicroseconds(2);
{
digitalWrite(2, HIGH);
noTone(12);
delayMicroseconds(2);
}
digitalWrite(2, LOW);
delay(100);
pinMode(3, INPUT);
}
duration2=pulseIn(3, HIGH);
Automatic Traffic Light:
int sv = 3;
int valh = 0;
int valv = 0; int th1 = 3000;
int rv = 5; int th2 = 400;
int av = 6; int tv1 = 3000;
int vv = 7; int tv2 = 400;
int rh = 11; if (valh == HIGH && valv == LOW) {
int ah = 12; Serial.println("Aumentar
Horizontal");
int vh = 13;
th1 = th1 * 2;
void setup() th2 = th2 * 2;
{ } else if (valh == LOW && valv ==
for (int i = 5; i <= 13; i+ HIGH) {
+) { Serial.println("Aumentar Vertical");
pinMode(i, tv1 = tv1 * 2;
OUTPUT); tv2 = tv2 * 2;
} }
pinMode(sh, INPUT); semaforoHorizontal(th1, th2);
pinMode(sv, INPUT); semaforoVertical(tv1, tv2); void semaforoVertical(int t1, int t2) {
Serial.begin(9600); } digitalWrite(rh, HIGH);
void semaforoHorizontal(int t1, int t2)digitalWrite(vv, HIGH);
}
{ delay(t1);
void loop() digitalWrite(rv, HIGH);
{ digitalWrite(vv, LOW);
digitalWrite(vh, HIGH); digitalWrite(av, HIGH);
valh = digitalRead(sh); delay(t1); delay(t2);
valv = digitalRead(sv); digitalWrite(vh, LOW); digitalWrite(rh, LOW);
Serial.print("Horizontal: digitalWrite(ah, HIGH); digitalWrite(av, LOW);
"); delay(t2); }
Serial.println(valh); digitalWrite(rv, LOW);
Serial.print("Vertical: "); digitalWrite(ah, LOW);
}
Home Automation:
void loop() {
const int motorForward = 2; int pirState = digitalRead(pirPin);
const int motorBackward = 3; if (pirState == HIGH) {
const int pirPin = 4; digitalWrite(ledPin, HIGH);
const int ledPin = 13; lcd.setCursor(0, 1);
const int potPin = A0; lcd.print("Motion Detected!");
const int rs = 12, en = 11, d4 = int potValue = analogRead(potPin);
5, d5 = 4, d6 = 3, d7 = 2; int motorSpeed = map(potValue, 0, 1023, 0, 255);
LiquidCrystal_I2C lcd(rs, en, d4, analogWrite(motorForward, motorSpeed);
d5, d6, d7); lcd.setCursor(0, 2);
lcd.print("Motor Speed: ");
void setup() { lcd.print(motorSpeed);
pinMode(motorForward, } else {
OUTPUT); digitalWrite(ledPin, LOW);
pinMode(motorBackward, analogWrite(motorForward, 0);
OUTPUT); lcd.setCursor(0, 1);
pinMode(pirPin, INPUT); lcd.print("No Motion");
pinMode(ledPin, OUTPUT); lcd.setCursor(0, 2);
pinMode(potPin, INPUT); lcd.print("Motor Speed: 0");
lcd.begin(20, 4); }
lcd.setCursor(0, 0); float temperature =
lcd.print("Home Automation tempSensor.getTempCByIndex(0);
System"); lcd.setCursor(0, 3);
} lcd.print("Temperature: ");
lcd.print(temperature);
lcd.print(" C");
delay(1000);
}
Distance Finder:
#include <LiquidCrystal.h>
delay(1000);
}
Gas Alarm:
const int gasSensorPin = A0;
const int buzzerPin = 9;
const int ledPin = 2; // or A1 for LED
void setup() {
pinMode(gasSensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int gasValue = analogRead(gasSensorPin);
Serial.print("Gas Value: ");
Serial.println(gasValue);
delay(1000);
}
Simple counter:
const int buttonPin = 9;
int digit = digits[number % 10];
const int digitPins[] = {2, 3, 4, 5, 6, 7, 8};
int value = digitValues[digit];
int count = 0
void setup() {
pinMode(buttonPin, INPUT); for (int i = 0; i < 7; i++) {
for (int i = 0; i < 7; i++) { digitalWrite(digitPins[i], (value & (1 << i)) ? HIGH :
pinMode(digitPins[i], OUTPUT); LOW);
} }
} }
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
count++;
displayNumber(count);
delay(500); // debounce delay
}
delay(100);
}
void displayNumber(int number) {
int digits[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int digitValues[] = {
0b0111111, // 0
0b0000110, // 1
0b1011011, // 2
0b1001111, // 3
0b1100110, // 4
0b1101101, // 5
0b1111101, // 6
0b0000111, // 7
0b1111111, // 8
0b1101111 // 9
};