× Introduction Embedded systems Arduino Arduino Programming Structure Arduino Hardware Digital Output DO Projects Digital Input Analog I/O Variables and Strings Serial Communication Examples Quiz-1
eBooks
Arduino Uno
Components
Computer
Tools
C Programs







Arduino Examples

    
 void setup(){
  pinMode(13, OUTPUT);
  }
  
 void loop(){
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);      
 }
 

    
 void setup(){
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
 }

 void loop(){  
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  delay(1000);   
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  delay(1000); 
  }

 

    
int greenLed = 2;
int yellowLed = 3;
int redLed = 4;

void setup(){
  pinMode(greenLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  
  digitalWrite(greenLed, LOW);
  digitalWrite(yellowLed, LOW);
  digitalWrite(redLed, LOW);
 }

void loop(){
  digitalWrite(redLed, HIGH); 
  delay(5000);
  digitalWrite(yellowLed, HIGH);
  digitalWrite(redLed, LOW);
  delay(2000);
  digitalWrite(greenLed, HIGH); 
  digitalWrite(yellowLed, LOW);
  digitalWrite(redLed, LOW);
  delay(5000);
  digitalWrite(greenLed, LOW);
 }

    
int pirStat = 0;
int pirPin = 3; //PIR Sensor
int ledPin = 4; //LED
void setup() {
 pinMode(pirPin, INPUT);
 pinMode(ledPin, OUTPUT);
}

void loop(){
 pirStat = digitalRead(pirPin);
  if (pirStat == HIGH) {
   digitalWrite(ledPin, HIGH);
   }
  else {
  digitalWrite(ledPin, LOW);
   }
}
 

    
int led = 13; // connect LED to pin 13
int pin = 0; // potentiometer on analogy pin 0
int value = 0; // variable to store the read value
void setup() {
}
void loop() {
value = analogRead(pin);
value /= 4; // converts 0-1023 to 0-255
analogWrite(led, value); // output PWM signal to LED
}
 

    
int pirPin = 3;
int ledPin = 4;
int pirStatus = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
Serial.begin(9600);
}
void loop(){
pirStatus = digitalRead(pirPin);
if (pirStatus == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println("Motion Detected!");
}
else {
digitalWrite(ledPin, LOW);
}
delay(100);
}
 

 
int sensorPin = A0;
int sensorValue = 0;
int led = 13;
void setup(){
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop(){
sensorValue = analogRead(A0);
Serial.print("LDR COunt = ");
Serial.println(sensorValue);
if(sensorValue < 0 && sensorValue > 1023 ){
Serial.println("LDR Signal out of range.");
}
delay(500);
}
 

 
int sensorPin = A0;
int sensorValue = 0;
int led = 13;
void setup(){
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop(){
  sensorValue = analogRead(A0);
  Serial.print("LDR COunt = ");
  Serial.println(sensorValue);
  if(sensorValue <= 500){
    digitalWrite(led, HIGH);
  }else{
    digitalWrite(led, LOW);
  }
  delay(500);
}
 

   
int sensorPin = A0;
int sensorValue = 0;
int lowLed = 11;
int normalLed = 12;
int highLed = 13;
void setup(){
  pinMode(lowLed, OUTPUT);
  pinMode(normalLed, OUTPUT);
  pinMode(highLed, OUTPUT);
  Serial.begin(9600);
}
void loop(){
 sensorValue = analogRead(A0);
 Serial.print("LDR COunt = ");
 Serial.println(sensorValue);
 if(sensorValue <= 500){
   digitalWrite(lowLed, HIGH);
   digitalWrite(normalLed, LOW);
   digitalWrite(highLed, LOW);
 }else if (sensorValue > 500 && sensorValue < 750 ) {
   digitalWrite(lowLed, LOW);
   digitalWrite(normalLed, HIGH);
   digitalWrite(highLed, LOW);
 }else if (sensorValue > 750 && sensorValue < 1000 ){
   digitalWrite(lowLed, LOW);
   digitalWrite(normalLed, LOW);
  digitalWrite(highLed, HIGH);
 }
 delay(500);
}
 

   
const int sensorMin = 0; // sensor minimum count
const int sensorMax = 600; // sensor maximum count
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}
void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
  // do something different depending on the range value.
  switch (range) {
    case 0:
    Serial.println("dark");
    break;
    case 1:
    Serial.println("dim");
    break;
    case 2:
    Serial.println("medium");
    break;
    case 3:
    Serial.println("bright");
    break;
   }
  delay(100); // delay in between reads for stability
}
 

    
int sensorPin = A0;
int sensorValue = 0;
int led = 13;
void setup(){
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}
void loop(){
  sensorValue = analogRead(A0);
  Serial.print("LDR COunt = ");
  Serial.println(sensorValue);
  (sensorValue <= 500)?(digitalWrite(led,HIGH)):(digitalWrite(led, LOW));
  delay(500);
}
 

    
void setup(){
 pinMode(13, OUTPUT);
 for (int blinkCount = 1; blinkCount <= 5 ; blinkCount++) {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
  }
}
void loop(){
}
 

    
void setup(){
pinMode(13, OUTPUT);
int blinkCount = 1;
while ( blinkCount <= 5 ){
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
blinkCount++;
}
}
void loop(){
}
 

    
void setup(){
pinMode(13, OUTPUT);
int blinkCount = 1;
do{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
blinkCount++;
}while(blinkCount <= 5)
}
void loop(){
}
 

    
void setup(){
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 1; j++){
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
delay(500);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
delay(500);
}
for (int k = 0; k <= 1; k++){
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
delay(500);
}
}
}

void loop(){
}