0

MCP 9808 and Trinket M0
Moderators: adafruit_support_bill, adafruit

Forum rules
Adafruit MicroPython is currently EXPERIMENTAL and BETA - Please visit https://learn.adafruit.com/category/micropython and http://forum.micropython.org/ in addition to our section here!
Please be positive and constructive with your questions and comments.

MCP 9808 and Trinket M0

by ryespy on Mon Sep 11, 2017 5:49 pm

Hello everyone,

Could someone point me towards a resource for reading a MCP 9808 temperature sensor with my Trinket. I see the one for the Raspberry Pi, but I am not sure if I can just use that library. I am just getting started with CircuitPython. Thank you for helping me.

Best,
Ryan

ryespy
 
Posts: 15
Joined: Fri May 10, 2013 1:30 pm

Re: MCP 9808 and Trinket M0

by tannewt2 on Mon Sep 11, 2017 5:54 pm


tannewt2
 
Posts: 362
Joined: Thu Oct 06, 2016 8:48 pm

Re: MCP 9808 and Trinket M0

by ryespy on Mon Sep 11, 2017 5:59 pm

Thank you tannewt2!

ryespy
 
Posts: 15
Joined: Fri May 10, 2013 1:30 pm

Re: MCP 9808 and Trinket M0

by ryespy on Tue Sep 12, 2017 5:05 pm

I apologize for being such a NOOB here, but I am not sure how to proceed. I think I added the libraries correctly, but can someone help guide me. I am getting the follwing error when running this code on my board:
main.py output:
Traceback (most recent call last):
File "main.py", line 16, in <module>
File "main.py", line 16, in <module>
AttributeError: 'module' object has no attribute 'MCP9808'

Thanks for the help. I would love to get this working so I can share my project tomorrow night.
Attachments
Screen Shot 2017-09-12 at 2.57.44 PM.png
Screen Shot 2017-09-12 at 2.57.44 PM.png (89.79 KiB) Viewed 93 times

ryespy
 
Posts: 15
Joined: Fri May 10, 2013 1:30 pm

Re: MCP 9808 and Trinket M0

by tannewt2 on Tue Sep 12, 2017 5:23 pm

It looks like your file structure adds an extra layer of adafruit_mcp9808 due to the folder. The file is the second one.

Instead of
Code: Select all | TOGGLE FULL SIZE
import adafruit_mcp9808


try

Code: Select all | TOGGLE FULL SIZE
from adafruit_mcp9808 import adafruit_mcp9808

tannewt2
 
Posts: 362
Joined: Thu Oct 06, 2016 8:48 pm

Re: MCP 9808 and Trinket M0

by ryespy on Tue Sep 12, 2017 5:29 pm

I get another error now:

Traceback (most recent call last):
File "main.py", line 19, in <module>
File "/lib/adafruit_mcp9808/adafruit_mcp9808.py", line 93, in temperature
File "/lib/adafruit_mcp9808/adafruit_mcp9808.py", line 92, in temperature
File "/lib/adafruit_bus_device/i2c_device.py", line 94, in write
OSError: 5

Should I fix the file structure, that is should those files be in /lib/ instead of /lib/adafruit_mcp9808?

ryespy
 
Posts: 15
Joined: Fri May 10, 2013 1:30 pm

Re: MCP 9808 and Trinket M0

by tannewt2 on Tue Sep 12, 2017 7:28 pm

That file structure will be fine. Could you post all of your code in a [CODE] block and post a picture of your setup? Thanks!

tannewt2
 
Posts: 362
Joined: Thu Oct 06, 2016 8:48 pm

Re: MCP 9808 and Trinket M0

by ryespy on Wed Sep 13, 2017 1:31 pm

Code: Select all | TOGGLE FULL SIZE
from board import *
import adafruit_bus_device
import busio
from adafruit_mcp9808 import adafruit_mcp9808

while True:
  # Do one reading
  with busio.I2C(SCL, SDA) as i2c:
    t = adafruit_mcp9808.MCP9808(i2c)

    # Finally, read the temperature property and print it out
  print(t.temperature)

This is my current code just trying to get a value from the sensor. Eventually I would like to insert it in this code in place of the button push. I am making a Neopixel based thermometer.
Code: Select all | TOGGLE FULL SIZE
# Trinket IO demo
# Welcome to CircuitPython :)

from touchio import *
from digitalio import *
from analogio import *
from board import *
#from adafruit_hid.keyboard import Keyboard
#from adafruit_hid.keycode import Keycode

import time
import neopixel

#from board import *
import busio
import adafruit_mcp9808



# NeoPixel strip (of 16 LEDs) connected on D4
NUMPIXELS = 12
NP = neopixel.NeoPixel(D4, NUMPIXELS, brightness=1, auto_write=False)



######################### HELPERS ##############################


blue= (0,0,255)
bluecyan= (0,127,255)
cyan= (0,255,255)
greencyan=(0,255,127)
green= (0,255,0)
greenyellow= (127,255,0)
yellow= (255,255,0)
orange= (255,127,0)
red= (255,0,0)
redmagenta= (255,0,127)
magenta= (255,0,255)
bluemagenta= (127,0,255)
off= (0,0,0)

