Hi folks!!!

In the last blog post we have learnt about SPI communication, how to take analog readings with Raspberry Pi with MCP3008 ADC and we have derived a formula to convert the voltage readings to distance values from the Sharp IR distance sensor. Please refer to the previous blog post in order to get a clear understanding on what is explained in this post.

Just connect the distance sensor and ADC as directed in the previous blog apart from connecting Ethernet cable between raspi and computer, and powering it, before trying to burn the code to raspi through matlab.


Matlab Program for reading ADC and getting the distance:


Let us first see how to access the analog to digital converted values sent by MCP3008.......

clear rpi;

rpi = raspi();

clear mcp3008;

mcp3008 = raspi.internal.mcp3008(rpi, 'CE0');


The variable rpi creates a raspi object which can communicate with the given raspberry pi hardware connected to the computer.  As we have connected the Channel 0 pin of ADC to the SPI channel CE0, we have to command raspi to listen to CE0.

voltage = readVoltage(mcp3008, 0);

fprintf('Voltage = %0.2f;\t', voltage);

dig = voltage*(1023/5);

fprintf('Digital value = %0.2f\n', dig);


fprintf('distance in cm is %0.2f\n',distance);

The readVoltage function lets us store the voltage read by raspi, here we are reading voltage in pin 0 of the two SPI pins 0 and 1.  We print the voltage values and convert it to digital values i.e. converting 0-5 V to 0- 1023 scale. Then, we apply the formula, we derived to get the distance and print it.


This program only taken 1 value of distance from sensor and it could be erroneous as the first reading is not always a stable one. So, we have modified the program to make it more accurate and usable.

for i = 1:50

    voltage = readVoltage(mcp3008, 0);

    fprintf('Voltage = %0.2f;\t', voltage);

    dig = voltage*(1023/5);


    fprintf('Digital value = %0.2f\n', dig);





fprintf('distance in cm is %0.2f\n', avg);


This program takes 50 values of distance consecutively and averages all of them at the end to give a value agreeable with the actual readings. Although one or two trash readings come up, it does not affect much on the outcome as the outcome may maximum be out of the tune by 0.01-0.1 cm which is negligible.


Arduino code for the same:

void setup() {



int sensorValue;

float distance;

void loop() {

sensorValue = analogRead(0);

if(sensorValue>=280&&sensorValue<=512)  //Corresponding distance range from 1m to 5.5m


   Serial.print("The distance is : ");







The above code just reads the analog output pin of the distance sensor connected to Analog 0 pin of Arduino. It reads A0 pin and prints the distance value on the Serial monitor continuously if the condition of the range of 1m -5.5m  is satisfied.

The arduino code of averaging a certain number of distance readings is self-explanatory and simple, so I leave it for you intelligent people out there.


The above pic shows the working model of the distance sensor being monitored by raspi and we can see that the IR transmitter being lit up which distinguishes it from the receiver module beside it.