Jump to content

MCP3008 on OrangePi Zero


Recommended Posts

Hello,

 

i want to read from a mcp3008 on my orange pi zero with armbian and python.

Sadly i can't read any data from my moisture sensor on channel 0 over the mcp3008.

Anyone got an idea or a tutorial for the mcp3008 with orange pi zero?

 

This is what i got right now:


pi@orangepizero:~/spi/SPI-Py$ ls -la /dev/spidev*
crw------- 1 root root 153, 0 Mar 20 13:17 /dev/spidev0.0
crw------- 1 root root 153, 1 Mar 20 13:17 /dev/spidev1.0

 

Vdd / Vref on 3,3.

Agnd / Dgnd on ground.

CLK on SPI1_CLK/PA14

Dout on SPI1_MOSI/PA15

Din on SPI1_MISO/PA16

CS/SHDN on SPI1_CS/PA13

 

 

 

 

Code:


#!/usr/bin/python

import spidev
import time
import os

# Open SPI bus
spi = spidev.SpiDev()
spi.open(1,0)

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data

# Define sensor channels
# (channels 3 to 7 unused)
swt_channel = 0
vrx_channel = 1
vry_channel = 2

# Define delay between readings (s)
delay = 0.5

while True:
  # Print out results
  print "--------------------------------------------"
  print(ReadChannel(0))
  print(ReadChannel(1))
  print(ReadChannel(2))
  print(ReadChannel(3))
  print(ReadChannel(4))
  print(ReadChannel(5))
  print(ReadChannel(6))
  print(ReadChannel(7))

  # Wait before repeating loop
  time.sleep(delay)

 

Result:


pi@orangepizero:~/spi/SPI-Py$ sudo python spi-test.py
--------------------------------------------
0
0
0
0
0
0
0
0

orangepizeromcp3008.jpg

Link to comment
Share on other sites

25 minutes ago, zador.blood.stained said:

Are you sure about your wiring? OPi Zero is the master device in your circuit and MCP3008 is a slave device, so Dout should be wired to MISO (Master In Slave Out) and Din should be wired to MOSI (Master Out Slave In)

Thx for your reply.
I changed it like you said and now i got (changing) values, even without a sensor on any channel

 

//edit:

with this new code i still get random values but the channel i am looking for, channel 0, is some kind of accurate.

is this normal to get some "ghost" values? channel 1-7 are not wired up but keep changing between 0 and 118

 


#!/usr/bin/python
import spi
import time
import os

# Open SPI bus
status = spi.openSPI(device="/dev/spidev1.0", speed=1000000)

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.transfer((1,(8+channel)<<4,0))
  data = ((adc[1]&3) << 8) + adc[2]
  return data

# Function to convert data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):
  volts = (data * 3.3) / float(1023)
  volts = round(volts,places)
  return volts

# Define delay between readings
delay = 1

while True:
  print(ReadChannel(0))
  print(ReadChannel(1))
  print(ReadChannel(2))
  print(ReadChannel(3))
  print(ReadChannel(4))
  print(ReadChannel(5))
  print(ReadChannel(6))
  print(ReadChannel(7))
  time.sleep(delay)
--------------
1023
27
23
17
14
11
10
10
--------------
1021
0
10
31
36
45
63
107
--------------
1020
62
72
69
68
68
74
92
--------------
667
7
13
11
8
3
2
0
--------------
264
25
36
41
48
55
75
118
--------------
281
69
73
62
54
48
45
54
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines