Hardware Reference
In-Depth Information
the extended digital outputs of the Arduino Mega2560. To create a self-contained
device, the sensor will be placed directly into the header pins, bypassing the
need for a breadboard. Let me explain.
By reading the datasheet of the HC-SR04, available at http://packetfury
.net/attachments/HCSR04b.pdf , you can i nd the requirements for power-
ing the sensor: one pin for power and one ground connection. The maximum
current used by the sensor is 15 mA. The maximum power delivered by the
Arduino's I/O pins can't exceed 40 mA. That is more than double, a comfort-
able safety margin. The pin connected to the sensor's VCC sets as output and
sets HIGH . The pin connected to the sensor's ground also is an output but sets
LOW . Just as LED lights can be powered by an I/O pin pulled HIGH , the sensor
will be powered by these pins. Similarly, the ground can be an I/O pin pulled
LOW . The sensor will be sufi ciently powered by the board, but remember that
this is a prototype and designed for simplicity. It is possible to do what you are
about to do, but if you end up creating your own shield with an LCD screen
and ultrasonic distance sensor built in, it is good practice to route the shield so
that the sensor is powered by the main power, not powered by the Arduino.
WARNING Don't connect the sensor just yet! The reason for this is explained
later in this chapter when I talk about the sketch.
Software
The sketch is shown in Listing 11-1.
Listing 11-1: Sketch (fi lename: Chapter 11.ino )
1 #include <LiquidCrystal.h>
2
3 const int vccPin=40;
4 const int gndPin=34;
5 const int trigPin=38;
6 const int echoPin=36;
7
8 // Initialize the library with the numbers of the interface pins
9 LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
10
11 void setup()
12 {
13 Serial.begin (9600);
14
15 // Set up the LCD's number of columns and rows
16 lcd.begin(16, 2);
17
18 // Configure the pins
19 pinMode(trigPin, OUTPUT);
Continues
 
Search WWH ::




Custom Search