Issue when using guizero mcp3008

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Post Reply
User avatar
ricky2777
 
Posts: 13
Joined: Mon Dec 23, 2024 5:58 am

Issue when using guizero mcp3008

Post by ricky2777 »

I tried to use adafruit_mcp3008 library but it did not work.
So I followed suggestion and used guizero mcp3008.
However, there is this little issue(I am using raspberrypi4b and bookworm)
When I used a voltage divider, the value of adc output should be 0.5, but it is 0.374694...
This is my code:

Code: Select all

import time 
import argparse 
from gpiozero import MCP3008
# Import SPI and MCP3008 library. 
import RPi.GPIO as GPIO 

DEBUG = True 
parser = argparse.ArgumentParser(description="Read AD/MCP3008") 
parser.add_argument('--times', type=int, default=5,
					help='number of times to read the pin') 
parser.add_argument('--delay', type=float, default=2,                     
					help='seconds of delay between readings') 
parser.add_argument('--AD_pin', type=int, default=0, 
					help='AD input pin to read') 
args = parser.parse_args() 

if (DEBUG):   
	print(f'pin: {args.AD_pin}\tTime: {args.times}\tDel: {args.delay}') 

i=0 
while True:
	adc = MCP3008(channel=args.AD_pin)  
	for values in adc.values:
		print (f'Raw ADC Value: {values}') 
		time.sleep(args.delay) 
		i+=1
		if i >=args.times:
			break
	break
		
This is my wiring:
微信图片_20250122082859.jpg
微信图片_20250122082859.jpg (231.8 KiB) Viewed 51 times
微信图片_20250122082857.jpg
微信图片_20250122082857.jpg (327.76 KiB) Viewed 51 times

User avatar
adafruit_support_carter
 
Posts: 31428
Joined: Tue Nov 29, 2016 2:45 pm

Re: Issue when using guizero mcp3008

Post by adafruit_support_carter »

I tried to use adafruit_mcp3008 library but it did not work.
We (Adafruit) can try and help with this if you can provide more information.

For gpiozero support (not an Adafruit library), see here:
https://github.com/gpiozero/gpiozero

User avatar
ricky2777
 
Posts: 13
Joined: Mon Dec 23, 2024 5:58 am

Re: Issue when using guizero mcp3008

Post by ricky2777 »

OK,my original plan was to use adafruit_mcp3008 library. And that was how I did.

Here is my code:

Code: Select all


#MCP3008 
import time 
import argparse 

# Import SPI and MCP3008 library. 
import RPi.GPIO as GPIO 
import Adafruit_MCP3008 
from Adafruit_GPIO.GPIO import RPiGPIOAdapter as Adafruit_GPIO_Adapter 

# Software SPI configuration: 
CLK  = 23       # pin 13 on 3008 
MISO = 21       # pin 12 on 3008 
MOSI = 19       # pin 11 on 3008 
CS   = 24       # pin 10 on 3008 

gpio_adapter = Adafruit_GPIO_Adapter(GPIO, mode=GPIO.BOARD) 
mcp = Adafruit_MCP3008.MCP3008(clk=CLK,cs=CS,miso=MISO,mosi=MOSI,gpio=gpio_adapter) 

DEBUG = True 
parser = argparse.ArgumentParser(description="Read AD/MCP3008") 
parser.add_argument('--times', type=int, default=5,
					help='number of times to read the pin') 
parser.add_argument('--delay', type=float, default=2,                     
					help='seconds of delay between readings') 
parser.add_argument('--AD_pin', type=int, default=0, 
					help='AD input pin to read') 
args = parser.parse_args() 

if (DEBUG):   
	print(f'pin: {args.AD_pin}\tTime: {args.times}\tDel: {args.delay}') 

i=0 
while (i<args.times):   
	print(f'Raw ADC Value: {mcp.read_adc(args.AD_pin)}')   
	time.sleep(args.delay) 
	i=i+1
Wiring was exactly the same.

It gives me this message:
Traceback (most recent call last):
File "/home/ricky/Desktop/module练习/module3.py", line 62, in <module>
mcp = Adafruit_MCP3008.MCP3008(clk=CLK,cs=CS,miso=MISO,mosi=MOSI,gpio=gpio_adapter)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/Adafruit_MCP3008-1.0.2-py3.11.egg/Adafruit_MCP3008/MCP3008.py", line 42, in __init__
File "/usr/local/lib/python3.11/dist-packages/Adafruit_GPIO-1.0.4-py3.11.egg/Adafruit_GPIO/SPI.py", line 165, in __init__
File "/usr/local/lib/python3.11/dist-packages/Adafruit_GPIO-1.0.4-py3.11.egg/Adafruit_GPIO/GPIO.py", line 187, in setup
File "/usr/lib/python3/dist-packages/RPi/GPIO/__init__.py", line 704, in setup
initial = _check(lgpio.gpio_read(_chip, gpio))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/lgpio.py", line 903, in gpio_read
return _u2i(_lgpio._gpio_read(handle&0xffff, gpio))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/lgpio.py", line 458, in _u2i
raise error(error_text(v))
lgpio.error: 'GPIO not allocated'

