Arduino Practical Questions/Answers
1. Arduino program to run a counter and display in LCD?
1. #include
2. int seconds = 0;
3. Adafruit_LiquidCrystal lcd(0);
4. void setup()
5. {
6. lcd.begin(16,2);
7. lcd.print("Start");
8. }
9. void loop()
10. {
11. lcd.setCursor(0, 1);
12. lcd.print(seconds);
13. lcd.setBacklight(1);
14. delay(500); // Wait for 500 millisecond(s)
15. lcd.setBacklight(0);
16. delay(500); // Wait for 500 millisecond(s)
17. seconds += 1;
18. }
2. Arduino program to connect a led through push button?
int buttonState = 0;
void setup()
{
pinMode(2, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
// read the state of the pushbutton value
buttonState = digitalRead(2);
if (buttonState == HIGH)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else {
digitalWrite(LED_BUILTIN, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}
3. Arduino program to print all integer number from 0 to 99
using for loop
void setup()
{
Serial.begin(9600);
for(int a=0;a<100;a++)
{
Serial.println(a);
}
}
void loop()
{
}
4. Arduino program to read a integer number and print its
table
int num = 0;
int a;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0)
{
num = Serial.parseInt();
Serial.print("Table of : ");
Serial.println(num);
for(a=1;a<=10;a++)
{
Serial.println(num*a);
}
}
}
5.Arduino program to print the square root of a given
number
void setup() {
Serial.begin(9600);
void loop() {
if (Serial.available() > 0) {
float number = Serial.parseFloat();
float squareRootValue = sqrt(number);
Serial.print("Square Root of ");
Serial.print(number);
Serial.print(" is: ");
Serial.println(squareRootValue);
6. Arduino program to interface buzzer with Arduino board to buzz on off
with the delay of 1sec.
const int BUZZER = 9;//buzzer to arduino pin 9
void setup()
{
pinMode(BUZZER, OUTPUT);//7 Set buzzer - pin 9 as an output
}
void loop()
{
tone(BUZZER, 1000); // Send 1KHz sound signal...
delay(1000);//for 1 sec
noTone(BUZZER); //Stop sound...
delay(1000); //..for 1sec
}
7.Arduino program to print Hello word in LCD
#include
Adafruit_LiquidCrystal lcd(0);
void setup()
{
lcd.begin(16,1);
lcd.print("hello world");
}
void loop()
{
8.Arduino program to interface buzzer with LDR and LED
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(8,OUTPUT);
pinMode(A0,INPUT);
}
void loop()
{
int signal;
signal=analogRead(A0);
Serial.println(signal);
if(signal>=400)
{
tone(8,1000);
digitalWrite(13,HIGH);
delay(100);
noTone(8);
digitalWrite(13,LOW);
delay(100);
Serial.println("ALARM ACTIVATED");
}
else
{
noTone(8);
digitalWrite(13,LOW);
Serial.println("ALARM DEACTIVATED");
}
}