######################### MAIN LOOP ##############################
push =0
push2=0
i = 0
while True:
  #if not button.value:
  if push <11:
     push= 1+ push
     print (push)
  else:
        push =0
  # Do one reading
  with busio.I2C(SCL, SDA) as i2c:
    t = adafruit_mcp9808.MCP9808(i2c)

    # Finally, read the temperature property and print it out
    print(t.temperature)

  if push ==0:
    NP[0]=blue
    NP[1]=off
    NP[2]=off
    NP[3]=off
    NP[4]=off
    NP[5]=off
    NP[6]=off
    NP[7]=off
    NP[8]=off
    NP[9]=off
    NP[10]=off
    NP[11]=off
    NP.show()
  elif push == 1:
    NP[0]=bluecyan
    NP[1]=bluecyan
    NP[2]=off
    NP[3]=off
    NP[4]=off
    NP[5]=off
    NP[6]=off
    NP[7]=off
    NP[8]=off
    NP[9]=off
    NP[10]=off
    NP[11]=off
    NP.show()
  elif push ==2:
    NP[0]=cyan
    NP[1]=cyan
    NP[2]=cyan
    NP[3]=off
    NP[4]=off
    NP[5]=off
    NP[6]=off
    NP[7]=off
    NP[8]=off
    NP[9]=off
    NP[10]=off
    NP[11]=off
    NP.show()

  elif push ==3:
    NP[0]=greencyan
    NP[1]=greencyan
    NP[2]=greencyan
    NP[3]=greencyan
    NP[4]=off
    NP[5]=off
    NP[6]=off
    NP[7]=off
    NP[8]=off
    NP[9]=off
    NP[10]=off
    NP[11]=off
    NP.show()

  elif push ==4:
    NP[0]=green
    NP[1]=green
    NP[2]=green
    NP[3]=green
    NP[4]=green
    NP[5]=off
    NP[6]=off
    NP[7]=off
    NP[8]=off
    NP[9]=off
    NP[10]=off
    NP[11]=off
    NP.show()

  elif push ==5:
    NP[0]=greenyellow
    NP[1]=greenyellow
    NP[2]=greenyellow
    NP[3]=greenyellow
    NP[4]=greenyellow
    NP[5]=greenyellow
    NP[6]=off
    NP[7]=off
    NP[8]=off
    NP[9]=off
    NP[10]=off
    NP[11]=off
    NP.show()

  elif push ==6:
    NP[0]=yellow
    NP[1]=yellow
    NP[2]=yellow
    NP[3]=yellow
    NP[4]=yellow
    NP[5]=yellow
    NP[6]=yellow
    NP[7]=off
    NP[8]=off
    NP[9]=off
    NP[10]=off
    NP[11]=off
    NP.show()

  elif push ==7:
    NP[0]=orange
    NP[1]=orange
    NP[2]=orange
    NP[3]=orange
    NP[4]=orange
    NP[5]=orange
    NP[6]=orange
    NP[7]=orange
    NP[8]=off
    NP[9]=off
    NP[10]=off
    NP[11]=off
    NP.show()

  elif push ==8:
    NP[0]=red
    NP[1]=red
    NP[2]=red
    NP[3]=red
    NP[4]=red
    NP[5]=red
    NP[6]=red
    NP[7]=red
    NP[8]=red
    NP[9]=off
    NP[10]=off
    NP[11]=off
    NP.show()

  elif push ==9:
    NP[0]=redmagenta
    NP[1]=redmagenta
    NP[2]=redmagenta
    NP[3]=redmagenta
    NP[4]=redmagenta
    NP[5]=redmagenta
    NP[6]=redmagenta
    NP[7]=redmagenta
    NP[8]=redmagenta
    NP[9]=redmagenta
    NP[10]=off
    NP[11]=off
    NP.show()

  elif push ==10:
    NP[0]=magenta
    NP[1]=magenta
    NP[2]=magenta
    NP[3]=magenta
    NP[4]=magenta
    NP[5]=magenta
    NP[6]=magenta
    NP[7]=magenta
    NP[8]=magenta
    NP[9]=magenta
    NP[10]=magenta
    NP[11]=off
    NP.show()

  else:
    NP[0]=bluemagenta
    NP[1]=bluemagenta
    NP[2]=bluemagenta
    NP[3]=bluemagenta
    NP[4]=bluemagenta
    NP[5]=bluemagenta
    NP[6]=bluemagenta
    NP[7]=bluemagenta
    NP[8]=bluemagenta
    NP[9]=bluemagenta
    NP[10]=bluemagenta
    NP[11]=bluemagenta
    NP.show()


In this second code block I am getting an error with import neopixe, "File "main.py", line 12, in <module>
ValueError: Incompatible .mpy file. Please update all .mpy files. See http://adafru.it/mpy-update for more info."

I was running into space issues yesterday and must have messed something up with the drivers or libraries. I followed Tony DiCola's instructions here:https://learn.adafruit.com/micropython-for-samd21/usb-mass-storage#mac-osx-file-copy-issues. When I use the bootloader to put adafruit-circuitpython-trinket_m0-2.0.0.uf2 back on the Trinket should my /CIRCUITPY/ drive have the default files, because mine has my python and library files?

Thank you for any help you can provide.
Attachments
IMG_9002.JPG
IMG_9002.JPG (431.91 KiB) Viewed 45 times

ryespy
 
Posts: 15
Joined: Fri May 10, 2013 1:30 pm

Re: MCP 9808 and Trinket M0

by tannewt2 on Wed Sep 13, 2017 5:00 pm

Hi ryespy,
The error you are getting is because mpy files are incompatible between 1.0.0 and 2.0.0. You'll need to download a bundle for 2.0.0 from here: https://github.com/adafruit/Adafruit_Ci ... g/20170912 and replace each file in lib with the matching one from the bundle. This will upgrade them so they are 2.0 compatible.
Thanks,
Scott

tannewt2
 
Posts: 362
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.