You are on page 1of 46

Arduino Class Radical Learning Hub Programming Training

Arduino Programming

Daw Marlar Kyaw

B.E.(Electronics, YIT)

M.Sc.(Electronic Systems and Engineering Management), Germany

email: radical.learning.hub@gmail.com

1|Page
Arduino Class Radical Learning Hub Programming Training

Hardware Specifications of Arduino UNO

 Microcontroller: ATmega328
 Operating Voltage: 5V
 Input Voltage (recommended):7-12V
 Input Voltage (limits): 6- 20V
 Digital I/O Pins: 14 (of which 6 provide PWM output)
 Analog Input Pins: 6
 DC Current per I/O Pin: 40-mA
 DC Current for 3.3V Pin: 50-mA
 Flash Memory: 32 KB -(ATmega328)
 SRAM: 2 KB (ATmega328)
 EEPROM: 1 KB (ATmega328)
 Clock Speed: 16 MHz

2|Page
Arduino Class Radical Learning Hub Programming Training

Digital_output LED flasher

#define ledPin 9

void setup() {

pinMode(ledPin, OUTPUT);

void loop() {

digitalWrite(ledPin, HIGH);

delay(1000);

digitalWrite(ledPin, LOW);

delay(1000);

3|Page
Arduino Class Radical Learning Hub Programming Training

Traffic light control

# define red1 10

# define yellow1 9

# define green1 8

# define red2 13

# define yellow2 12

# define green2 11

int i;

void setup(){

pinMode(red1, OUTPUT);

pinMode(yellow1, OUTPUT);

pinMode(green1, OUTPUT);

pinMode(red2, OUTPUT);

pinMode(yellow2, OUTPUT);

pinMode(green2, OUTPUT);

void loop(){
4|Page
Arduino Class Radical Learning Hub Programming Training

digitalWrite(green1, HIGH);

digitalWrite(yellow1, LOW);

digitalWrite(red1, LOW);

digitalWrite(green2, LOW);

digitalWrite(yellow2, LOW);

digitalWrite(red2, HIGH);

delay(20000);

digitalWrite(green1, LOW);

digitalWrite(yellow1, HIGH);

delay(3000);

digitalWrite(yellow1, LOW);

digitalWrite(red1, HIGH);

digitalWrite(red2, LOW);

digitalWrite(green2, HIGH);

delay(20000);

digitalWrite(green2, LOW);

digitalWrite(yellow2, HIGH);

delay(3000);

7 SEGMENT

5|Page
Arduino Class Radical Learning Hub Programming Training

# define a 2
# define b 3
# define c 4
# define d 5
# define e 6
# define f 7
# define g 8
int i;
void setup()
{
for(i=2;i<9;i++)

6|Page
Arduino Class Radical Learning Hub Programming Training

{
pinMode(i,OUTPUT);
}
}
void loop()
{
display_0();
display_1();
display_2();
display_3();
display_4();
display_5();
display_6();
display_7();
display_8();
display_9();
}
void display_0()
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g,LOW);
delay(1000);
}
//SEVEN SEGMENT PORT ARDUINO
char table[]= {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
int i;
void setup()
{
for(i=0;i<8;i++)
{
pinMode(i,OUTPUT);
}
}
void loop()
{ for(i = 0;i<10;i++)

7|Page
Arduino Class Radical Learning Hub Programming Training

{
PORTD=table[i];
delay(1000);
}
for(i =9;i>=0;i--)
{
PORTD=table[i];
delay(1000);
}
}

Seven segment

byte seven_seg_digits[10][7] = { { 1,1,1,1,1,1,0 }, // = 0

{ 0,1,1,0,0,0,0 }, // = 1

{ 1,1,0,1,1,0,1 }, // = 2

{ 1,1,1,1,0,0,1 }, // = 3

{ 0,1,1,0,0,1,1 }, // = 4

{ 1,0,1,1,0,1,1 }, // = 5

8|Page
Arduino Class Radical Learning Hub Programming Training

{ 1,0,1,1,1,1,1 }, // = 6

{ 1,1,1,0,0,0,0 }, // = 7

{ 1,1,1,1,1,1,1 }, // = 8

{ 1,1,1,0,0,1,1 } // = 9

};

void setup() {

pinMode(2, OUTPUT);

pinMode(3, OUTPUT);

pinMode(4, OUTPUT);

pinMode(5, OUTPUT);

pinMode(6, OUTPUT);

pinMode(7, OUTPUT);

pinMode(8, OUTPUT);

pinMode(9, OUTPUT);

writeDot(0);

void writeDot(byte dot) {

digitalWrite(9, dot);

void sevenSegWrite(byte digit) {

byte pin = 2;

for (byte segCount = 0; segCount < 7; ++segCount) {

digitalWrite(pin, seven_seg_digits[digit][segCount]);

++pin;

9|Page
Arduino Class Radical Learning Hub Programming Training

void loop() {

for (byte count = 10; count > 0; --count) {

delay(1000);

sevenSegWrite(count - 1);

delay(4000);

Digital Input Output

# define buttonPin 2

# define ledPin 9

int buttonState = 0;

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);

10 | P a g e
Arduino Class Radical Learning Hub Programming Training

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

digitalWrite(ledPin, HIGH);

else {

digitalWrite(ledPin, LOW);

//Analog Input to delay

# define sensorPin A0

# define ledPin 9

int sensorValue = 0;

void setup() {

11 | P a g e
Arduino Class Radical Learning Hub Programming Training

Serial.begin(9600);

pinMode(ledPin, OUTPUT);

void loop() {

sensorValue = analogRead(sensorPin);

Serial.print("Input sensor value: ");

Serial.println(sensorValue);

digitalWrite(ledPin, HIGH);

delay(sensorValue);

digitalWrite(ledPin, LOW);

delay(sensorValue);

AnalogInput and Output Serial

12 | P a g e
Arduino Class Radical Learning Hub Programming Training

# define analogInPin A0

# define analogOutPin 9

int sensorValue = 0;

int outputValue = 0;

void setup() {

Serial.begin(9600);

void loop() {

sensorValue = analogRead(analogInPin);

outputValue = map(sensorValue, 0, 1023, 0, 255);

analogWrite(analogOutPin, outputValue);

Serial.print("sensor = " );

Serial.print(sensorValue);

Serial.print("\t output = ");

Serial.println(outputValue);

delay(500);

4x3 KEYPAD and 4X4 KEYPAD

//Keypad Test

#include <Keypad.h>

# define ROWS 4

# define COLS 4

char hexaKeys[ROWS][COLS] ={ {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','=','D'} };

byte rowPins[ROWS] = {0,1,2,3};

byte colPins[COLS] = {4,5,6,7};

13 | P a g e
Arduino Class Radical Learning Hub Programming Training

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){

Serial.begin(9600);

void loop(){

char key = customKeypad.getKey();

if (key){

Serial.println(key);

/////////////////////////////////////////////

Liquid Crystal Display (LCD)

14 | P a g e
Arduino Class Radical Learning Hub Programming Training

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

lcd.begin(16, 2);

lcd.print("hello, world!");

void loop() {

lcd.noDisplay();

delay(500);

lcd.display();

delay(500);

15 | P a g e
Arduino Class Radical Learning Hub Programming Training

Lcd test Lcd character table

16 | P a g e
Arduino Class Radical Learning Hub Programming Training

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

lcd.begin(16, 2);

lcd.print("hello, world!");

void loop() {

int i;

for(i=0x20;i<0x7f;i++){

lcd.setCursor(0, 1);

lcd.print(char(i));

delay(500);

for(i=0xa0;i<0xff;i++){

lcd.setCursor(0, 1);

lcd.print(char(i));

PIR sensor

MOTION SENSOR

17 | P a g e
Arduino Class Radical Learning Hub Programming Training

#define ledPin 13

#define inputPin 2

int pirState = LOW;

int val = 0;

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(inputPin, INPUT);

Serial.begin(9600);

void loop(){

val = digitalRead(inputPin);

if (val == HIGH) {

digitalWrite(ledPin, HIGH);

if (pirState == LOW) {

Serial.println("Motion detected!");

pirState = HIGH;

} } else {

digitalWrite(ledPin, LOW);

if (pirState == HIGH){

Serial.println("Motion ended!");

pirState = LOW;

} }

IR SENSOR MODULE

18 | P a g e
Arduino Class Radical Learning Hub Programming Training

IR detection

#define ledPin 9

#define irPin 2

int val;

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(irPin, INPUT);

Serial.begin(9600);

void loop(){

val = digitalRead(irPin);

if (val == LOW) {

digitalWrite(ledPin, HIGH);

Serial.println("Object detected!");

else {

digitalWrite(ledPin, LOW);

Serial.println("NO Object");

19 | P a g e
Arduino Class Radical Learning Hub Programming Training

delay(20);

DHT 11 temperature-humidity sensor

#include <dht.h>

dht DHT;

#define DHT11_PIN 5

void setup()

Serial.begin(9600);

Serial.println("DHT TEST PROGRAM ");

Serial.println();

Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");

void loop()

Serial.print("DHT11, \t");

int chk = DHT.read11(DHT11_PIN);

Serial.print(DHT.humidity, 1);

Serial.print(",\t");

20 | P a g e
Arduino Class Radical Learning Hub Programming Training

Serial.println(DHT.temperature, 1);

delay(2000);

COLOR SENSOR

#define OutPut 10

unsigned int R = 0, G = 0, B = 0;

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 7, 11, 12, 13); //RS,EN,D4,D5,D6,D7

void setup()

lcd.begin(16, 2);

pinMode(2, OUTPUT);

21 | P a g e
Arduino Class Radical Learning Hub Programming Training

pinMode(3, OUTPUT);//PINS 2, 3,4,5 as OUTPUT

pinMode(4, OUTPUT);

pinMode(5, OUTPUT);

pinMode(10, INPUT);//PIN 10 as input

digitalWrite(2,HIGH);

digitalWrite(3,LOW);//setting frequency selection to 20%

void loop()

lcd.print("R=");//printing name

digitalWrite(4,LOW);

digitalWrite(5,LOW);//setting for RED color sensor

R = pulseIn(OutPut, LOW);//reading frequency

lcd.print(R);//printing RED color frequency

lcd.print(" ");

lcd.setCursor(7, 0);//moving courser to position 7

delay(500);

lcd.print("B=");// printing name

digitalWrite(4,LOW);

digitalWrite(5,HIGH);// setting for BLUE color sensor

B = pulseIn(OutPut, LOW);// reading frequency

lcd.print(B);// printing BLUE color frequency

lcd.print(" ");

lcd.setCursor(0, 1);

22 | P a g e
Arduino Class Radical Learning Hub Programming Training

delay(500);

lcd.print("G=");// printing name

digitalWrite(4,HIGH);

digitalWrite(5,HIGH);// setting for GREEN color sensor

G = pulseIn(OutPut, LOW);// reading frequency

lcd.print(G);// printing GREEN color frequency

lcd.print(" ");

lcd.setCursor(0, 0);

delay(500);

DC MOTOR SPEED, DIRECTION CONTROL

#define enablePin 9

#define in1Pin 11

#define in2Pin 10

23 | P a g e
Arduino Class Radical Learning Hub Programming Training

#define switchPin 7

#define potPin 0

void setup()

pinMode(in1Pin, OUTPUT);

pinMode(in2Pin, OUTPUT);

pinMode(enablePin, OUTPUT);

pinMode(switchPin, INPUT_PULLUP);

void loop()

int speed = analogRead(potPin) / 4;

boolean reverse = digitalRead(switchPin);

setMotor(speed, reverse);

void setMotor(int speed, boolean reverse)

analogWrite(enablePin, speed);

digitalWrite(in1Pin, !reverse);

digitalWrite(in2Pin, reverse);

24 | P a g e
Arduino Class Radical Learning Hub Programming Training

TEMPERATURE DISPLAY WITH LED

void setup() {

pinMode(13,OUTPUT);

25 | P a g e
Arduino Class Radical Learning Hub Programming Training

pinMode(12,OUTPUT);

pinMode(11,OUTPUT);

pinMode(10,OUTPUT);

Serial.begin(9600);

void loop()

{int var,T;

var=analogRead(A0);

T=var*0.49;

Serial.print(T);

Serial.println("deg C");

if(T<10)

{ digitalWrite(13,HIGH);

digitalWrite(12,LOW);

digitalWrite(11,LOW);

digitalWrite(10,LOW);

else if (T<20)

digitalWrite(13,LOW);

digitalWrite(12,HIGH);

digitalWrite(11,LOW);

digitalWrite(10,LOW);

26 | P a g e
Arduino Class Radical Learning Hub Programming Training

else if (T<30)

digitalWrite(13,LOW);

digitalWrite(12,LOW);

digitalWrite(11,HIGH);

digitalWrite(10,LOW);

else

{ digitalWrite(13,LOW);

digitalWrite(12,LOW);

digitalWrite(11,LOW);

digitalWrite(10,HIGH);

delay(500);

27 | P a g e
Arduino Class Radical Learning Hub Programming Training

#include <LiquidCrystal.h>

long int Temp, T, i, F;

float TT;

LiquidCrystal lcd(4, 5, 0, 1, 2, 3);

void setup() {

lcd.begin(16, 2);

lcd.setCursor(0,0);

lcd.print("Temp Monitoring ");

delay(1000);

void loop() {

Temp = 0;

for(i = 0;i<30; i++)

{ Temp += analogRead(A0);

delay(2); }

TT = (float)Temp/30.0;

TT = (float)(TT/1024.0)*5000;

T = (int) TT/10.0;

lcd.setCursor(0,1);

lcd.print(T,DEC);

lcd.print(" degC,");

F = (T*9)/5+32;

lcd.print(F,DEC);

lcd.print(" degF");

28 | P a g e
Arduino Class Radical Learning Hub Programming Training

delay(1000);

Stepper motor control

#include <Stepper.h>

#define in1Pin 12

#define in2Pin 11

#define in3Pin 10

#define in4Pin 9

Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin);

void setup()

pinMode(in1Pin, OUTPUT);

pinMode(in2Pin, OUTPUT);

pinMode(in3Pin, OUTPUT);

pinMode(in4Pin, OUTPUT);

Serial.begin(9600);

motor.setSpeed(40);

void loop()

{ if (Serial.available())

int steps = Serial.parseInt();

motor.step(steps);

} }

29 | P a g e
Arduino Class Radical Learning Hub Programming Training

Servo Motor control

#include <Servo.h>

Servo myservo;

void setup()

myservo.attach(10);

void loop()

30 | P a g e
Arduino Class Radical Learning Hub Programming Training

myservo.write(0);

delay(1000);

myservo.write(45);

delay(1000);

myservo.write(90);

delay(1000);

myservo.write(135);

delay(1000);

myservo.write(180);

delay(1000);

myservo.write(135);

delay(1000);

myservo.write(90);

delay(1000);

myservo.write(45);

delay(1000);

myservo.write(0);

delay(1000);

PROJECTS

// SOLAR TRACKING

#include <Servo.h>

#include <LiquidCrystal.h>

31 | P a g e
Arduino Class Radical Learning Hub Programming Training

Servo myservo1,myservo2;

LiquidCrystal lcd(2,3,4,5,6,7);

#define potpin A0

int val1,val2, volt, volt1;

int i, d, deg, pos1,pos2;

float volt2;

void setup()

myservo1.attach(9);

myservo2.attach(0);

lcd.begin(16,2);

myservo1.write(90);

myservo2.write(90);

lcd.setCursor(0,0);

lcd.print("Solar Tracking");

lcd.setCursor(0,1);

lcd.print("Volt: ");

lcd.setCursor(12,1);

lcd.print(" V ");

pos1 = 90;

pos2 = 90;

void loop()

32 | P a g e
Arduino Class Radical Learning Hub Programming Training

val1 = analogRead(A0);

val2 = analogRead(A1);

long int total =0, i;

for(i=0; i<100; i++)

volt = analogRead(A3);

total = volt +total;

volt = total/100;

volt1 = map(volt, 0,1023,0,5000);

volt2 = volt1*3/1000.0;

lcd.setCursor(7,1);

lcd.print(volt2);

d = val1-val2;

if(d>35)

pos1--;

pos2++;

myservo1.write(pos1);

myservo2.write(pos2);

delay(20);

if(d<-35)

33 | P a g e
Arduino Class Radical Learning Hub Programming Training

pos1++;

pos2--;

myservo1.write(pos1);

myservo2.write(pos2);

delay(20);

//delay(100);

CAR PARKING GATE CONTROL

Gate control with servo

#include <LiquidCrystal.h>

#include <Servo.h>

Servo myservo;

#define IR1 8

34 | P a g e
Arduino Class Radical Learning Hub Programming Training

#define IR2 9

int S1, S2;

int count = 0;

// initialize1 the library with the numbers of the interface pins

LiquidCrystal lcd(4, 5, 0, 1, 2, 3);

void setup() {

pinMode(IR1, INPUT);

pinMode(IR2, INPUT);

pinMode(12, OUTPUT);

pinMode(13, OUTPUT);

myservo.attach(6);

myservo.write(0);

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

lcd.setCursor(0,0);

lcd.print("Gate Control ");

lcd.setCursor(0,1);

lcd.print("Count= ");

delay(100);

void loop() {

S1 = digitalRead(IR1);

S2 = digitalRead(IR2);

lcd.print(count);

if(S1==LOW)

35 | P a g e
Arduino Class Radical Learning Hub Programming Training

digitalWrite(12, HIGH);

lcd.setCursor(9,1);

lcd.print("Car In ");

myservo.write(90);

while(digitalRead(IR1)==LOW);

delay(20);

while(digitalRead(IR2)==HIGH);

delay(20);

while(digitalRead(IR2)==LOW);

delay(100);

myservo.write(0);

count++;

lcd.setCursor(6,1);

lcd.print(count);

lcd.print(" ");

delay(1000);

else if(S2 == LOW)

digitalWrite(13, HIGH);

lcd.setCursor(9,1);

lcd.print("Car Out");

myservo.write(90);

while(digitalRead(IR2)==LOW);

36 | P a g e
Arduino Class Radical Learning Hub Programming Training

delay(20);

while(digitalRead(IR1)==HIGH);

delay(20);

while(digitalRead(IR1)==LOW);

delay(100);

myservo.write(0);

count--;

lcd.setCursor(6,1);

lcd.print(count);

lcd.print(" ");

delay(1000);

lcd.setCursor(9,1);

lcd.print(" ");

digitalWrite(12, LOW);

digitalWrite(13, LOW);

KEYPAD SERVO GATE CONTROL

37 | P a g e
Arduino Class Radical Learning Hub Programming Training

#include <Servo.h>

#include <Keypad.h>

Servo myservo;

char* secretCode = "11111";

int position = 0;

int count = 0;

#define rows 4

#define cols 3

char keys[rows][cols] = { {'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'} };

byte rowPins[rows] = {3, 4, 5, 6};

38 | P a g e
Arduino Class Radical Learning Hub Programming Training

byte colPins[cols] = {0, 1, 2};

int i,flag;

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

#define redPin 11

#define greenPin 12

#define servoPin 10

#define lr1 7

#define lr2 8

#define lr3 9

#define lr4 13

int l1, l2, l3, l4;

void setup()

pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(servoPin,OUTPUT);

pinMode(lr1, INPUT);

pinMode(lr2, INPUT);

pinMode(lr3, INPUT);

pinMode(lr4, INPUT);

myservo.attach(10);

void loop()

39 | P a g e
Arduino Class Radical Learning Hub Programming Training

char key;

flag=1;

myservo.write(0);

l1 = digitalRead(lr1);

l2 = digitalRead(lr2);

l3 = digitalRead(lr3);

if(l1 == 0 && l2 == 0 && l3 ==1)

{ myservo.write(90);

delay(20);

do{

l4 = digitalRead(lr4);

}while(l4 == 1);

delay(20);

do{ l4 = digitalRead(lr4);

}while(l4 == 0);

delay(20);

myservo.write(0);

else if(l1 == 0 && l2 == 0 && l3 ==0)

{ flag = 1;

position=0;

key =0;

for(i=0;i<5;i++)

{ while((key= keypad.getKey())==0) ;

40 | P a g e
Arduino Class Radical Learning Hub Programming Training

if (key == secretCode[position])

{ digitalWrite(greenPin,HIGH);

delay(100);

digitalWrite(greenPin,LOW);

position++;

else

{ digitalWrite(redPin,HIGH);

delay(100);

digitalWrite(redPin,LOW);

digitalWrite(redPin,HIGH);

delay(100);

digitalWrite(redPin,LOW);

flag = 0;

break;

if (flag == 1)

{ myservo.write(90);

delay(20);

do{

l4 = digitalRead(lr4);

}while(l4 == 1);

delay(20);

41 | P a g e
Arduino Class Radical Learning Hub Programming Training

do{

l4 = digitalRead(lr4);

} while(l4 == 0);

delay(20);

myservo.write(0);

else {

for(i=0;i<5;i++)

{ digitalWrite(redPin,HIGH);

delay(100);

digitalWrite(redPin,LOW);

delay(100);

key=0;

delay(100);

Ultrasonic Sensor

HC-SR04 Ultrasonic Distance Sensor is a popular and low cost solution for non-contact distance
measurement function. It is able to measure distances from 2cm to 400cm with an accuracy of
about 3mm. This module includes ultrasonic transmitter, ultrasonic receiver and its control
circuit.

HC-SR04 module has 4 pins :

 VCC – 5V, +ive of the power supply


 TRIG – Trigger Pin

42 | P a g e
Arduino Class Radical Learning Hub Programming Training

 ECHO – Echo Pin


 GND – -ive of the power supply

TRIG and ECHO pins can be used to interface this module with a microcontroller unit. These are
TTL (0 – 5V) input output pins

HC-SR04 Ultrasonic Module Working

Ultrasonic Module Operation

1. Provide TRIGGER signal, at least 10μS High Level (5V) pulse.


2. The module will automatically transmit eight 40kHz ultrasonic burst.
3. If there is an obstacle in-front of the module, it will reflect the ultrasonic burst.
4. If the signal is back, ECHO output of the sensor will be in HIGH state (5V) for a duration
of time taken for sending and receiving ultrasonic burst. Pulse width ranges from about
150μS to 25mS and if no obstacle is detected, the echo pulse width will be about 38ms.

43 | P a g e
Arduino Class Radical Learning Hub Programming Training

#include <LiquidCrystal.h>

#define trigger 10

#define echo 11

#define motor 8

#define buzzer 12

LiquidCrystal lcd(7,6,5,4,3,2);

float time=0,distance=0;

int temp=0;

void setup()

lcd.begin(16,2);

pinMode(trigger,OUTPUT);

pinMode(echo,INPUT);

pinMode(motor, OUTPUT);

pinMode(buzzer, OUTPUT);

lcd.print(" Water Level ");

lcd.setCursor(0,1);

44 | P a g e
Arduino Class Radical Learning Hub Programming Training

lcd.print(" Indicator ");

delay(2000);

void loop()

{ lcd.clear();

digitalWrite(trigger,LOW);

delayMicroseconds(2);

digitalWrite(trigger,HIGH);

delayMicroseconds(10);

digitalWrite(trigger,LOW);

delayMicroseconds(2);

time=pulseIn(echo,HIGH);

distance=time*0.0340/2;

lcd.clear();

lcd.print("Water Space In ");

lcd.setCursor(0,1);

lcd.print("Tank is: ");

lcd.print(distance);

lcd.print("Cm");

delay(2000);

if(distance<12 && temp==0)

{ digitalWrite(motor, LOW);

digitalWrite(buzzer, HIGH);

lcd.clear();

lcd.print("Water Tank Full ");

45 | P a g e
Arduino Class Radical Learning Hub Programming Training

lcd.setCursor(0,1);

lcd.print("Motor Turned OFF");

delay(2000);

digitalWrite(buzzer, LOW);

delay(3000);

temp=1;

else if(distance<12 && temp==1)

{ digitalWrite(motor, LOW);

lcd.clear();

lcd.print("Water Tank Full ");

lcd.setCursor(0,1);

lcd.print("Motor Turned OFF");

delay(5000);

else if(distance>30)

{ digitalWrite(motor, HIGH);

lcd.clear();

lcd.print("LOW Water Level");

lcd.setCursor(0,1);

lcd.print("Motor Turned ON");

delay(5000);

temp=0;

46 | P a g e

You might also like