0

help with getting mcp9808 code to work on pi
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

help with getting mcp9808 code to work on pi

by JzHarrison on Wed Jun 09, 2021 10:23 am

I have been able to get the MCP9808 to work in the past but am totally stumped now.

I am using a pi zero with raspbian lite

I install pip3

sudo apt install python3-pip

cd ~

git clone https://github.com/adafruit/Adafruit_Python_MCP9808.git

cd Adafruit_Python_MCP9808

sudo python3 setup.py install


all seems to go well but when I run simplest.py I get this error

Traceback (most recent call last):
File "simpletest.py", line 29, in <module>
import Adafruit_MCP9808.MCP9808 as MCP9808
ImportError: No module named Adafruit_MCP9808.MCP9808

JzHarrison
 
Posts: 14
Joined: Fri Feb 22, 2019 12:12 pm

Re: help with getting mcp9808 code to work on pi

by mikeysklar on Wed Jun 09, 2021 7:21 pm

@JzHarrison,

You can use pip3 to install the library. No need to go to the old github site for the original Python code.

Code: Select all | TOGGLE FULL SIZE
sudo pip3 install adafruit-circuitpython-mcp9808


If you use sudo to install with pip3 then be consistent and use sudo python3 to run the example code.

Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_mcp9808

i2c = board.I2C()  # uses board.SCL and board.SDA

# To initialise using the default address:
mcp = adafruit_mcp9808.MCP9808(i2c)

# To initialise using a specified address:
# Necessary when, for example, connecting A0 to VDD to make address=0x19
# mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x19)

while True:
    tempC = mcp.temperature
    tempF = tempC * 9 / 5 + 32
    print("Temperature: {} C {} F ".format(tempC, tempF))
    time.sleep(2)



https://learn.adafruit.com/adafruit-mcp ... -2997670-6

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: help with getting mcp9808 code to work on pi

by JzHarrison on Thu Jun 10, 2021 7:49 am

thank you very much for your prompt reply, unfortunately I am still not having success.

I installed the library as you suggested and made a file called temp.py containing the code you posted.

when I ran it with sudo python3 I got the following error

pi@temptest:~ $ sudo python3 temp.py
Traceback (most recent call last):
File "temp.py", line 7, in <module>
import adafruit_mcp9808
File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp9808.py", line 41, in <module>
from adafruit_register.i2c_bits import RWBits
ModuleNotFoundError: No module named 'adafruit_register

I am sorry to be a bother.

regards

John

JzHarrison
 
Posts: 14
Joined: Fri Feb 22, 2019 12:12 pm

Re: help with getting mcp9808 code to work on pi

by mikeysklar on Thu Jun 10, 2021 12:06 pm

Hi John,

A failure, but I think this is still good progress.

There is a line in the provided example:

Code: Select all | TOGGLE FULL SIZE
# mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x19)


The i2c address is manually being passed there.

Can you confirm that your MCP9808 is wired up to the Pi and that you are seeing the i2c address for it. I'm curious what the address is that is showing up.

Generally this command will show you what is on the i2c bus:

Code: Select all | TOGGLE FULL SIZE
sudo i2cdetect -y 1

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: help with getting mcp9808 code to work on pi

by JzHarrison on Thu Jun 10, 2021 12:35 pm

thanks for your continued help- much appreciated.
results of the command you gave me are below

pi@temptest:~ $ sudo python3 temp.py
Traceback (most recent call last):
File "temp.py", line 7, in <module>
import adafruit_mcp9808
File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp9808.py", line 41, in <module>
from adafruit_register.i2c_bits import RWBits
ModuleNotFoundError: No module named 'adafruit_register'

pi@temptest:~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
pi@temptest:~ $

JzHarrison
 
Posts: 14
Joined: Fri Feb 22, 2019 12:12 pm

Re: help with getting mcp9808 code to work on pi

by mikeysklar on Fri Jun 11, 2021 10:42 am

Let's focus on the i2cdetect not showing any i2c devices connected.

Two things to confirm here.

1) Did you enable i2c using raspi-config

2) Post a photo of your wiring to the Pi with your MCP9808 so we can confirm all four connections (3v3 / GND / SDA / SDL) have been made.

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: help with getting mcp9808 code to work on pi

by JzHarrison on Fri Jun 11, 2021 12:29 pm

hi - yes I have enabled. i12c
I have a cloned sd card from another pi which has a MCP9808 attached and working so I put that card in the pi and simplest.py is giving me the correct temperature readings so I am sure my wiring is ok - but photo attached as requested

Image

JzHarrison
 
Posts: 14
Joined: Fri Feb 22, 2019 12:12 pm

Re: help with getting mcp9808 code to work on pi

by JzHarrison on Fri Jun 11, 2021 12:58 pm

when I run sudo i2cdetect -y 1

I get

pi@temptest:~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- 18 -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
pi@temptest:~ $
but running the code gives

