ES Lab Experiment Program Code:
1. Built-in LED state control by push button sketch implementation.
Code: // Define constants for the button and LED pins
const int buttonPin = 2;
const int ledPin = 13;
// Variable to store the state of the button
int buttonState = 0;
void setup() {
// Set the button pin as INPUT
pinMode(buttonPin, INPUT);
// Set the LED pin as OUTPUT
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the state of the button (HIGH or LOW)
buttonState = digitalRead(buttonPin);
// Check if the button is pressed (buttonState is LOW)
if (buttonState == LOW) {
// If the button is pressed, turn on the LED
digitalWrite(ledPin, HIGH);
} else {
// If the button is not pressed, turn off the LED
digitalWrite(ledPin, LOW);
}
}
Certainly! Below is an example Arduino sketch that implements the control of the built-in LED
using a push button. This sketch assumes that the push button is connected to pin 2 and the
built-in LED is connected to pin 13 on an Arduino board.
cpp
// Define constants for the button and LED pins
const int buttonPin = 2;
const int ledPin = 13;
// Variable to store the state of the button
int buttonState = 0;
void setup() {
// Set the button pin as INPUT
pinMode(buttonPin, INPUT);
// Set the LED pin as OUTPUT
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the state of the button (HIGH or LOW)
buttonState = digitalRead(buttonPin);
// Check if the button is pressed (buttonState is LOW)
if (buttonState == LOW) {
// If the button is pressed, turn on the LED
digitalWrite(ledPin, HIGH);
} else {
// If the button is not pressed, turn off the LED
digitalWrite(ledPin, LOW);
}
}
Explanation:
● The sketch defines constants for the button pin (buttonPin) and the LED pin
(ledPin).
● In the setup() function, it sets the button pin as INPUT and the LED pin as OUTPUT.
● The loop() function continuously reads the state of the button using
digitalRead(buttonPin).
● If the button is pressed (the button state is LOW), it turns on the LED using
digitalWrite(ledPin, HIGH).
● If the button is not pressed, it turns off the LED using digitalWrite(ledPin, LOW).
2. Built-in LED blinking sketch implementation
Code: // Define the LED pin
const int ledPin = 13;
void setup() {
// Set the LED pin as OUTPUT
pinMode(ledPin, OUTPUT);
}
void loop() {
// Turn on the LED
digitalWrite(ledPin, HIGH);
// Wait for 1 second (1000 milliseconds)
delay(1000);
// Turn off the LED
digitalWrite(ledPin, LOW);
// Wait for 1 second
delay(1000);
}
Explanation:
3. The sketch defines a constant ledPin to represent the pin connected to the built-in LED
(pin 13 on most Arduino boards).
4. In the setup() function, it sets the LED pin as OUTPUT, indicating that the pin will be
used to output a digital signal.
5. The loop() function contains the main code that runs continuously.
6. Inside the loop, it turns on the LED by setting the digital output of the LED pin to HIGH
using digitalWrite(ledPin, HIGH).
7. It then waits for 1 second using delay(1000) to keep the LED on.
8. After that, it turns off the LED by setting the digital output to LOW using
digitalWrite(ledPin, LOW).
9. Another delay of 1 second is added to keep the LED off.
10. The loop then repeats, creating a simple blinking effect.
3. Built-in LED blinking by toggling states based on binary operation.
Code: // Define the LED pin
const int ledPin = 13;
void setup() {
// Set the LED pin as OUTPUT
pinMode(ledPin, OUTPUT);
}
void loop() {
// Toggle the state of the LED using XOR (^) bitwise operation
digitalWrite(ledPin, digitalRead(ledPin) ^ 1);
// Wait for 1 second (1000 milliseconds)
delay(1000);
}
Explanation:
11. The sketch toggles the state of the built-in LED using the XOR (^) bitwise operation.
12. In the setup() function, it sets the LED pin as OUTPUT.
13. The loop() function contains the main code that runs continuously.
14. Inside the loop, it uses digitalRead(ledPin) to read the current state of the LED
(HIGH or LOW).
15. The XOR operation (^) is then used to toggle the state: digitalRead(ledPin) ^ 1.
16. The result is written back to the LED pin using digitalWrite(ledPin, ...).
17. The sketch then waits for 1 second using delay(1000).
18. The loop repeats, creating a blinking effect by toggling the LED state
4. Built-in LED state control by user interface through serial port.
Code: // Define the LED pin
const int ledPin = 13;
void setup() {
// Set the LED pin as OUTPUT
pinMode(ledPin, OUTPUT);
// Start serial communication
Serial.begin(9600);
}
void loop() {
// Check for incoming serial data
if (Serial.available() > 0) {
// Read the incoming command
String command = Serial.readStringUntil('\n');
// Process the command
if (command == "ON") {
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("LED is ON");
} else if (command == "OFF") {
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("LED is OFF");
} else {
Serial.println("Invalid command"); // Print an error for invalid
commands
}
}
}
Explanation:
19. The sketch defines a constant ledPin to represent the pin connected to the
built-in LED (pin 13 on most Arduino boards).
20. In the setup() function, it sets the LED pin as OUTPUT and starts serial
communication at a baud rate of 9600.
21. The loop() function contains the main code that runs continuously.
22. Inside the loop, it checks for incoming serial data using Serial.available().
23. If there is data available, it reads the incoming command until a newline character
('\n') is encountered using Serial.readStringUntil('\n').
24. It then processes the received command. If the command is "ON," it turns on the
LED; if it's "OFF," it turns off the LED. For any other command, it prints an error
message.
25. The result of each command is sent back to the Serial Monitor.
5. User interface for boolean operation and bit wise operation through serial port.
Code: // Define the maximum length of the user input
const int maxInputLength = 20;
void setup() {
// Start serial communication
Serial.begin(9600);
// Wait for serial connection to be established
while (!Serial) {
delay(1000);
}
// Display instructions in the Serial Monitor
Serial.println("Enter a boolean expression or bitwise operation:");
Serial.println("Boolean expressions: AND, OR, NOT");
Serial.println("Bitwise operations: & (AND), | (OR), ^ (XOR), ~ (NOT)");
Serial.println("Example: 1 & 0");
}
void loop() {
// Check for incoming serial data
if (Serial.available() > 0) {
// Read the incoming user input until a newline character is encountered
char userInput[maxInputLength];
Serial.readBytesUntil('\n', userInput, maxInputLength);
// Process the user input
processUserInput(userInput);
}
}
void processUserInput(char* userInput) {
// Display the user input
Serial.print("Input: ");
Serial.println(userInput);
// Parse and process the user input
int operand1, operand2, result;
char operation;
// Extract operands and operation from the user input
int parseResult = sscanf(userInput, "%d %c %d", &operand1, &operation, &operand2);
// Check if parsing was successful
if (parseResult == 3) {
// Perform the operation based on the user input
switch (operation) {
case '&':
result = operand1 & operand2; // Bitwise AND
break;
case '|':
result = operand1 | operand2; // Bitwise OR
break;
case '^':
result = operand1 ^ operand2; // Bitwise XOR
break;
case '~':
result = ~operand1; // Bitwise NOT
break;
case 'A':
case 'a':
result = operand1 && operand2; // Logical AND
break;
case 'O':
case 'o':
result = operand1 || operand2; // Logical OR
break;
case 'N':
case 'n':
result = !operand1; // Logical NOT
break;
default:
Serial.println("Invalid operation");
return;
}
// Display the result
Serial.print("Result: ");
Serial.println(result);
} else {
// Display an error message for invalid input
Serial.println("Invalid input format. Please enter a valid expression.");
}
}
5. write an embedded code Controlling multiple LEDs with a loop and an array.
Code: // Define the number of LEDs
const int numLEDs = 3;
// Define an array to store the LED pin numbers
const int ledPins[numLEDs] = {2, 3, 4};
// Set the delay time between LED changes
const int delayTime = 500;
void setup() {
// Set each LED pin as OUTPUT
for (int i = 0; i < numLEDs; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// Turn on each LED in sequence
for (int i = 0; i < numLEDs; i++) {
digitalWrite(ledPins[i], HIGH);
delay(delayTime);
digitalWrite(ledPins[i], LOW);
}
// Turn off each LED in reverse sequence
for (int i = numLEDs - 1; i >= 0; i--) {
digitalWrite(ledPins[i], HIGH);
delay(delayTime);
digitalWrite(ledPins[i], LOW);
}
}
6. Write an embedded code for Use an analog output (PWM pin) to fade an LED.
Code: // Define the LED pin
const int ledPin = 9;
void setup() {
// Set the LED pin as OUTPUT
pinMode(ledPin, OUTPUT);
}
void loop() {
// Fade in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness);
delay(10); // Adjust the delay for the speed of fading
}
// Fade out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness);
delay(10); // Adjust the delay for the speed of fading
}
}
7. Write an embedded code for Servo Motor Control using PWM.
Code: #include <Servo.h>
// Create a Servo object
Servo myServo;
// Define the servo motor pin
const int servoPin = 9;
void setup() {
// Attach the servo to the specified pin
myServo.attach(servoPin);
}
void loop() {
// Move the servo to the 0-degree position
myServo.write(0);
delay(1000); // Wait for 1 second
// Move the servo to the 90-degree position
myServo.write(90);
delay(1000); // Wait for 1 second
// Move the servo to the 180-degree position
myServo.write(180);
delay(1000); // Wait for 1 second
}
Explanation:
26. The sketch includes the Servo library, which simplifies servo motor control.
27. It creates a Servo object named myServo.
28. The constant servoPin is defined to specify the pin to which the servo motor is
connected.
29. In the setup() function, the myServo object is attached to the specified servo pin
using attach(servoPin).
30. The loop() function contains the main code that runs continuously.
31. The write function is used to move the servo motor to specific angles: 0 degrees,
90 degrees, and 180 degrees.
32. A delay of 1000 milliseconds (1 second) is added between each movement to
observe the positions.
8. write an embedded code for Temperature sensor interfacing and sending its reading
over serial port.
Code: // Define the analog pin for the temperature sensor
const int sensorPin = A0;
void setup() {
// Start serial communication
Serial.begin(9600);
}
void loop() {
// Read the analog voltage from the temperature sensor
int sensorValue = analogRead(sensorPin);
// Convert the analog value to temperature in Celsius
float temperatureC = (sensorValue * 5.0 / 1024.0) * 100.0;
// Print the temperature to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" degrees Celsius");
// Wait for a short time before reading again
delay(1000);
}
10. write an embedded code for I2C light sensor interfacing and sending its reading over
serial port.
Code: #include <Wire.h>
#include <BH1750.h>
// Create an instance of the BH1750 class
BH1750 lightSensor;
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize the light sensor
lightSensor.begin();
}
void loop() {
// Request light level measurement from the sensor
uint16_t lightLevel = lightSensor.readLightLevel();
// Print the light level to the Serial Monitor
Serial.print("Light Level: ");
Serial.print(lightLevel);
Serial.println(" lux");
// Wait for a short time before reading again
delay(1000);
}
Explanation:
● The sketch includes the Wire library for I2C communication and the BH1750 library,
which provides functions for interfacing with the BH1750 light sensor.
● An instance of the BH1750 class is created as lightSensor.
● In the setup() function, serial communication is started, and the light sensor is
initialized using lightSensor.begin().
● The loop() function contains the main code that runs continuously.
● It requests the light level measurement from the sensor using
lightSensor.readLightLevel().
● Prints the light level to the Serial Monitor.
● Adds a delay of 1000 milliseconds (1 second) before reading again.
To use this sketch:
1. Connect the BH1750 light sensor to the I2C pins on your Arduino (SDA to A4, SCL to
A5).
2. Upload the sketch to your Arduino.
3. Open the Serial Monitor in the Arduino IDE (Tools > Serial Monitor).
4. Observe the light level readings printed to the Serial Monitor.