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.

11
  • 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
    Commented Apr 11, 2020 at 13:15
  • 1
    I've tried that, but I can't find a clear answer.
    – UNKNOWN
    Commented Apr 11, 2020 at 13:17
  • 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 ww1.microchip.com/downloads/en/devicedoc/21295c.pdf. / to continue, ...
    – tlfong01
    Commented Apr 11, 2020 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
    Commented Apr 11, 2020 at 14:13
  • 1
    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
    Commented Apr 11, 2020 at 14:25

1 Answer 1

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.

3
  • Sorry but I can't use adafruit software
    – UNKNOWN
    Commented Apr 13, 2020 at 20:26
  • But can’t you read it to understand how it works?
    – user115418
    Commented Apr 13, 2020 at 20:54
  • Hi everyone I figured out the answer so no need to waste time on this question.
    – UNKNOWN
    Commented Apr 25, 2020 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.