1. Arduino.
2. ultrasonic sensor( Hc-sr04 ).
3. LCD display with I2C
4. Resistor 220ohm.
5. LED.
6. Breadboard.
7. Jumper wire.
CIRCUIT DIAGRAM:
#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);
}
},,
#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 20const int trig = 2;const int echo = 3;int duration = 0;int distance = 0;void setup(){//ultrasonic sensorpinMode(trig , OUTPUT);pinMode(echo , INPUT);//LEDSfor (int i = 4; i <= 13; i++) {pinMode(i, OUTPUT);}//LCD displaylcd.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 leftvoid 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 offvoid clear_led(void){for (int i = 4; i <= 13; i++) {digitalWrite(i, LOW);}}
Verry nice your project.👍
ReplyDelete