You are on page 1of 11

Bruriah / CIJE / Dr.

Regev
1

























Mini Project 2
December 2013

Dr. Regev





Bruriah / CIJE / Dr. Regev
2

LCD YwRobot

This unit connects with 4 wires including Vcc* and Gnd. It is easiest with a 4-wire cable (supplied) that plugs
into the communications connector.

But you can wire it directly yourself if needed: There are 4 pins on the display... (See photo below)








Top to bottom:

GND - GND
VCC - 5V
SDA - ANALOG Pin 4
SCL - ANALOG pin 5

On most Arduino boards, SDA (data line) is on analog input pin 4, and SCL (clock line) is on analog input pin
5. On the Arduino Mega, SDA is digital pin 20 and SCL is 21.
http://arduino-info.wikispaces.com/LCD-Blue-I2C



----------------------------------------------------------------
*VCC is an acronym for Voltage at a Common Connector. This is the positive power that supplies the object.
*VCC, VEE, VBB are grounded voltages of a collector, emitter, and base, respectively, on a transistor. The
reason this letter is doubled is to represent a voltage connected.
Bruriah / CIJE / Dr. Regev
3

Examples:

/* YourDuino.com Example Software Sketch
16 character 2 line I2C Display
Backpack Interface labelled "YwRobot Arduino
LCM1602 IIC V1"
terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <Wire.h> // Comes with Arduino IDE
// Get the LCD I2C Library here:
// https://bitbucket.org/fmalpartida/new-
liquidcrystal/downloads
// Move any other LCD libraries to another folder or
delete them
// See Library "Docs" folder for possible commands
etc.
#include <LiquidCrystal_I2C.h>

/*-----( Declare Constants )-----*/
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line
display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3,
POSITIVE); // Set the LCD I2C address

/*-----( Declare Variables )-----*/
//NONE

void setup() /*----( SETUP: RUNS ONCE )----*/
{
Serial.begin(9600); // Used to type in characters

lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines,
turn on backlight

// ------- Quick 3 blinks of backlight -------------
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on


//-------- Write characters on the display ------------------
// need LiquidCrystal_I2C library
// NOTE: Cursor Position: (CHAR, LINE) start at 0
lcd.setCursor(0,0); //Start at character 4 on line 0
lcd.print("Hello, world!");
delay(1000);
lcd.setCursor(0,1);
lcd.print("HI!YourDuino.com");
delay(8000);

// Wait and then tell user they can start the Serial
Monitor and type in characters to
// Display. (Set Serial Monitor option to "No Line
Ending")
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Use Serial Mon");
lcd.setCursor(0,1);
lcd.print("Type to display");


}/*--(end setup )---*/


void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
{
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}
}

}/* --(end main loop )-- */


/* ( THE END ) */



Bruriah / CIJE / Dr. Regev
4

//LCD_Stop_Drive_Text
//for displays with backpack interface labelled "YwRobot Arduino
LCM1602 IIC V1")
// need LiquidCrystal_I2C library

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En,
Rw, Rs, d4, d5, d6, d7, backlighpin, polarity

void setup()
{
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Welcome to ");

lcd.setCursor(0, 1);
lcd.print("T Island");
}
void loop()
{
delay(1000);
lcd.clear();
int x =3;
while (x >1)
{
lcd.setCursor(0, 0);

lcd.print("Stop!");

delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Drive");
delay(2000);
lcd.clear();
}
} //************End Loop / The End************

/* YourDuino.comExample Software Sketch
16 character 2 line I2C Display
Backpack Interface labelled "YwRobot Arduino LCM1602 IIC V1"
terry@yourduino.com*/

/*-----( Import needed libraries )-----*/
#include <Wire.h> // Comes with Arduino IDE
// Get the LCD I2C Library here:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>

/*-----( Declare Constants )-----*/
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the
LCD I2C address



/*-----( Declare Variables )-----*/
//NONE

void setup() /*----( SETUP: RUNS ONCE )----*/
{
Serial.begin(9600); // Used to type in characters

lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on
backlight

// ------- Quick 3 blinks of backlight -------------
for(int i =0; i<3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on

//-------- Write characters on the display ------------------
// NOTE: Cursor Position: (CHAR, LINE) start at 0
lcd.setCursor(0,0); //Start at character 4 on line 0
lcd.print("Hello, world!");
delay(1000);
lcd.setCursor(0,1);
lcd.print("HI!YourDuino.com");
delay(8000);

// Wait and then tell user they can start the Serial Monitor and
type in characters to
// Display. (Set Serial Monitor option to "No Line Ending")
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Use Serial Mon");
lcd.setCursor(0,1);
lcd.print("Type to display");


}/*--(end setup )---*/


void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
{
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() >0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}
}

}/* --(end main loop )-- */


/* ( THE END ) */

Bruriah / CIJE / Dr. Regev
5

//for displays with backpack interface labelled "YwRobot Arduino LCM1602 IIC
V1")
// need LiquidCrystal_I2C library

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En, Rw, Rs, d4,
d5, d6, d7, backlighpin, polarity

