Uncategorized

MCP3008 notes

Asked 
Active today
Viewed 8 times
0

I’m building a device that will measure soil moisture, and then water the garden if the moisture is too low. I am using a MCP3008 to translate analog signal for my raspberry pi, but I’m new to this kind of chip and spidev. Can anyone tell me how to put the reading of the sensor in a variable? I have the chip wired and spidev installed, but I don’t know what code to use.

P.S. I can’t use adafruit software.

 New contributor
  • 1
    There must be hundreds if not thousands of on-line tutorials. Follow one and if you have a problem tell us what you did and what did and did not work. – joan 1 hour ago
  • 1
    I’ve tried that, but I can’t find a clear answer. – UNKNOWN 1 hour ago
  • Hi @UNKNOWN, Welcome and nice to meet you. Ah, let me see. So you have MCP3008 wired, and spidev installed, but you don’t know how to code. Let me see if I can help. First, a couple of questions: (1) Have you read the datasheet and found things not clear? If yes, I can try to explain the things your are not clear, before trying to give “clear” answer. – tlfong01 54 mins ago   
  • Of course there are too many things in the datasheet not “clear”. But there are a couple of things you need to make very “clear”, otherwise you can’t code. Let us start with the datasheet, and skim Sections 3, 4, 5.: (1) MCP3004/3008 2.7V 4-Channel/8-Channel 10-Bit A/D Converters with SPI Serial Interface – MicroChip ww1.microchip.com/downloads/en/devicedoc/21295c.pdf. / to continue, … – tlfong01 44 mins ago    
  • Now another question: (2) Usually SPI ADCs have “registers”, and you use SPI commands to read and write the registers, and job is done. Now do you have any experience in using SPI to read and write a register? If not, then you need to read a SPI tutorial for newbies and do some coding to get to know the stuff. If you understand SPI well, then you might find many MCP3008 tutorials “clear”. – tlfong01 29 mins ago   
  • You might like to read a similar How-to-code-MCP3008 question: (1) How can Rpi python control MCP3008 ADC? raspberrypi.stackexchange.com/questions/106728/…. But you won’t find any answer there, because the answer is hddden in the CHAT: (2) “CHAT about MCP3008”: chat.stackexchange.com/rooms/102745/…. – tlfong01 17 mins ago    
  • In case you wish to know how a MCP3008 code looks like, here is one: (1) “MCP3208 Programming Penzu Log”: penzu.com/public/79948cde. In case you want to know if I have any code for MCP3008, the answer is “No”, because I don’t spoon feed codes for lazy to learn guys. I gave hints and ask them to code themselves, this way they learn and understand better. Ah bed time! I call it a day, see you later. – tlfong01 6 mins ago   
  • Last question before I go: Why can’t you use Adafruit software? – tlfong01 4 mins ago   Edit   

1

Not a great answer (sorry folks) but a great tutorial dedicated to the Pi and this chip is at Adafruit here

They give clear wiring diagrams:

Chip connected to cobbler

That can be translated to the GPIO pins by the diagram at PINOUT.XYZ if you do not have the breakout boards they use.

They have their own Python support library (here at GitHub) that is a one line install:

sudo pip3 install adafruit-circuitpython-mcp3xxx

and very simple code to read the analogue values:

# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D22)

# create the mcp object
mcp = MCP.MCP3008(spi, cs)

# create an analog input channel on pin 0
chan0 = AnalogIn(mcp, MCP.P0)

(Code snippet take from Adafruit example here in the above tutorial and is not stand alone)

Big thanks to Adafruit and Michael Sklar in particular for producing these. They also sell the chip and starter kits – personally I would go for one of the kits every time unless you are very familiar with he technology due tot he hand holding examples you get.

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: