How to connect the MCP3008 Analog to Digital Converter (ADC) to a Raspberry Pi and Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
neat analog inputs on your Raspberry Pi we got you covered with the mCP 3008 ABC all the parts all the code come on in [Music] welcome to makers digest in this episode we're gonna have a look at the mCP 3008 analog to digital converter the Raspberry Pi doesn't have any analog inputs we can use this chip to easily add 8 analog inputs or maybe you just need some more inputs on your Arduino this will work there as well the mCP 3008 is an 8 channel 10 bit analog to digital converter it's similar to the quality and precision you would find on the Arduino Uno using this chip you can read a variety of simple analog inputs like potentiometers temperature sensors light sensors etc one of my favorite uses is reading the inputs from a joystick which is just a couple of potentiometers these chips are pretty cheap too I picked these up for about 3 bucks each off at Amazon which is paying a premium for prime you can pick them up off a place like digit key or something for about 2 and 1/4 USD as usual we're gonna get right down to the implementation today we're gonna start off with a Raspberry Pi because it's got the highest need for analog inputs let's jump over to the workbench and get started all right here we go we're gonna connect the mCP 3008 to the Raspberry Pi there's two ways to connect the mCP to the Raspberry Pi and the hardware connection is almost exactly the same but it's hardware spi versus software SPI Hardware SPI uses very specific pins on the Raspberry Pi as it has serial interfaces on the Raspberry Pi there's two of them if you want to hook it up with software SPI you just for the four wires we're gonna use four SPI you just pick any four GPIO pins and specify in the software we're only gonna do the hardware connection here but in the software that's on github it's got hardware and software both in there so you can take your pic if you run my example in order to run hardware SPI on the Raspberry Pi we need to configure SPI through res PI config and we'll just go through that real quick it's really simple but I wanted to show you how to do it so on the Raspberry Pi run sudo Raz PI config and we want to go down to interfacing options enable disable automatic loading of spi kernel module would you like this enabled yes I would interface is enabled mine was already enabled so it might ask you do you want to auto load the kernel module say yes finish and then you'll have to do a reboot so the kernel module gets loaded correctly but once it comes back up you should be ready to roll in order to connect this we're gonna need a few parts we're gonna need the MCP chip itself an analog device to test with this is a small potentiometer we're gonna need a whole bunch of male-to-male jumper wires there are seven of these and another mess of male-to-female jumper wire and jumper wires and there are six of these let's take the chip will jam it in here somewhere the pin out I'll throw up the pin out over here so we can see what all the pins are if you look at that you can see that there's two power and two ground and then the rest of them are the serial connection so instead of trying to pull directly off of the Raspberry Pi 4 to power and to ground at 3.3 volt and ground we're just going to jumper off of the PI on to the power rail of the breadboard and then jumper from there into the chip let's start off with our ground ground is pin six on the Raspberry Pi this is pin one so we're just gonna count 1 2 3 4 5 6 that's gonna go into our ground rail power 4 this is 3.3 volts and that's on pin 1 on the Raspberry Pi which is here and that's gonna go into the power side of the rail now we can jumper in power and ground and just get that out of the way so that for that we're gonna use our male-to-male jumper wires go from power into pen 16 on the mCP chip which is top left corner here you've got another power on pen 15 this is the voltage reference and now our two grounds we've got 10 14 on the mCP so we'll take off of our ground rail into pin 14 take ground rail to pin 9 all the way down here now that we've got all of our power connections out of the way we can connect our SPI connection which are the 4 remaining wires we have that are male to female the first one is clock this is the serial clock interface for SPI on the Raspberry Pi that is pin 23 and this is one of the things about the Raspberry Pi that drives me crazy is you kind of have to count them off whenever you need to find it so we've got 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 i hook this thing up earlier and I was off by one pin and I couldn't figure out why it wasn't working and then you know eventually I went back and countered it off and I was off by one pin so it threw off the whole thing because you'll see what I'm gonna do next is the next connection is digital out from the mCP chip just pin 12 and that goes to pin 21 on the Raspberry Pi which is M ISO and in serial SPI interface it is M ISO is master input slave output and that's on pin 21 so if this is 23 we go 20 21 and that's kind of what bit me is because I counted off that first one wrong and then I went backwards and so all of them all the rest subsequent we were wrong next is a digital in on the mCP chip which is pin 11 and that goes to pin 19 on the Raspberry Pi which is mo SI mo si is master output slave input and that's pin 19 so if we were just on 21 20 19 and the last one is slave select on the mCP that is pin 10 and on the Raspberry Pi that is pin 22 so if this last one was 23 and 22 is right here and now that we've got all the power and the serial connection done we can hook up one of our devices and our analog device that we're going to test with is this potentiometer it's got three pins on it so just pick a spot on your breadboard plug that in the way that this works is you hook one side of that up to ground the other side of it to the voltage reference and then the center wiper pin connect that to our example we're using pin 8 on the mCP chip but that's going to be referenced as pin 7 because it starts at 0 1 2 3 4 5 6 7 and now that we have this hooked up we have all eight of these pins on this side of the chip that we can reference in our software so let's take a look at the software as usual wrote this offer on my time not yours so it's already done in this example we use the Adafruit GPIO SPI library and the 88 of fruit MCP 3008 library they make great stuff if you want to learn more about that go check out the github repos for those modules here we import this module from time we import sleep and here is the Adafruit GPIO SPI as SPI which is required by Adafruit MCP 3008 as I said before we can do software or hardware and I did some detection in here and or just made it easy for you to change it from hardware to software if you want to so SPI type we have hardware delay is 5 half a second point 5 and if you were using off software SPI this is where you would do the configuration for it you just set the clock master input slave output and master output slave input and slave select to any of these GPIO pins for hardware configuration we're gonna set port 0 because on the Raspberry Pi there are two SPI ports 0 & 1 we're using 0 and so that's where you configure that this next block is just a little bit of logic to detect if it's hardware or software because the mCP module gets instantiated a little bit differently for hardware you have to specify what hardware port for software you specify which GPIO pins correlate to the SPI serial interface this block just determined that we have fired up the software correctly because you have to give it an analogue port if not it'll kick it back and tell you how to use it correctly let's see it just gives us a little bit of information and down here while true valve equals MC P dot read underscore ADC and then the analog port that we have specified when we ran it prints out the value sleeps for half a second goes back through the loop so what we're gonna see is just the value kicking out every time every half second let's run this see how it goes sudo python MCP and they're real I didn't give it a port and it told us we needed to give it a port so remember I said there's eight ports here working on pin eight starts at zero so this is seven and there we go we're getting some values the potentiometer is turned all the way one way so that should be zero let's grab a little screwdriver and what happens there we go and this will go all the way up to 1023 from 0 to 1023 so now you can take those valid values and interpolate them in any way you need to to you know control whatever this could control a rotation of a servo or a stepper motor or you know whatever you wanted to connect to this thing see if we can set it at a precise number let's try for 300 yeah they're up close enough so that we have it we have raspberry pi with eight analog inputs on it right on all right here we go would be Arduino the Arduino can also do software as well as hardware spi we're going to cover hardware spi in this example for this example we're going to need the mCP chip a whole mess of male-to-male jumper wires and an analog device to test with this is our potentiometer yeah I real that's what I want so let's take the mCP chip jam it in wherever we want and as you can see on the pin out that there are multiple power and ground on this so instead of pulling them all off of the Arduino directly or we're gonna go to our power and ground rail here let's start with ground we'll take ground off the Arduino into the ground rail take 5 volts off of the Arduino into our 5 volt rail on the mCP we have pen 16 right here needs 5 volts so we'll take 5 volts from here off of our rail we also need 5 volts for V ref on the mCP 3008 now we'll get our ground so pin 16 15 14 goes to ground as well as 10 9 it'll take ground to pen 9 that's all out of the way we can start connecting the SPI interface the really cool thing about this is on the Arduino Uno the pin numbers match up perfectly so we can take pin 13 which is the s clock that goes to pin 13 on the Arduino pin 12 which is master input slave output goes to 10 12 and 11 on the mCP which is master output slave input goes to pin 11 on here - we know and pin 10 on the mCP which is our slave select goes to pin 10 on the Arduino so that's great easier to remember it's awesome let's set up the analog device we've got small potentiometer here we'll run one side of the potentiometer to ground at the other side to 5 volts which is our reference here and then the center pin the wiper to pen 7 or pen 8 on the mCP which is referenced as pin 7 in the software there it is we've got the connection let's jump over to the software and take a look as usual wrote this on my time not yours and again we're going to use the Adafruit library because ain't a free ride if you don't have this library installed we can easily install it with go over to sketch include library manage libraries and just search for MC p300 8 and it's the first one here Adafruit mCP 3008 by Adafruit version 1 I've already got it installed if you don't have it installed you'll have a button that looks like that click it it'll install and be ready to go so here we include that library we've got a couple of variables set up we've got a delay of half a second 500 milliseconds the channel we're going to read in this example is channel 7 here we instantiate the Adafruit MCP 3008 object as MCP set up our serial port so we can view our set our readings and here's just a little information about how the pins are set up and it's just the you know that works out this way if you have a different AVR board he left at Google and figure out which pins are the SPI pins for your board the Adafruit library example has a bunch of great info on that so you can you know run their example and take a look and this library is set up default for the uno so you can say you have to specify what the slave select pin is here and that's ten but if you leave it blank it will just automatically select 10 for you in the loop portion we have an a value that's an integer and it's MCP read a DC Channel and that is channel 7 we print out the channel value here throw in our delay of 1/2 second and loop back through so every half second it will tell us what the value of this is let's send that over to the Arduino and see what happens look at our serial monitor here and there we have channel value of zero and it's either zero because it's not working or because it's turned all the way to one side let's check it is it's working so it goes from zero all the way up to ten twenty three say 1024 steps reading here and you can interpolate that any way you want for whatever purpose you have let's see if we can nail a 500 there it is awesome there's the Arduino we've done it rad good to go there you have it mCP three thousand eight analog to digital converter running on the Raspberry Pi and the Arduino don't forget all the code we used in this episode is on github and linked below in the description there's also a link to where you can buy this chip if you want to try it for yourself now that you have everything you need go be creative and share with me what you make I can be found on Facebook Instagram Twitter patreon and all those if you like this video please consider subscribing and ringing that little Bell so you'll be notified when a new video is available thanks for watching and until next time keep making I lose that little check 1 2 check 1 2
Info
Channel: Maker's Digest
Views: 14,865
Rating: undefined out of 5
Keywords: Maker, Maker Project, Makers Digest, makersdigest, digest, DIY, Build, arduino, raspberry pi, ADC, analog
Id: eQaBFLbYMNY
Channel Id: undefined
Length: 23min 38sec (1418 seconds)
Published: Wed Jan 09 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.