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 programing. 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. I need to check if the moisture level is greater than a certain number.

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

tlfong01
  • 4,665
  • 3
  • 10
  • 24
UNKNOWN
  • 79
  • 1
  • 3
  • 21
  • 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 Apr 11 '20 at 13:15
  • 1
    I've tried that, but I can't find a clear answer. – UNKNOWN Apr 11 '20 at 13:17
  • 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 Apr 11 '20 at 13:48
  • 1
    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 http://ww1.microchip.com/downloads/en/devicedoc/21295c.pdf. / to continue, ... – tlfong01 Apr 11 '20 at 13:57
  • 1
    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 Apr 11 '20 at 14:13
  • 1
    You might like to read a similar How-to-code-MCP3008 question: (1) How can Rpi python control MCP3008 ADC? https://raspberrypi.stackexchange.com/questions/106728/mcp3008-outputting-just-0. But you won't find any answer there, because the answer is hddden in the CHAT: (2) "CHAT about MCP3008": https://chat.stackexchange.com/rooms/102745/discussion-between-tlfong01-and-aubrey-champagne. – tlfong01 Apr 11 '20 at 14:25
  • 1
    In case you wish to know how a MCP3008 code looks like, here is one: (1) "MCP3208 Programming Penzu Log": https://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 Apr 11 '20 at 14:36
  • 1
    Last question before I go: Why can't you use Adafruit software? – tlfong01 Apr 11 '20 at 14:38
  • 1
    The adafruit software gets confused between python 2.7 and python 3. I can't seem to get it to ever put the reading in a varible – UNKNOWN Apr 13 '20 at 20:43
  • 1
    Hi @UNKNOWN, thank you for your explanation. I agree that Adafruit tutorials often mix up python2 and 3 and so sadly confuse newbies. I also found that AdaFruit this couple of years use Circuit Python in their tutorials, and Circuit Python does not run smoothly in Rpi. – tlfong01 Apr 23 '20 at 05:07
  • 1
    Thank you for pointing me in the right direction. :) – UNKNOWN Apr 25 '20 at 13:53

1 Answers1

2

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.