User avatar
adafruit_support_carter
 
Posts: 31428
Joined: Tue Nov 29, 2016 2:45 pm

Re: Issue when using guizero mcp3008

Post by adafruit_support_carter »

Thanks. That info helps. You're using an old deprecated library. The current library is this one:
https://github.com/adafruit/Adafruit_Ci ... n_MCP3xxx/

What model Pi are you using?

Have you gone through the initial Blinka setup as covered in this guide?
https://learn.adafruit.com/circuitpytho ... spberry-pi

User avatar
ricky2777
 
Posts: 13
Joined: Mon Dec 23, 2024 5:58 am

Re: Issue when using guizero mcp3008

Post by ricky2777 »

I use raspberry pi 4b bookworm

User avatar
adafruit_support_carter
 
Posts: 31428
Joined: Tue Nov 29, 2016 2:45 pm

Re: Issue when using guizero mcp3008

Post by adafruit_support_carter »

OK, start by following the Blinka guide and make sure that initial setup is working OK.

Make sure the Blinka test script passes:
https://learn.adafruit.com/circuitpytho ... st-3030038

User avatar
ricky2777
 
Posts: 13
Joined: Mon Dec 23, 2024 5:58 am

Re: Issue when using guizero mcp3008

Post by ricky2777 »

I followed the instruction. Here is my code now.

Code: Select all

#MCP3008 simpletest.py
import time 
import argparse 

# Import SPI and MCP3008 library. 
# import RPi.GPIO as GPIO 
# import Adafruit_MCP3008 
# from Adafruit_GPIO.GPIO import RPiGPIOAdapter as Adafruit_GPIO_Adapter 
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn

# # Software SPI configuration: 
# CLK  = 23       # pin 13 on 3008 
# MISO = 21       # pin 12 on 3008 
# MOSI = 19       # pin 11 on 3008 
# CS   = 24       # pin 10 on 3008 

# gpio_adapter = Adafruit_GPIO_Adapter(GPIO, mode=GPIO.BOARD) 
# mcp = Adafruit_MCP3008.MCP3008(clk=CLK,cs=CS,miso=MISO,mosi=MOSI,gpio=gpio_adapter) 

# 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.D5)

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

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

DEBUG = True 
parser = argparse.ArgumentParser(description="Read AD/MCP3008") 
parser.add_argument('--times', type=int, default=5,
					help='number of times to read the pin') 
parser.add_argument('--delay', type=float, default=2,                     
					help='seconds of delay between readings') 
parser.add_argument('--AD_pin', type=int, default=0, 
					help='AD input pin to read') 
args = parser.parse_args() 

if (DEBUG):   
	print(f'pin: {args.AD_pin}\tTime: {args.times}\tDel: {args.delay}') 

i=0 
while (i<args.times):   
	print(f'Raw ADC Value: {chan.value}')   
	time.sleep(args.delay) 
	i=i+1
I did not change the wiring. However, everytime I read, it always give me adc reading of 0.Where it should be 0.5 since I have a voltage divider with two 10 k ohm resisters.

User avatar
adafruit_support_carter
 
Posts: 31428
Joined: Tue Nov 29, 2016 2:45 pm

Re: Issue when using guizero mcp3008

Post by adafruit_support_carter »

Please post a new photo of the current setup.

User avatar
ricky2777
 
Posts: 13
Joined: Mon Dec 23, 2024 5:58 am

Re: Issue when using guizero mcp3008

Post by ricky2777 »

IMG_7405-compressed.jpeg
IMG_7405-compressed.jpeg (237.93 KiB) Viewed 33 times
IMG_7404-compressed.jpeg
IMG_7404-compressed.jpeg (215.01 KiB) Viewed 33 times
IMG_7403-compressed.jpeg
IMG_7403-compressed.jpeg (262.98 KiB) Viewed 33 times

User avatar
adafruit_support_carter
 
Posts: 31428
Joined: Tue Nov 29, 2016 2:45 pm

Re: Issue when using guizero mcp3008

Post by adafruit_support_carter »

Thanks. The chip select (CS) pin is still connected to CE0 on the Pi GPIO. Move to GPIO5 to match code.
cs.png
cs.png (713.59 KiB) Viewed 32 times

User avatar
ricky2777
 
Posts: 13
Joined: Mon Dec 23, 2024 5:58 am

Re: Issue when using guizero mcp3008

Post by ricky2777 »

thanks i will try that

User avatar
ricky2777
 
Posts: 13
Joined: Mon Dec 23, 2024 5:58 am

Re: Issue when using guizero mcp3008

Post by ricky2777 »

Sorry, I dont think I understand, cause cs should be the purple jumper line, right?

User avatar
ricky2777
 
Posts: 13
Joined: Mon Dec 23, 2024 5:58 am

Re: Issue when using guizero mcp3008

Post by ricky2777 »

Problem solved. I reused the original code of library Adafruit_MCP3008. and it went well. Thank you very much.

Post Reply
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”