arduino distance measurement with lcd and leds code

Mr.ElectroUino
1


                                     Tutorial  @Mr ProjectsoPedia
In this blog you will learn about how to meaure the distance with help of hc-sr04 ultrasonic sensor arduino. We can display the distance on the lcd display.


Component requirements:

    1. Arduino.

    2. ultrasonic sensor( Hc-sr04 ).

    3. LCD display with I2C

    4. Resistor 220ohm.

    5. LED.

    6. Breadboard.

    7. Jumper wire.

CIRCUIT DIAGRAM:

Pin connection:

1. connect the LEDs on the breadboard and make sure that the cathode pin is connected to the ground.

2. Connect led 1 to arduino digital pin 13.
3. Connect led 2 to arduino digital pin 12.
4. Connect led 3 to arduino digital pin 11.
5. Connect led 4 to arduino digital pin 10.
6. Connect led 5 to arduino digital pin 9.
7. Connect led 6 to arduino digital pin 8.
8. Connect led 7 to arduino digital pin 7.
9. Connect led 8 to arduino digital pin 6.
10. Connect led 9 to arduino digital pin 5.
11. Connect led 10 to arduino digital pin 4.
Don't forget to add a current limit resistor to the led Anode pin. Its value is 220 resistor.
12. Connect Hc-sr04 Vcc pin to arduino 5v pin.
13. Connect Hc-sr04 GND pin to arduino digital GND pin
14. Connect Hc-sr04 Trig pin to arduino digital pin 3.
15. Connect Hc-sr04 Echo pin to arduino digital pin 2.

16. Connect LCD I2C  Vcc pin to arduino 5v pin.
17. Connect LCD I2C GND pin to arduino digital GND pin
18. Connect LCD I2C SDA pin to arduino analog pin A4.
19. Connect LCD I2C Echo pin to arduino analog pin A5.
Now circuit is complete.


LCD I2C Address Finder CODE:

#include <Wire.h>

void setup()

{

Wire.begin();

Serial.begin(9600);

Serial.println("\nI2C Scanner");

}

void loop()

{

byte error, address;

int Devices;

Serial.println("Scanning...");

Devices = 0;

for(address = 1; address < 127; address++ )

{

Wire.beginTransmission(address);

error = Wire.endTransmission();

if (error == 0)

{

Serial.print("I2C device found at address 0x");

if (address<16)

Serial.print("0");

Serial.print(address,HEX);

Serial.println("  !");

Devices++;

}

else if (error==4)

{

Serial.print("Unknown error at address 0x");

if (address<16)

Serial.print("0");

Serial.println(address,HEX);

}

}

if (Devices == 0)

Serial.println("No I2C devices found\n");

else

Serial.println("done\n");

delay(5000);          

}


Distance measuring CODE:

#include <Wire.h>

#include <LiquidCrystal_I2C.h> //First instal the library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library

// Set the LCD address.

//if you don't know what is your lcd address.

//just upload lcd I2C address Finder code and check the serial moniter.

LiquidCrystal_I2C lcd(0x3f, 16, 2);

const int trig = 2;

const int echo = 3; 

int duration = 0;

int distance = 0;

int i;

void setup()

{

  //ultrasonic sensor

  pinMode(trig , OUTPUT);

  pinMode(echo , INPUT);

  //LEDS

  for (i = 4; i <= 13; i++) {

    pinMode(i, OUTPUT);

  }

  //LCD display

  lcd.begin();

  lcd.backlight();

  Serial.begin(9600);

}

void loop()

{

  digitalWrite(trig , HIGH);

  delayMicroseconds(1000);

  digitalWrite(trig , LOW);

  duration = pulseIn(echo , HIGH);

  distance = (duration / 2) / 29.1 ;

  Serial.println(distance);

  lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("Distance: "); 

  lcd.print(distance);

  lcd.print("CM"); 

  delay(500);

  if (distance >= 10) {     //change the value for distance measuring

    digitalWrite(13, HIGH);

  }else{

  digitalWrite(13, LOW);

  }

  if (distance >= 13) {

    digitalWrite(12, HIGH);

  }else{

  digitalWrite(12, LOW);

  }

  if (distance >= 15) {

    digitalWrite(11, HIGH);

  }else{

  digitalWrite(11, LOW);

  }

  if (distance >= 18) {

    digitalWrite(10, HIGH);

  }else{

  digitalWrite(10, LOW);

  }

  if (distance >= 21) {

    digitalWrite(9, HIGH);

  }else{

  digitalWrite(9, LOW);

  }

  if (distance >= 24) {

    digitalWrite(8, HIGH);

  }else{

  digitalWrite(8, LOW);

  }

  if (distance >= 27) {

    digitalWrite(7, HIGH);

  }else{

  digitalWrite(7, LOW);

  }

  if (distance >= 30) {

    digitalWrite(6, HIGH);

  }else{

  digitalWrite(6, LOW);

  }

  if (distance >= 33) {

    digitalWrite(5, HIGH);

  }else{

  digitalWrite(5, LOW);

  }

  if (distance >= 36) {

    digitalWrite(4, HIGH);

  }else{

  digitalWrite(4, LOW);

  }

},,


Effect 2 CODE:

#include <Wire.h>
#include <LiquidCrystal_I2C.h> //First instal the library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
// Set the LCD address.
//if you don't know what is your lcd address.
//just upload lcd I2C addressFinder code and check the serial moniter.
LiquidCrystal_I2C lcd(0x3f, 16, 2);
#define t1  20
const int trig = 2;
const int echo = 3; 
int duration = 0;
int distance = 0;
void setup()
{
  //ultrasonic sensor
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  //LEDS
  for (int i = 4; i <= 13; i++) {
    pinMode(i, OUTPUT);
  }
  //LCD display
  lcd.begin();
  lcd.backlight();
  Serial.begin(9600);
}
void loop()
{
  digitalWrite(trig , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig , LOW);
  duration = pulseIn(echo , HIGH);
  distance = (duration / 2) / 29.1 ;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Distance: "); 
  lcd.print(distance);
  if (distance <= 30) {
    lcd.setCursor(0,1);
    lcd.print("Distance is < 30");
    delay(100);
  effect_1(); 
  }
  else{
    clear_led();
    lcd.setCursor(0,1);
    lcd.print("Distance is > 30");
    delay(100);
  }
}

//left to right and right to left
void effect_1()
{
  for (int i = 4; i <= 13; i++) {
    digitalWrite(i, HIGH);
    delay(t1);
    digitalWrite(i + 1, HIGH);
    delay(t1);
    digitalWrite(i + 2, HIGH);
    delay(t1);
    digitalWrite(i, LOW);
    delay(t1);
    digitalWrite(i + 1, LOW);
    delay(t1);
  }
  for (int i = 13; i >= 4; i--) {
    digitalWrite(i, HIGH);
    delay(t1);
    digitalWrite(i - 1, HIGH);
    delay(t1);
    digitalWrite(i - 2, HIGH);
    delay(t1);
    digitalWrite(i, LOW);
    delay(t1);
    digitalWrite(i - 1, LOW);
    delay(t1);
  }
}
// function to switch all LEDs off
void clear_led(void)
{
  for (int i = 4; i <= 13; i++) {
    digitalWrite(i, LOW);
  }
}


 Watch This Tutorial: 



Post a Comment

1Comments

Post a Comment