You are on page 1of 6

Analog input by MCP3008 Many of my young friends are sad when it comes to tie up analog inputs to Raspi for

completing their great and stunning robotic projects. Most of them even shifts to Arduino based projects rather than Raspi based projects only because of this. Apart from that they also complain about the lack of sufficient GPI s which the Arduino mega board or the other kinds of development boards offer of the shelf. I feel hurt when people compare Raspi and Arduino on the same term. Arduino is a micro processor whereas Raspi is a full blown computer. Arduino is a faithful slave whereas Raspi can do much better besides controlling a number of slaves on it!s GPI s" I#$ or %PI buses. Also the difference between having a few more GPI s or a handful of analog inputs is a mere lack of knowledges. &ust by adding an M$P#'()* one can increase the number of GPI s by )+ and the cost for this is not even Rs,#(( -element)..com/ in the same way by adding an M$P'((0 cost of which is about Rs,#)( -element)..com/ one can add 0 analog inputs of )( bit resolution. 1or connecting additional micro processors Raspi has two kinds of buses 2 I#$ and %PI -%erial Peripheral Interfaces/. I#$ is just two wires 2 %3A 4 %$5 -Physical pin 2 ' 4 6/ whereas %PI is a combination of . wires 2 M %I" MI% "%$57 4 $8 -Physical pin 2 )9" #)" #' 4 #./. Apart from that "these pins can also act as normal GPI s. :he speed of %PI bus is definitely more than I#$ bus and both works ;uite satisfactorily. 8arlier I!ve shown how to connect M$P #'()* or M$P#'(0 to the I#$ bus. Also by changing the several combinations of address buses of M$P#'()* or M$P#'(0 -A("A)"A#/ we can add several of these micro processors to the Raspi thereby increasing the number of GPI s by several folds. <ow I shall show how to connect M$P'((0" the )( bit 0 channel A3$. =efore connecting these devices first we have to make Raspi enable by commenting out the blacklisted lines in the >etc>modprobe.d>raspi?blacklist.conf file. @sudo nano >etc>modprobe.d>raspi?blacklist.conf Put a !A! in front of the i#c?bcm#*(0 4 i#c?bcm#*(0 line. %ave the nano editor and reboot raspi. :o ensure that the i#c 4 bspi has been enabled "do an lsmod and see that the output contains spiBbcm#*(0 4 i#cBbcm#*(0. %o far so good.

&ust to recap here!s how we connected the M$P#'()* to enhance the GPI s by )+ -A(..A* 4 =(...=*/ <ow lets install the softwares re;uired for the spi to work. @sudo apt?get update >> I always recommend an update before any major works C @sudo apt?get install python?dev >> I hope python is already there in Raspi 2 after all the word !pi! comes from Python only. @mkdir spi >> make a clean directory for this great work @cd spi >> land into that directory and download the following files @wget https,>>raw.github.com>doceme>py?spidev>master>setup.py @wget https,>>raw.github.com>doceme>py?spidev>master>spidevBmodule.c >> <ow install it by the following command @sudo python setup.py install :his can also be done using github but now?a?days I find github runs very slow therefore" I recommend you download those files and then run installation.

Dere!s the pin up diagram of M$P'((0 A3$" on the right shown

which pin to go where on Raspi. $onnect the above pins with the M$P'((0 as per the diagram and get ready for analog measurements with the $D( to $D*. Eref is the reference voltage of measurements. Any analog signal which is a voltage between ( volt and '.' voltage is measured between ( to #)( -)( bit or )(#'/. Anything beyond '.' voltage will be considered ) or )(#' but it is never recommended to increase it beyond 6 volts else the device will be damaged. 1or eFample connect $D("$D) with the Eref and $D#"$D' with ground or Gero volt and then run the following script. H. H. H.. . H.... . H.... :he value of $D("$D) is )(#' whereas the $D#"$D' is ( . <ow lets put an 53R across $D( in the following way and see how it behaves. :he )(7 resistance can be of any value between '.'7 ohms to )(7 ohms. Ihen there is no light the 53R becomes a resistor of few mega ohms thereby sending full '.' volt to the output which is connected to $D(. n the other hand as the intensity of light increases "sending the resistor value down to finally at full light it becomes Gero thus connecting the $D( to ground. :hus 53R becomes a measurement of light. Put a beam of light on the 53R and the channel?( reading increases towards )(#' for sure on the other hand cover it with your palm and the value reduces.

Analog temperature sensor: :MP'+ is a '.' volt device and it!s value varies linearly between ( to '.' volt as per ( degree $ent. at ( volt and #0( degree $ent at '.' volt. At ).6 volt it gives )(( 3eg $ent. Caution: ccasionally I find people complain for not getting the analog reading properly " the reading changes very fast etc and in most of the cases I find it!s a few faulty wires going to or coming from Raspi. Dad this been integrated in the Raspi board itself in the design state for both M$P'((0 4 M$P#'()* this hassle of connections etc could have been avoided altogether. %ome latest development boards have been built upon this very concept ? having every kinds of port in plenty on board. =ut frankly speaking it steals away the learning skill that you have learnt just now C Raspi provides everything yet you have to learn a little bit of everything to harness that advantage and once you ac;uire that knowledge you look no further C My python script @ sudo nano adc.py import spidev import time spi J spidev.%pi3ev-/ spi.open-("(/ A read %PI data from M$P'((0 chip" 0 possible adc!s -( thru */ def readadc-adcnum/, if --adcnum K */ or -adcnum L (//, return ?) r J spi.Ffer#-M)"-0Nadcnum/LL."(O/ adcout J --rM)O4'/ LL 0/ N rM#O return adcout while :rue, tempJreadadc-)/P''(>)(#' ? 6(

lightJreadadc-(/ print Q:emp,Q"temp print Q5ight,Q"light time.sleep-6/ Aend of program

%chematic diagram,

My prototype,

Pi?face from Adafruit.com -around @.6 a piece/ is nothing but a combi of a M$P#'()* 4 M$P'((0 to provide additional analog 4 digital ports for Raspi which now you know is nothing but adding a few wires here and there of Raspi. Word of caution: M$P'((0 can take maFimum 6 volt input but on 6 volt input the :MP'+ will give erroneous reading. Rou need to trim it!s linear analog scale accordingly. =ye bye.. %. =era Eindhyanagar