You are on page 1of 11
Bruriah / CIJE / Dr. Regev Mini Project 2 December 2013 Dr. Regev 1
Bruriah / CIJE / Dr. Regev
Mini Project 2
December 2013
Dr. Regev
1
Bruriah / CIJE / Dr. Regev LCD YwRobot This unit connects with 4 wires including Vcc*
Bruriah / CIJE / Dr. Regev
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.
2
Bruriah / CIJE / Dr. Regev Examples: /* YourDuino.com Example Software Sketch 16 character 2 line
Bruriah / CIJE / Dr. Regev
Examples:
/* YourDuino.com Example Software Sketch
16 character 2 line I2C Display
Backpack Interface labelled "YwRobot Arduino
LCM1602 IIC V1"
terry@yourduino.com */
//-------- 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);
/*-----( Import needed libraries )-----*/
lcd.print("HI!YourDuino.com");
#include <Wire.h> // Comes with Arduino IDE
// Get the LCD I2C Library here:
delay(8000);
// 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>
// 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);
/*-----( Declare Constants )-----*/
lcd.print("Type to display");
/*-----( 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:
}/*--(end setup )---*/
//
addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3,
void loop()
/*----( LOOP: RUNS CONSTANTLY )----*/
POSITIVE); // Set the LCD I2C address
{
{
/*-----( Declare Variables )-----*/
//NONE
// when characters arrive over the serial port ...
if (Serial.available()) {
// wait a bit for the entire message to arrive
void setup()
/*----( SETUP: RUNS ONCE )----*/
delay(100);
{
Serial.begin(9600); // Used to type in characters
lcd.begin(16,2);
// initialize the lcd for 16 chars 2 lines,
turn on backlight
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
// ------- Quick 3 blinks of backlight -------------
for(int i = 0; i< 3; i++)
{
}
}
}
lcd.backlight();
delay(250);
}/* --(end main loop )-- */
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
/* ( THE END ) */
3
Bruriah / CIJE / Dr. Regev //LCD_Stop_Drive_Text //for displays with backpack interface labelled "YwRobot Arduino LCM1602
Bruriah / CIJE / Dr. Regev
//LCD_Stop_Drive_Text
//for displays with backpack interface labelled "YwRobot Arduino
LCM1602 IIC V1")
// need LiquidCrystal_I2C library
/*-----( Declare Variables )----- */
//NONE
void setup()
/*----( SETUP : RUNS ONCE )---- */
#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
{
Serial.begin(9600); // Used to type in characters
lcd.begin(16,2);
// initialize the lcd for 16 chars 2 lines, turn on
backlight
void setup()
{
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Welcome to ");
// ------- Quick 3 blinks of backlight -------------
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
lcd.setCursor(0, 1);
lcd.print("T Island");
}
lcd.backlight(); // finish with backlight on
}
void loop()
{
delay(1000);
lcd.clear();
int x = 3;
while (x > 1)
{
//-------- 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");
lcd.setCursor(0, 0);
delay(8000);
lcd.print("Stop!");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Drive");
// 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");
delay(2000);
lcd.setCursor(0,1);
lcd.clear();
}
} // ************End Loop / The End************
lcd.print("Type to display");
}/*--(end setup )--- */
/* YourDuino.com Example Software Sketch
16 character 2 line I2C Display
Backpack Interface labelled "YwRobot Arduino LCM1602 IIC V1"
terry@yourduino.com */
void loop()
/*----( LOOP:
RUNS CONSTANTLY )---- */
{
{
/*-----( Import needed libraries )----- */
#include <Wire.h> // Comes with Arduino IDE
// Get the LCD I2C Library here:
// when characters arrive over the serial port ...
if (Serial.available()) {
// wait a bit for the entire message to arrive
// 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>
delay(100);
/*-----( Declare Constants )----- */
/*-----( Declare objects )----- */
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
// 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
}/* --(end main loop )-- */
/* ( THE END ) */
4
Bruriah / CIJE / Dr. Regev //for displays with backpack interface labelled "YwRobot Arduino LCM1602 IIC
Bruriah / CIJE / Dr. Regev
//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
// 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()
/*----( SETUP: RUNS ONCE )----*/
{
void setup()
{
Serial.begin(9600); // Used to type in characters
lcd.begin(16,2);
lcd.begin(16,2);
// initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight();
lcd.setCursor(0, 0);
//First column, first row
// ------- Quick 3 blinks of backlight -------------
for(int i = 0; i< 3; i++)
{
lcd.print("Hello world!");
lcd.setCursor(0, 1);
//First column, second row
lcd.backlight();
lcd.print("Row number: ");
delay(250);
lcd.setCursor(12, 1);
//12th column, second row
lcd.noBacklight();
delay(250);
lcd.print("2");
}
lcd.backlight(); // finish with backlight on
lcd.blink();
delay (3000);
//start blinking cursor
//delay 3 seconds
//-------- 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!");
}
void loop()
{
delay(1000);
demoDisplay();
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");
}
void demoDisplay()
{
lcd.clear();
zero
// clear display, set cursor position to
lcd.setBacklight(HIGH);
lcd.setCursor (0,0);
// Backlight on
//col 0 of line 1
lcd.setCursor(0,1);
lcd.print("Boot sequence");
lcd.print("Type to display");
lcd.setCursor (0,1);
lcd.print("starting.");
lcd.noBlink();
// go col 0 of line 2
}/*--(end setup )---*/
delay(1000);
void loop()
/*----( LOOP: RUNS CONSTANTLY )----*/
wipeLines();
//stop blinking cursor
//delay 1 second
//run function wipeLines
{
}
{
// 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());
void wipeLines() {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 20; x++) {
lcd.setCursor (x,y);
lcd.print(" ");
delay(10);
}
}
}
}
}
}
}/* --(end main loop )-- */
5
Bruriah / CIJE / Dr. Regev Ping Ultrasonic Range Finder The Ping))) is an ultrasonic range
Bruriah / CIJE / Dr. Regev
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 be cause 40 kHz is ultrasonic.
What we consider sound is our inner ear's ability to detect the variations in air pressure caused b y
vibration. The rate of these variations determines the pitch of the tone. Higher frequenc y tones result in
higher pitch sounds and lower frequenc y tones result in lo we r 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 freque ncies below 20 Hz , and ultrasonic is sound with frequencies above
20 kHz. Since the Ping))) sensor's chirps are at 40 kHz, the y are definitely ultrasonic, and not audible.
6
Bruriah / CIJE / Dr. Regev Hardware Required • • • Arduino Board (1) Ping Ultrasonic
Bruriah / CIJE / Dr. Regev
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:
7
Bruriah / CIJE / Dr. Regev Code /* Ping))) Sensor This sketch reads a PING))) ultrasonic
Bruriah / CIJE / Dr. Regev
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);
8
Bruriah / CIJE / Dr. Regev Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm" ); Serial.println(); delay(100); } long
Bruriah / CIJE / Dr. Regev
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, Just 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:
9

Serial.begin(9600);

}

void loop()

{

Bruriah / CIJE / Dr. Regev

// 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

10

Bruriah / CIJE / Dr. Regev Mini Project 2 The End 11
Bruriah / CIJE / Dr. Regev
Mini Project 2
The End
11