LED Brightness Control with POT
Code:
int sensorValue = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(13, OUTPUT);
}
void loop()
{
sensorValue = analogRead(A0);
digitalWrite(13, HIGH);
delay(sensorValue);
digitalWrite(13, LOW);
delay(sensorValue);
}
Traffic Light with Button Input
Code:
void setup()
{
pinMode(2, INPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(8, OUTPUT);
}
void loop()
{
if (digitalRead(2) == HIGH)
{
digitalWrite(13, HIGH);
delay(3000);
digitalWrite(13, LOW);
delay(3000);
digitalWrite(12, HIGH);
delay(3000);
digitalWrite(12, LOW);
delay(3000);
digitalWrite(8, HIGH);
delay(3000);
digitalWrite(8, LOW);
delay(3000);
}
else
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(8, LOW);
}
}
4. Mood Light with POT
// C++ code
//
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
int potRed = A0;
int potGreen = A1;
int potBlue = A2;
int redValue = 0; // store pot's value
int greenValue = 0;
int blueValue = 0;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
redValue = analogRead(potRed);
greenValue = analogRead(potGreen);
blueValue = analogRead(potBlue);
redValue = redValue/4;
greenValue = greenValue/4 ;
blueValue = blueValue/4 ;
// can be done with map also
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.print("Red:\t");
Serial.print(redValue);
Serial.print("\tGreen:\t");
Serial.print(greenValue);
Serial.print("\tBlue:\t");
Serial.println(blueValue);
delay(100);
}
Sensor Data Logger on LCD
// C++ code
//
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,6,5,4,3);
const int temp = A0;
const int D = 8;
void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
pinMode(D, OUTPUT);
}
void loop()
{
digitalWrite(D, LOW);
int t = analogRead(temp);
float volts = (t/965.0)*5;
float c = (volts-0.5)*100;
float f = (c*9/5 + 32);
Serial.println(f);
lcd.setCursor(1,0);
lcd.print(f);
delay(2000); // Wait for 2000 millisecond(s)
}
Ultrasonic Distance Meter on LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd = LiquidCrystal(10,9,8,7,6,5); // Create an LCD object. Parameters: (RS, E, D4, D5,
D6, D7):
const int trigPin = 12;
const int echoPin = 11;
float time, distance;
void setup()
{
lcd.begin(16, 2); // Specify the LCD's number of columns and rows. Change to (20, 4) for a 20x4 LCD
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
time = pulseIn(echoPin, HIGH);
distance = (time*.0343)/2;
// For Serial Monitor
Serial.print("Distance:CM ");
Serial.println(distance);
// For LCD Display
lcd.setCursor(0,0);
lcd.print("Distance in CM");
lcd.setCursor(0,1);
lcd.print(distance);
Soil Moisture Level with Display
#include <LiquidCrystal.h>
// LCD pins: rs=13, en=12, d4=6, d5=5, d6=4, d7=3
LiquidCrystal lcd(13, 12, 6, 5, 4, 3);
const int moistureAnalog = A0;
const int moistureDigital = 2;
void setup() {
pinMode(moistureDigital, INPUT);
lcd.begin(16, 2);
lcd.print("Soil Monitor");
delay(2000);
}
void loop() {
int analogValue = analogRead(moistureAnalog);
int digitalStatus = digitalRead(moistureDigital);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moist: ");
lcd.print(analogValue);
lcd.setCursor(0, 1);
if (digitalStatus == LOW) {
lcd.print("Alert: Soil Dry!");
} else {
lcd.print("Soil OK");
}
delay(2000);
}
8. POT-Controlled Servo Motor
#include <Servo.h>
Servo myServo; // Create servo object
const int potPin = A0; // Potentiometer connected to A0
void setup() {
myServo.attach(9); // Servo signal pin connected to D9
}
void loop() {
int potValue = analogRead(potPin); // Read value (0 to 1023)
int angle = map(potValue, 0, 1023, 0, 180); // Map to servo angle
myServo.write(angle); // Rotate servo
delay(15); // Small delay for smooth motion
}
LCD Thermometer
#include <LiquidCrystal.h>
// LCD pin configuration: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(13, 12, 6, 5, 4, 3);
const int lm35Pin = A0; // LM35 output connected to A0
void setup() {
lcd.begin(16, 2); // Initialize 16x2 LCD
lcd.print("Temp Monitor");
delay(2000);
lcd.clear();
}
void loop() {
int sensorValue = analogRead(lm35Pin);
// Convert analog value to temperature in Celsius
float voltage = sensorValue * (5.0 / 1023.0); // in volts
float temperatureC = voltage * 100.0; // LM35 gives 10mV per °C
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperatureC, 1); // Show 1 decimal place
lcd.print((char)223); // Degree symbol
lcd.print("C");
delay(1000); // Update every 1 second
}
Digital Dice
#include <LiquidCrystal.h>
// LCD pin order: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(13, 12, 6, 5, 4, 3);
const int buttonPin = 2;
int lastButtonState = HIGH;
void setup() {
lcd.begin(16, 2); // Initialize LCD
lcd.print("Press to Roll"); // Initial message
pinMode(buttonPin, INPUT); // Using external pull-up
randomSeed(analogRead(A0)); // Seed for random roll
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
int roll = random(1, 7); // Dice roll from 1 to 6
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You Rolled:");
lcd.setCursor(6, 1);
lcd.print(roll);
delay(500); // Debounce + hold result
}
lastButtonState = buttonState;
}
Pushbutton Counter on LCD
#include <LiquidCrystal.h>
// RS, E, D4, D5, D6, D7
LiquidCrystal lcd(13, 12, 6, 5, 4, 3);
const int buttonPin = 2;
int buttonState;
int lastButtonState = HIGH;
int counter = 0;
void setup() {
lcd.begin(16, 2);
lcd.print("Press Count:");
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
// Detect button press (HIGH to LOW)
if (lastButtonState == HIGH && buttonState == LOW) {
counter++;
lcd.setCursor(0, 1); // Second line
lcd.print("Count: ");
lcd.print(counter);
lcd.print(" "); // Clear leftover digits
delay(200); // Debounce
}
lastButtonState = buttonState;
}
Mini Piano with Buzzer and Buttons
const int buttonPins[3] = {2, 3, 4}; // Pins where buttons are connected
const int tones[3] = {262, 294, 330}; // Notes: C4, D4, E4
const int buzzerPin = 13; // Buzzer connected here
void setup() {
for (int i = 0; i < 3; i++) {
pinMode(buttonPins[i], INPUT);
}
pinMode(buzzerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 3; i++) {
if (digitalRead(buttonPins[i]) == HIGH) {
tone(buzzerPin, tones[i]); // Play corresponding note
delay(200); // Hold tone briefly
noTone(buzzerPin); // Stop tone
}
}
}
Gas leak alarm
#include <LiquidCrystal.h>
const int gasPin = A0; // MQ-2 Analog pin
const int buzzerPin = 8; // Buzzer
int gasValue = 0; // To store sensor reading
int threshold = 300; // Sensitivity level
// LCD: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(13, 12, 6, 5, 4, 3);
void setup() {
lcd.begin(16, 2);
pinMode(buzzerPin, OUTPUT);
lcd.print("Gas Sensor Ready");
delay(2000);
lcd.clear();
}
void loop() {
gasValue = analogRead(gasPin);
lcd.setCursor(0, 0);
lcd.print("Gas Level: ");
lcd.print(gasValue);
if (gasValue > threshold) {
digitalWrite(buzzerPin, HIGH);
lcd.setCursor(0, 1);
lcd.print("!! Gas Leak !! ");
} else {
digitalWrite(buzzerPin, LOW);
lcd.setCursor(0, 1);
lcd.print("Status: Normal ");
}
delay(500);
}
Water Sensor lcd & led
#include <LiquidCrystal.h>
// LCD: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
const int sensorPin = 2; // Water sensor signal pin (S)
const int ledPin = 3; // LED pin
void setup() {
pinMode(sensorPin, INPUT);
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
lcd.print("Water Monitor");
delay(1000);
}
void loop() {
int waterDetected = digitalRead(sensorPin);
lcd.setCursor(0, 1);
if (waterDetected == HIGH) {
digitalWrite(ledPin, HIGH);
lcd.print("Water Detected ");
} else {
digitalWrite(ledPin, LOW);
lcd.print("No Water ");
}
delay(500);
}
Light Level Meter
#include <LiquidCrystal.h>
// Initialize LCD: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
const int ldrPin = A0; // LDR connected to analog pin A0
void setup() {
lcd.begin(16, 2); // 16x2 LCD
lcd.print("Light Meter");
delay(1000);
}
void loop() {
int lightValue = analogRead(ldrPin); // 0 - 1023
// Convert to percentage
float lightPercent = (lightValue / 1023.0) * 100;
// Display value
lcd.setCursor(0, 0);
lcd.print("Light Level: ");
lcd.setCursor(0, 1);
lcd.print(lightPercent, 1); // show 1 decimal place
lcd.print(" % ");
delay(500);
}
Real Time Analog Value
#include <LiquidCrystal.h>
// LCD: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
const int analogPin = A0; // Connect POT or LDR to A0
void setup() {
Serial.begin(9600); // For Serial Plotter
lcd.begin(16, 2); // Initialize LCD
lcd.print("Analog Monitor");
delay(1000);
}
void loop() {
int value = analogRead(analogPin); // 0–1023
float voltage = value * (5.0 / 1023.0); // Convert to volts
// Print to LCD
lcd.setCursor(0, 0);
lcd.print("Raw: ");
lcd.print(value);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Volt: ");
lcd.print(voltage, 2);
lcd.print(" V ");
// Print to Serial Monitor/Plotter
Serial.println(value);
delay(200);
}
Automatic Night Lamp with LDR
#include <LiquidCrystal.h>
// LCD: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
const int ldrPin = A0; // LDR analog input
const int ledPin = 3; // Night lamp LED
void setup() {
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
lcd.print("Night Lamp System");
delay(2000);
lcd.clear();
}
void loop() {
int ldrValue = analogRead(ldrPin); // 0 = dark, 1023 = bright
lcd.setCursor(0, 0);
lcd.print("LDR: ");
lcd.print(ldrValue);
lcd.print(" "); // clear trailing digits
if (ldrValue < 500) { // Threshold for darkness
digitalWrite(ledPin, HIGH); // Turn on lamp
lcd.setCursor(0, 1);
lcd.print("Status: DARK ");
} else {
digitalWrite(ledPin, LOW); // Turn off lamp
lcd.setCursor(0, 1);
lcd.print("Status: BRIGHT ");
}
delay(500);
}
IR remote
const int ledPins[] = {4, 5, 6}; // LEDs
const int btnPins[] = {7, 8, 9}; // Buttons as fake IR codes
void setup() {
for (int i = 0; i < 3; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(btnPins[i], INPUT_PULLUP); // Simulate remote buttons
}
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 3; i++) {
if (digitalRead(btnPins[i]) == LOW) { // Button pressed
digitalWrite(ledPins[i], !digitalRead(ledPins[i])); // Toggle LED
Serial.print("Toggled LED ");
Serial.println(i + 1);
delay(300); // Debounce
}
}
}
Sound Sensor
const int soundSensorPin = 2;
const int ledPin = 3;
const int buttonPin = 4;
bool ledState = false;
bool lastButtonState = HIGH;
unsigned long lastClapTime = 0;
void setup() {
pinMode(soundSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // optional button
Serial.begin(9600);
}
void loop() {
// Clap detection
int soundState = digitalRead(soundSensorPin);
if (soundState == HIGH && (millis() - lastClapTime > 300)) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
Serial.println("Clap detected!");
lastClapTime = millis();
}
// Optional button toggle
bool buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
Serial.println("Button pressed!");
delay(300); // debounce
}
lastButtonState = buttonState;
}
Dc Motor Project
#include <DHT.h>
#define DHTPIN 2 // DHT11 data pin connected to D2
#define DHTTYPE DHT11 // We're using DHT11
#define RELAY_PIN 3 // Relay control pin
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Fan OFF initially
Serial.begin(9600);
dht.begin();
}
void loop() {
float temp = dht.readTemperature(); // Read in Celsius
if (isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" °C");
if (temp >= 30.0) {
digitalWrite(RELAY_PIN, HIGH); // Turn ON relay (fan)
Serial.println("Fan ON");
} else {
digitalWrite(RELAY_PIN, LOW); // Turn OFF relay (fan)
Serial.println("Fan OFF");
}
delay(2000); // Read every 2 seconds
}