Sie sind auf Seite 1von 14

APPLICATION

Distance sensors can precisely measure the distance to a target object. Among the
advantages of ultrasonic sensors over traditional sensors is the ability to detect and
measure moving objects. Ultrasonic sensors are not affected by the color of the object
and they can detect small objects over long distances.

Ultrasonic sensors can measure the distance to a wide range of objects regardless of
shape, color or surface texture. They are also able to measure an approaching or
receding object. By using “non-contact” ultrasonic sensors, distances can be measured
without damage to the object. They’re easy to use and, in many cases, can be used in
place of other traditional sensors when the environmental conditions make traditional
sensors unusable.

The HC-SR04 Ultrasonic sensor is versatile device that has become a staple in robotics
projects. This inexpensive component measures the distance between itself and the
nearest solid object using pulses of ultrasonic sound. It has reasonable accuracy “out of
the box” and can be made even more accurate with one additional component.

This project can be useful in to leveling the water tanks , alert the car if it bump while
reverse and it can also help disable person like the deaf and blind since the project is
consist of light and buzzer it can alert them in various way.

Language reference
analogWrite()
Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying
brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will
generate a steady square wave of the specified duty cycle until the next call
to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The
frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and
similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on
the Leonardo also run at 980 Hz.

Long
Long variables are extended size variables for number storage, and store 32 bits
(4 bytes), from -2,147,483,648 to 2,147,483,647.
#define
#define is a useful C++ component that allows the programmer to give a name
to a constant value before the program is compiled. Defined constants in arduino
don’t take up any program memory space on the chip. The compiler will replace
references to these constants with the defined value at compile time.
#include
#include is used to include outside libraries in your sketch. This gives the
programmer access to a large group of standard C libraries (groups of pre-made
functions), and also libraries written especially for Arduino.
Libraries
The Arduino environment can be extended through the use of libraries, just like most
programming platforms. Libraries provide extra functionality for use in sketches, e.g.
working with hardware or manipulating data. To use a library in a sketch, select it
from Sketch > Import Library.
A number of libraries come installed with the IDE, but you can also download or create
your own.

Pre set codes from the Libraries to be use:


NewPing :
NewPing label(TRIGGER PIN, ECHOPIN, MAX DISTANCE);to define the input and
output pin .also indicate the label of the new ping
ping_cm(): to measure distance by centimeter

LiquidCrystal_I2c:

lcd.init(); to run the LCD


lcd.backlight(); to turn on the backlight of the LCD
lcd.print(); to print in the LCD

delay()
Pauses the program for the amount of time (in miliseconds) specified as parameter.
(There are 1000 milliseconds in a second.)

Serial.begin()

Sets the data rate in bits per second (baud) for serial data transmission. For
communicating with Serial Monitor, make sure to use one of the baud rates listed
in the menu at the bottom right corner of its screen. You can, however, specify
other rates - for example, to communicate over pins 0 and 1 with a component
that requires a particular baud rate.

An optional second argument configures the data, parity, and stop bits. The
default is 8 data bits, no parity, one stop bit.

Serial.print()

Prints data to the serial port as human-readable ASCII text. This command can
take many forms. Numbers are printed using an ASCII character for each digit.
Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes
are sent as a single character.

If
The if statement checks for a condition and executes the proceeding statement
or set of statements if the condition is 'true'
else

The if…else allows greater control over the flow of code than the
basic if statement, by allowing multiple tests to be grouped. An else clause (if at
all exists) will be executed if the condition in the if statement results in false.
The else can proceed another if test, so that multiple, mutually exclusive tests can
be run at the same time.

COMPONENTS:
RGB
The RGB junction rectifier will emit completely different colors by compounding the three
basic colors red, inexperienced and blue. thus it really consists of three separate LEDs red,
inexperienced and blue packed in an exceedingly single case. That’s why it’s four leads,
one lead for every one of the three colors and one common cathode or anode relying
upon the RGB junction rectifier sort.

ULTRASONIC (HC-SR04)
It emits an ultrasound at 40 000 Hz which travels through the air and if there is an object
or obstacle on its path It will bounce back to the module. Considering the travel time
and the speed of the sound you can calculate the distance.
The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground
and the VCC pins of the module needs to be connected to the Ground and the 5 volts
pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin
on the Arduino Board.

In order to generate the ultrasound you need to set the Trig on a High State for 10 µs.
That will send out an 8 cycle sonic burst which will travel at the speed sound and it will
be received in the Echo pin. The Echo pin will output the time in microseconds the
sound wave traveled.

PIEZO BUZZER
The piezo buzzer produces sound based on reverse of the piezoelectric effect. The
generation of pressure variation or strain by the application of electric potential across
a piezoelectric material is the underlying principle. These buzzers can be used alert a
user of an event corresponding to a switching action, counter signal or sensor input.
They are also used in alarm circuits.

The buzzer produces a same noisy sound irrespective of the


voltage variation applied to it. It consists of piezo crystals
between two conductors. When a potential is applied across
these crystals, they push on one conductor and pull on the
other. This, push and pull action, results in a sound wave. Most
buzzers produce sound in the range of 2 to 4 kHz.
I2C LCD

This type of LCD is ideal for displaying text and numbers, hence the name
‘Character LCD’. The I2C LCDs that we are come with a small add-on circuit
mounted on the back of the module. This module features a PCF8574 chip (for
I2C communication) and a potentiometer to adjust the LED backlight. The
advantage of an I2C LCD is that the wiring is very simple. You only need two
data pins to control the LCD.

Standard LCDs typically require around 12 connections, which can be a


problem if you do not have many GPIO pins available.

