Fis Alm Q&a
Fis Alm Q&a
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(2000);
}
Formulate an Arduino code to smoothly vary the brightness of an LED using the Analog
Write technique.
#define LED_PIN 11
#define POTENTIOMETER_PIN A0
void setup()
{
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
int potentiometerValue = analogRead(POTENTIOMETER_PIN);
int brightness = potentiometerValue / 4;
analogWrite(LED_PIN, brightness);
}
Build a Arduino program to read the state of three push buttons connected to Pins 2, 3,
and 4, and light up corresponding LEDs connected to Pins 8, 9, and 10.
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int ledPin1 = 8;
const int ledPin2 = 9;
const int ledPin3 = 10;
void setup() {
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop() {
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
if (buttonState1 == LOW) {
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}
if (buttonState2 == LOW) {
digitalWrite(ledPin2, HIGH);
} else {
digitalWrite(ledPin2, LOW);
}
if (buttonState3 == LOW) {
digitalWrite(ledPin3, HIGH);
} else {
digitalWrite(ledPin3, LOW);
}
}
Create an Arduino code to manage the intensity of an LED's illumination using PWM
(Pulse Width Modulation) on a designated digital pin.
const int ledPin = 9;
void setup() {
}
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness);
delay(10);
}
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness);
delay(10);
}
}
1. Compose an Arduino program to Blink 2 LED’s alternatively with a delay of one
second.
const int ledPin1 = 8;
const int ledPin2 = 9;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(1000);
}
Compose an Arduino program to create a blinking effect for an LED using the Analog
Write method.
const int ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
analogWrite(ledPin, 255);
delay(1000); // Wait for 1 second
analogWrite(ledPin, 0);
delay(1000); // Wait for 1 second
}
CO-2, ALM Questions and Answers
Develop an Arduino code that reads data from a temperature sensor (LM35) and sends the
temperature value to the serial monitor.
const int LM35_PIN = A0; // Analog pin connected to the LM35 sensor
void setup() {
Serial.begin(9600); // Initialize the serial monitor
}
void loop() {
int rawValue = analogRead(LM35_PIN); // Read the analog value from LM35
float voltage = (rawValue / 1023.0) * 5.0; // Convert raw value to voltage
float temperatureC = (voltage - 0.5) * 100.0; // Convert voltage to temperature in Celsius
Create an Arduino program that reads data from a gas sensor and sounds a buzzer if the gas
concentration exceeds a certain threshold.
const int gasSensorPin = A0; // Analog pin connected to the gas sensor
const int buzzerPin = 2; // Digital pin connected to the buzzer
const int gasThreshold = 500; // Set your gas concentration threshold here
void setup() {
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600); // Initialize the serial monitor for debugging
}
void loop() {
int gasValue = analogRead(gasSensorPin); // Read analog value from the gas sensor
Serial.print("Gas Concentration: ");
Serial.println(gasValue);
if (gasValue > gasThreshold) {
// Gas concentration exceeds the threshold, sound the buzzer
digitalWrite(buzzerPin, HIGH);
delay(1000); // Buzzer sounds for 1 second (adjust as needed)
digitalWrite(buzzerPin, LOW);
}
delay(1000); // Delay for 1 second (adjust as needed between readings)
}
Create an Arduino sketch that reads data from an LDR and turns on an LED if the light intensity
falls below a certain threshold.
const int ldrPin = A0; // Analog pin connected to the LDR
const int ledPin = 2; // Digital pin connected to the LED
const int lightThreshold = 300; // Set your light intensity threshold here
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Initialize the serial monitor for debugging
}
void loop() {
int lightValue = analogRead(ldrPin); // Read analog value from the LDR
Serial.print("Light Intensity: ");
Serial.println(lightValue);
void setup() {
Serial.begin(9600); // Initialize the serial monitor
}
void loop() {
int thermistorValue = analogRead(thermistorPin); // Read the analog value
// Calculate the resistance of the thermistor using the voltage divider formula
float voltage = (5.0 * thermistorValue) / 1023.0;
float thermistorResistance = (referenceResistor * voltage) / (5.0 - voltage);
Develop an Arduino code that interfaces with a gas sensor to measure the concentration of a
specific gas and displays it on the serial monitor.
const int gasSensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int gasValue = analogRead(gasSensorPin);
float gasConcentration = convertToGasConcentration(gasValue);
Serial.print("Gas Concentration: ");
Serial.print(gasConcentration);
Serial.println(" ppm");
delay(1000);
}
float convertToGasConcentration(int analogValue) {
return analogValue;
}
Create an Arduino sketch that continuously measures distances using an ultrasonic sensor and
logs the data to the serial monitor.
#define trigPin 9 // Trigger pin of the ultrasonic sensor (connect to digital pin 9)
#define echoPin 10 // Echo pin of the ultrasonic sensor (connect to digital pin 10)
void setup() {
Serial.begin(9600); // Initialize the serial monitor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
Serial.print("Distance: ");
Serial.print(distanceCM);
Serial.println(" cm");
delay(1000);
}