The first step in connecting an IR sensor to your Galileo is to obtain a sensor. One popular choice is the Sharp series of IR sensors. The following image shows one of the models, the Sharp 2Y0A02, a unit that provides sensing to a distance of 150 cm:
To connect this unit, you'll connect the three pins that are available on the bottom of the sensor. Here is the connection list:
Galileo Pin |
Sensor Pin |
---|---|
5V |
Vcc |
GND |
Gnd |
A3 |
Vo |
Unfortunately, there are no labels on the unit, but here are the pins you'll connect:
One of the challenges of making this connection is that the female-to-male connection jumpers are too big to connect directly to the sensor. You'll want to order a three-wire cable with connectors with the sensor. Many versions come with this cable. You can make the connections between this cable and the Galileo using the male-to-male jumper wires. Here is the diagram:
Once the pins are connected, you are ready to access the sensor via the IDE.
Now bring up the IDE. The following that provides access to the sensor and returns via the serial link the distance to the object:
This is quite simple. The three global variables at the top set the input pin to 3, and provide a storage location for the input value and distance. The setup()
function simply sets the serial port baud rate to 9600 and prints out single line to the serial port. The Serial Monitor communicates with the Galileo via a serial port, and the baud rate specifies the communication rate between the Galileo and the host computer.
In the loop()
function, you first get the value from the A3 input pin. The next step is to convert it to a distance based on the voltage.
If you open the Serial Monitor window and place an object in front of the sensor, you'll see the readings for the distance to the object, like this:
By the way, when you place the object closer than 15 cm, you should begin to see distances that seem much larger than indicated. This is due to the voltage-to-distance curve at these much shorter distances. If you truly need very short distances, you'll want a different sensor.
3.17.75.43