CS3691 - Embedded System and IoT Lab - Arduino Programs
Arduino Platform and Programming
Arduino is an open-source electronics platform based on easy-to-use hardware and software.
It consists of a physical programmable circuit board (microcontroller) and a development environment (Arduino IDE)
used to write and upload code.
Key Features of Arduino UNO:
- Microcontroller: ATmega328P
- Digital I/O Pins: 14 (6 support PWM)
- Analog Input Pins: 6
- Operating Voltage: 5V
- Clock Speed: 16 MHz
Arduino programs are written in a simplified version of C/C++ and uploaded using USB cable through the Arduino IDE.
Basic Structure of an Arduino Program:
1. setup(): Runs once when the program starts.
2. loop(): Repeats indefinitely.
1. LED Blinking
Aim:
To blink an LED connected to digital pin 13 of Arduino UNO.
Program:
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
Output:
The LED connected to pin 13 will blink ON for 1 second and OFF for 1 second repeatedly.
Result:
The LED blinking program was successfully executed and verified.
2. Button Controlled LED
Aim:
To control an LED using a push button connected to Arduino UNO.
Program:
void setup() {
pinMode(2, INPUT);
pinMode(13, OUTPUT);
CS3691 - Embedded System and IoT Lab - Arduino Programs
}
void loop() {
int state = digitalRead(2);
if(state == HIGH) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
Output:
When the button is pressed, the LED turns ON. When released, the LED turns OFF.
Result:
Button control of LED was implemented and verified successfully.
3. LDR Sensor with LED
Aim:
To turn ON an LED in darkness using an LDR sensor connected to Arduino UNO.
Program:
int ldrPin = A0;
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int ldrValue = analogRead(ldrPin);
Serial.println(ldrValue);
if(ldrValue < 300) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(500);
}
Output:
In low light, the LED turns ON. In bright light, the LED turns OFF. LDR values are displayed on Serial Monitor.
Result:
The LDR-based LED control was executed successfully and tested under different light conditions.