void setup() /*----( SETUP: RUNS ONCE )----*/
{
Serial.begin(9600); // Used to type in characters

lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight

// ------- Quick 3 blinks of backlight -------------
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on

//-------- Write characters on the display ------------------
// NOTE: Cursor Position: (CHAR, LINE) start at 0
lcd.setCursor(0,0); //Start at character 4 on line 0
lcd.print("Hello, world!");
delay(1000);
lcd.setCursor(0,1);
lcd.print("HI!YourDuino.com");
delay(8000);

// Wait and then tell user they can start the Serial Monitor and type in characters
to
// Display. (Set Serial Monitor option to "No Line Ending")
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Use Serial Win");
lcd.setCursor(0,1);
lcd.print("Type to display");


}/*--(end setup )---*/


void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
{
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}
}

}/* --(end main loop )-- */
// LCD Cursor Manipulation
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity

void setup()
{
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(0, 0); //First column, first row
lcd.print("Hello world!");
lcd.setCursor(0, 1); //First column, second row
lcd.print("Row number: ");
lcd.setCursor(12, 1); //12th column, second row
lcd.print("2");
lcd.blink(); //start blinking cursor
delay (3000); //delay 3 seconds
}
void loop()
{
demoDisplay();
}
void demoDisplay()
{
lcd.clear(); // clear display, set cursor position to
zero
lcd.setBacklight(HIGH); // Backlight on
lcd.setCursor (0,0); //col 0 of line 1
lcd.print("Boot sequence");
lcd.setCursor (0,1); // go col 0 of line 2
lcd.print("starting.");
lcd.noBlink(); //stop blinking cursor
delay(1000); //delay 1 second
wipeLines(); //run function wipeLines
}

void wipeLines() {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 20; x++) {
lcd.setCursor (x,y);
lcd.print(" ");
delay(10);
}
}
}


Bruriah / CIJE / Dr. Regev
6

Ping Ultrasonic Range Finder
The Ping))) is an ultrasonic range finder from Parallax. It detects the distance of the closest object in front of the sensor (from 2 cm up
to 3m). It works by sending out a burst of ultrasound and listening for the echo when it bounces off of an object. The Arduino board
sends a short pulse to trigger the detection, then listens for a pulse on the same pin using the pulseIn() function. The duration of this
second pulse is equal to the time taken by the ultrasound to travel to the object and back to the sensor. Using the speed of sound, this
time can be converted to distance.

HOW DOES THE PING))) SENSOR WORK?
Figure 2 shows how the Ping))) sensor sends a brief chirp with its ultrasonic speaker and makes it possible for the BASIC Stamp to
measure the time it takes the echo to return to its ultrasonic microphone. The BASIC Stamp starts by sending the Ping))) sensor a
pulse to start the measurement. Then, the Ping))) sensor waits long enough for the BASIC Stamp program to start a PULSIN
command. At the same time the Ping))) sensor chirps
its 40 kHz tone, it sends a high signal to the BASIC Stamp. When the Ping))) sensor detects the echo with its ultrasonic microphone, it
changes that high signal back to low.

The BASIC Stamp's PULSIN command stores how long the high signal from the Ping))) sensor lasted in a variable. The time
measurement is how long it took sound to travel to
the object and back. With this measurement, you can then use the speed of sound in air to make your program calculate the object's
distance in centimeters, inches, feet, etc...
____________________________________________________________________________________________
The draft material in this Chapter is part of forthcoming Stamps in Class text by Andy Lindsay.
(c) 2005 by Parallax Inc - all rights reserved.

Figure 2 - How the Ping))) Sensor Works



The Ping))) sensor's chirps are not audible because 40 kHz is ultrasonic.

What we consider sound is our inner ear's ability to detect the variations in air pressure caused by
vibration. The rate of these variations determines the pitch of the tone. Higher frequency tones result in
higher pitch sounds and lower frequency tones result in lower pitch tones.

Most people can hear tones that range from 20 Hz, which is very low pitch, to 20 kHz, which is very high
pitch. Subsonic is sound with frequencies below 20 Hz, and ultrasonic is sound with frequencies above
20 kHz. Since the Ping))) sensor's chirps are at 40 kHz, they are definitely ultrasonic, and not audible.
Bruriah / CIJE / Dr. Regev
7


Hardware Required
Arduino Board
(1) Ping Ultrasonic Range Finder
hook-up wire
Circuit: The 5V pin of the PING))) is connected to the 5V pin on the Arduino, the GND pin is connected to the GND pin, and the SIG
(signal) pin is connected to digital pin 7 on the Arduino. Image developed using Fritzing. For more circuit examples, see the Fritzing project page





















Schematic:

Bruriah / CIJE / Dr. Regev
8

Code
/* Ping))) Sensor

This sketch reads a PING))) ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.

The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 7

http://www.arduino.cc/en/Tutorial/Ping

created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

*/

// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin =7;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration =pulseIn(pingPin, HIGH);

// convert the time into a distance
inches =microsecondsToInches(duration);
cm =microsecondsToCentimeters(duration);
Bruriah / CIJE / Dr. Regev
9


Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Now, J ust the Code:
/* Ping))) Sensor

This sketch reads a PING))) ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.

The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 7

http://www.arduino.cc/en/Tutorial/Ping

created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

*/

// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin =7;

void setup() {
// initialize serial communication:
Bruriah / CIJE / Dr. Regev
10

Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration =pulseIn(pingPin, HIGH);

// convert the time into a distance
inches =microsecondsToInches(duration);
cm =microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
} //End Loop





Bruriah / CIJE / Dr. Regev
11

























Mini Project 2
The End

You might also like