Arduino Uno Based Programming Examples - To Be Shared To Students
Arduino Uno Based Programming Examples - To Be Shared To Students
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
[2] Flashing 3 LEDs for Traffic Light Signal Control :
void setup()
{
pinMode(LEDR,OUTPUT);
pinMode(LEDY,OUTPUT);
pinMode(LEDG,OUTPUT);
}
void loop()
{
digitalWrite(LEDR,HIGH);
digitalWrite(LEDY,LOW);
digitalWrite(LEDG,LOW);
delay(8000);
digitalWrite(LEDR,LOW);
digitalWrite(LEDY,HIGH);
digitalWrite(LEDG,LOW);
delay(300);
digitalWrite(LEDR,LOW);
digitalWrite(LEDY,LOW);
digitalWrite(LEDG,HIGH);
delay(2000);
digitalWrite(LEDR,LOW);
digitalWrite(LEDY,HIGH);
digitalWrite(LEDG,LOW);
delay(300);
}
[3] Flashing 4 -BIT LEDs :
void setup()
{
for (int i = 0; i < 4; i++)
{
pinMode(ledPins[i], OUTPUT);
}
}
void loop()
{
for (int i = 0; i < 4; i++)
{
digitalWrite(ledPins[i], HIGH);
delay(250);
digitalWrite(ledPins[i], LOW);
}
}
[4] Operating On Board LED based on Switch Input :
int switchPin = 2;
int ledPin = 13;
void setup()
{
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchPin) == HIGH)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
[5] Operating OFF Board LED based on Switch Input :
int switchPin = 7;
int ledPin = 8;
void setup()
{
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchPin) == HIGH)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
[6] 16x2 LCD Display with I2C: TO Display GOOD MORNING :
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("GOOD MORNING");
}
void loop()
{
}
[7] TWO IR Sensors with Object Counting Logic : To Display on
Computer using USB Serial PORT
int sensor1Pin = 2;
int sensor2Pin = 3;
int count = 0;
void setup()
{
pinMode(sensor1Pin, INPUT);
pinMode(sensor2Pin, INPUT);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(sensor1Pin) == LOW && digitalRead(sensor2Pin) == HIGH)
{
count++;
Serial.print("Count: ");
Serial.println(count);
}
}
[8] Displaying "GOOD MORNING" on USB Serial Port:
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("GOOD MORNING");
delay(1000);
}
[9] TWO IR Sensors with Object Counting Logic : To Display on
LCD Display :
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Objects: 0");
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(sensorPin1) == LOW && digitalRead(sensorPin2) == HIGH)
{
delay(100); // Debounce delay
if (digitalRead(sensorPin1) == LOW && digitalRead(sensorPin2) ==
HIGH)
{
objectCount++;
lcd.setCursor(9, 0);
lcd.print(objectCount);
Serial.print("Object detected. Total count: ");
Serial.println(objectCount);
delay(500); // Adjust delay as needed
}
}
}