CIRCUIT CONNECTION:
RGB

The cathode is going to be connected to the bottom and also the three nodes are
going to be connected through 250 Ohms resistors to three digital pins on the Arduino
Board PIN that may offer PWM signal.we’ll use PWM for simulating analog output which
can offer totally different voltage levels to the LEDs, therefore, we are able to get the
specified colors. Red pin ,green pin and blue pin will be connected to digital pin 9, 10
and 11 respectively
LCD
Power the LCD module to 5 volts and connect the ground as well. The SDA pin of the
i2c module conected to arduinio A5 and the SCL pin to A4(as indicated in the preset
library “LiquidCrystal_I2C.h”). In order to make the LCD work we need to inport the LCD
library for arduino.

BUZZER

The long lead is connected to the Input digital pin and the short lead is
connected Ground.

ULTRASONIC
Power the sensor by connecting vcc pin to 5v pin of Arduino and gndto the ground pin
as well. The trigger pin of the sensor conected to arduinio digital pin 12 and the echp
pin to digital pin 11. In order to make the ultrasonic sensor work we need to inport the
HCSR04 library for arduino.

OVERALL SCHEMATIC CIRCUIT:

How the Code Works

You start by including the HCSR04 library LCD I2C LIBRARY:

#include <NewPing.h>

#include <LiquidCrystal_I2C.h>
Then, define the trigger and echo pin. The trigger pin is connected to
the Arduino digital Pin 13, and the echo to Pin 12. You also need to
define the MAX_DISTANCE variable to be able to use the library.

#define TRIGGER_PIN 13

#define ECHO_PIN 12

#define MAX_DISTANCE 200

Create NewPing instance called distanceSensor. Also create


LiquidCrystal_I2C instance called lcd. Then define the address and type
of the LCD.

LiquidCrystal_I2C lcd(0x27, 16, 2);


NewPing distanceSensor(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

We then define the pins of the RGB and Buzzer.

int redPin = 9;

int greenPin = 10;

int bluePin = 11;

int buzzerpin = 6;

We also the create three variable type of long: red, green and distance. The
red variable saves the range brightness of red color in RGB. The green
variable saves the range of brightness of green color in RGB. The distance
variable saves the distance in centimeter.

long red, green, distance;

In the setup(), initialize the serial port at a baud rate of 9600, and set
the RGB’s pin as output. We also set t on the LCD back light and initiate
using backlight() and init().

Serial.begin (9600);
lcd.init();

lcd.backlight();

pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);

in the loop(), to get the distance you just need to use


the ping_cm() method on the sonar object. This will give you the
distance in centimeters. We put delay 500 millisecond before printing
the measure distance in the LCD. The lcd clear the pixel first using
clear() then print the word distance and the measure distance in the
LCD using print()We add a Serial.print to also print the measure
distance in serial monitor.

distance =distanceSensor.ping_cm();

delay(250);

lcd.clear();

lcd.print("distance :");

lcd.setCursor(0,1);

lcd.print(distance);

lcd.print(" cm");

Serial.println("distance :");

Serial.println(distance + " cm");


Now that we have the current distance we can manipulate the color
of the RGB ,when the distance is less 100 cm the green variable start to
decrease and the red variable start to increase. Since the maximum
value to fully brighten up the leds is 255 we simply divide it by the
distance where the transition of light start to occur. The red led will start
to brighten up when the distance is less than 100 cm and the green
led start to brigten up when the distance is greater that 50 cm. while
the blue led will have a constant brightness.

If (distance <= 50){

red = 255-distance*(255/100);

green=0;

} else if(distance >= 100)

red = 0;

green = (.5*distance);

} else

green = (.25*distance);

red = 255-distance*2.55;

analogWrite(redPin, red);
analogWrite(greenPin, green);

analogWrite(bluePin, 50);

last for the buzzer , since the will start as the red light brighten up then
we can use the value of the variable red for the loudness of the buzzer .
the beat of the buzzer also varies 5 times the current distance using the
delay function and it will have a steady noise when the distance is less
than 10 cm.

if (distance <= 100){

analogWrite(buzzerpin,red);

if(distance > 10){

delay(5*distance);

analogWrite(buzzerpin,0);

}else{

analogWrite(buzzerpin,0);

}
ARDUINO CODE
#include <NewPing.h>

#include <LiquidCrystal_I2C.h>

#define TRIGGER_PIN 13

#define ECHO_PIN 12

#define MAX_DISTANCE 200

LiquidCrystal_I2C lcd(0x27, 16, 2);

NewPing distanceSensor(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

int redPin = 9;

int greenPin = 10;

int bluePin = 11;

int buzzerpin = 6;

long red, green, distance;

void setup()

Serial.begin (9600);

lcd.init();

lcd.backlight();

pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);
}

void loop()

distance =distanceSensor.ping_cm();

delay(250);

lcd.clear();

lcd.print("distance :");

lcd.setCursor(0,1);

lcd.print(distance);

lcd.print(" cm");

Serial.println("distance :");

Serial.println(distance + " cm");

If (distance <= 50){

red = 255-distance*(255/100);

green=0;

} else if(distance >= 100)

red = 0;

green = (.5*distance);

} else

green = (.25*distance);
red = 255-distance*2.55;

analogWrite(redPin, red);

analogWrite(greenPin, green);

analogWrite(bluePin, 50);

if (distance <= 100){

analogWrite(buzzerpin,red);

if(distance > 10){

delay(5*distance);

analogWrite(buzzerpin,0);

}else{

analogWrite(buzzerpin,0);

Das könnte Ihnen auch gefallen