Hi folks!!!
In the last blog post, I've provided directions on how to get started with the Raspberry pi, setting it up and a brief introduction and characteristics of the Sharp IR distance sensor GP2Y0A710K. If you are not sure on setting up raspberry pi, please refer to my previous post before proceeding further or feel free to ask for clarifying doubts, if any.
Well, in this blog post we will be concentrating on how to take analog readings from the Sharp IR distance sensor, which has an analog output and manipulate the readings to get the approximate value of distance from the obstacle or object. The Raspberry Pi is not equipped with provision to take analog readings directly with GPIO pins, so we use an Analog-to-Digital converter, MCP3008, a 10-bit 8- channel ADC and connect to the SPI peripheral on Raspberry Pi hardware. The understanding of SPI communiaction, the details of the pin-out of MCP3008 and the integration of the ADC with raspberry pi is given in this tutorial. The gist of that tutorial is mentioned below:
SPI COMMUNICATION:
The Serial Peripheral Interface (SPI) is a synchronous serial data link that you use to interface one or more slave peripheral devices to a single master SPI device. SPI uses three signal lines common to all slave peripherals:
- Master in slave out (MISO). Data is moved from slave to master on this wire.
- Master out slave in (MOSI). Data is moved from master to slave on this wire.
- Serial clock (SCLK). The master SPI device generates this clock.
Each slave peripheral is wired to an individual chip-select or slave-transmit enable line. During SPI communication, the master selects only one slave device at a time. Any other slave on the bus that has not been activated using its chip-select line must disregard the input clock and MOSI signals. It also must not drive the MISO line.
You can determine if the SPI is enabled on your Raspberry Pi hardware by inspecting the AvailableSPIChannels property of the raspi object.
clear rpi
rpi = raspi();
rpi.AvailableSPIChannels
If the SPI peripheral is not enabled, you will see an empty cell array. To enable the SPI peripheral execute the following command.
enableSPI(rpi);
After you execute the preceding command, inspecting the AvailableSPIChannels property of the rpi object should yield 'CE0' and 'CE1' SPI channels listed as available.
OVERVIEW of ADC:
MCP3008 is a 10-bit, 8-channel analog to digital converter (ADC) with an SPI interface.
As seen in this diagram, pins 1 through 8 are analog input channels. Pin 16 is the digital supply voltage and pin 9 is the digital ground. Vref and Agnd are the pins used for reference voltage level for analog measurements. The digital 10-bit voltage measurement value is scaled so that a value of 0 corresponds to Agnd and a value of 1023 (2^10 - 1) corresponding to Vref. Pins 10 through 13 are connections for SPI interface.
Connect MCP3008 to the Raspberry Pi hardware as seen in the following circuit diagram.
Connect the analog output pin of distance sensor to the CH0 pin of the ADC instead of the variable resistor as shown in the above diagram. And connect the 4 SPI connections between Raspberry Pi and ADC as shown. Finally connect the power supply connections of 3.3 V and gnd accordingly.
Overview of Distance sensor connection:
The sharp IR distance sensor GP2Y0A710K, has a good detection range from 100 cm to 550 cm with a decreasing exponential response with increasing distance. There are 5 pins on the sensor to be connected. These are 2 x supply voltage,2 x gnd and an output analog pin. The pin configuration is shown below.
The pin 4 of distance sensor is connected to the pin 1 Channel 0 of ADC MCP3008.
Understanding calculation of Distance measurement:
The distance sensor GP2Y0A710K, has a detection range from 100 cm to 550 cm and considering the analog output values of the sensor at the extreme distance values of 100 cm and 550 cm, we have,
| Distance (m) | Analog Voltage (v) | Digital value (0 to 2^10-1) |
|---|---|---|
| 1 | 2.5 | 512 |
| 5.5 | 1.4 | 286 |
and we can thus put down an equation for approximating the analog output into distance.
We have a linear equation of digital value and distance.
(512-digValue)/(1/100-1/distance)=(512-286)/(0.01-0.002)
=> distance=28250/(digValue-229.5);
Here,
digvalue= voltage * (1023/5)>>> It indicates converting 0-5 V to 0-1023 i.e. a 10 bit resolution.
Hence, the appropriate formula is given by,
Distance=28250÷(DigValue−229.5) centimeters.
But, you might be wondering about the contradiction that there is an another formula mentioned in the previous post for distance calculation. Yes, that is also a valid formula but, it is calculated by considering entire range of distances irrespective of the working range of 1-5.5 m. Hence, you might find that the one mentioned in the previous post is a bit erroneous compared to the present one.
Let us see how to program the Arduino and the Raspberry Pi to get the distance value from the sensor in the next blog.






Top Comments