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.
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:
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.