sudo python3 temp.py
Traceback (most recent call last):
File "temp.py", line 7, in <module>
import adafruit_mcp9808
File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp9808.py", line 41, in <module>
from adafruit_register.i2c_bits import RWBits
ModuleNotFoundError: No module named 'adafruit_register'
pi@temptest:~ $

JzHarrison
 
Posts: 14
Joined: Fri Feb 22, 2019 12:12 pm

Re: help with getting mcp9808 code to work on pi

by mikeysklar on Fri Jun 11, 2021 5:16 pm

That is a good sign that you are seeing the i2c address. Wiring looks good.

It sounds as though simpletest works as well. Maybe it is just an initialization issue with temp.py?

Since this device is at i2c address 0x18 does it make a difference if you manually initialize the mcp9808 in the code:

Code: Select all | TOGGLE FULL SIZE
mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x18)

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: help with getting mcp9808 code to work on pi

by JzHarrison on Sat Jun 12, 2021 5:46 am

hi

simplest worked on the sd card that I have previously got working. It does not work on the card that I am trying to install on now. I just did that to check the wiring was correct.
I have changed the mcp line to
mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x18)

and get this when running

pi@temptest:~ $ sudo python3 temp.py
Traceback (most recent call last):
File "temp.py", line 7, in <module>
import adafruit_mcp9808
File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp9808.py", line 41, in <module>
from adafruit_register.i2c_bits import RWBits
ModuleNotFoundError: No module named 'adafruit_register'
pi@temptest:~ $

I notice that their is a difference in my home directory between the card the works and the one I am trying to install to

The one that I am trying to install to has this in the /home/pi/ directory

Bookshelf temp3.py temp.py test1.py

the one that works has in /home/pi/ a directory Adafruit_Python_MCP9808 which has a number of files and sub directories.

JzHarrison
 
Posts: 14
Joined: Fri Feb 22, 2019 12:12 pm

Re: help with getting mcp9808 code to work on pi

by mikeysklar on Mon Jun 14, 2021 1:38 pm

So far this does sound like a library difference between the working SD card installation and the non-working. It appears that this was a MCP9808 library issue for the error you are seeing around "No module named 'adafruit_register'. It has been resolved recently, but you need to be on version 3.3.7 or higher.

https://github.com/adafruit/Adafruit_Ci ... /issues/30

Code: Select all | TOGGLE FULL SIZE
sudo pip3 install --upgrade adafruit-circuitpython-mcp9808

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: help with getting mcp9808 code to work on pi

by JzHarrison on Wed Jun 16, 2021 9:09 am

I did the pip3 upgrade

I then ran the python script and got this error

pi@temptest:~ $ python test.py
Traceback (most recent call last):
File "test.py", line 6, in <module>
import adafruit_mcp9808
ImportError: No module named adafruit_mcp9808

JzHarrison
 
Posts: 14
Joined: Fri Feb 22, 2019 12:12 pm

Re: help with getting mcp9808 code to work on pi

by mikeysklar on Wed Jun 16, 2021 9:48 am

You will need to be consistent with your python execution. You had been running python3 before the script and in the last instance you ran python (without the '3'). One will cause a failure to find the libraries.

also....

What versions are you running for you circuitpython related pip3 libraries? The pip3 installer can lag behind github releases.

Code: Select all | TOGGLE FULL SIZE
pip3 list | grep -i ada

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: help with getting mcp9808 code to work on pi

by JzHarrison on Wed Jun 16, 2021 1:04 pm

hi

result using python3 to run the script


pi@temptest:~ $ python3 test.py
Traceback (most recent call last):
File "test.py", line 8, in <module>
i2c = board.I2C() # uses board.SCL and board.SDA
NameError: name 'board' is not defined

running pip3 list | grep -I ada


pi@temptest:~ $ pip3 list | grep -i ada
Adafruit-Blinka 6.10.2
adafruit-circuitpython-busdevice 5.0.6
adafruit-circuitpython-mcp9808 3.3.8
adafruit-circuitpython-register 1.9.5
Adafruit-PlatformDetect 3.13.4
Adafruit-PureIO 1.1.9
pi@temptest:~ $

JzHarrison
 
Posts: 14
Joined: Fri Feb 22, 2019 12:12 pm

Re: help with getting mcp9808 code to work on pi

by JzHarrison on Wed Jun 16, 2021 2:07 pm

hi - success, thanks for all your help, I went back through your posts and followed a link from this link you sent me https://learn.adafruit.com/adafruit-mcp ... -2997670-6

that advised me to

sudo pip3 install adafruit-blinka
- sudo pip3 install adafruit-circuitpython-mcp9808
- sudo pip3 install adafruit_circuitpython_register

PYTHON SCRIPT

import board
import busio
import adafruit_mcp9808
i2c = busio.I2C(board.SCL, board.SDA)
mcp = adafruit_mcp9808.MCP9808(i2c)
print('Temperature: {} degrees C'.format(mcp.temperature))


which works perfectly - thanks for sticking with me and being so helpful'

regards

John

JzHarrison
 
Posts: 14
Joined: Fri Feb 22, 2019 12:12 pm

Please be positive and constructive with your questions and comments.