Hardware Reference
In-Depth Information
gndPin are the power pins, and trigPin and echoPin are the data pins. Later,
trigPin will be an output, and echoPin will be an input.
On line 9, the LCD display is coni gured, creating an lcd device. This function
uses six parameters, which tell the sketch that it will use four data lines and
does not use the optional read/write parameter. It is called using six integers:
8, 9, 4, 5, 6, and 7. The i rst value corresponds to the RS pin. On the SainSmart
LCD Keypad shield, RS is pin 8. The second value is the enable pin, and this is
wired to pin 9. Finally, 4, 5, 6, and 7 are the data pins. As with the rs and enable
pins, these are hardwired on the shield.
On line 11, setup() is declared. On line 13, the serial port is initialized. It isn't
used in this example, but it is ready in case you need to start debugging your
application. The LCD device is already activated, but the sketch knows only what
pins the LCD device is connected to. It still doesn't know how many lines and
columns the device has. This is done on line 16 with begin() ; it has 16 columns
and two lines. On lines 19 to 24, the four pins for the sensor are coni gured. One
pin, echoPin , will be coni gured as INPUT , and the three others will be OUTPUT .
On line 25, the trigger pin is set LOW . On line 28, the vccPin is set HIGH ; it will
now supply 5 V. On line 29, the gndPin is set LOW ; it is now a ground connection.
Finally, on line 32, some text is sent to the LCD device: one word—“Distance.”
This is printed at the default cursor position: (0,0), located at the top-left corner
of the screen. This text will be present at all times, and the text on the second
line will be updated in loop() .
On line 35, loop() is declared. This is where all the sensor reading and text
writing takes place. It starts by declaring two variables: duration and distance .
The HC-SR04 requires a pulse on the trigPin pin of at least 10 microseconds.
To do this, the sketch i rst sets trigPin HIGH , waits for 10 microseconds using
delayMicroseconds() , and then sets trigPin to a logical LOW .
After receiving a pulse, the HC-SR04 starts working. It emits a number of
ultrasonic bursts and listens to the results. After the distance has been calcu-
lated, the result is returned via the pulsePin , a variable length pulse. So how
can the Arduino know how long the pulse is? The answer is simple: pulseIn() .
This function was presented in Chapter 4. Put simply, it waits for a pulse to
appear on the designated pin. It waits for the logic level to change and then
starts counting. When the logic level changes back to its original setting, it stops
counting and returns the length of the pulse in microseconds. This is done on
line 53, placing the result into a variable: duration . On line 54, a small calcula-
tion is made; the variable duration is divided by 58. This value comes from the
sensor's documentation. Divide the number by 58 to get a result in centimeters
and by 148 to get the result in inches. Now that you have the distance, it is time
to print the results.
The results will be printed on the second line of the LCD screen, so the
coordinates must be set. This is done on line 47; the position is set to column 0,
Search WWH ::




Custom Search