adafruit / Adafruit_CircuitPython_LTR390

CircuitPython library for the LTR390 ambient light and UV sensor
MIT License
7 stars 2 forks source link

MemoryError: memory allocation failed while using adafruit_circuitplayground.express #7

Closed JohannaE closed 2 years ago

JohannaE commented 3 years ago

Code is working without including adafruit_circuitplayground.express:

import board
import busio
import gc
import adafruit_ltr390

gc.collect()

i2c = busio.I2C(board.SCL, board.SDA)
ltr = adafruit_ltr390.LTR390(i2c)

print("UV:", ltr.uvs, "\t\tAmbient Light:", ltr.light)
print("UV Index:", ltr.uvi, "\t\tLux:", ltr.lux)
print("mem: ", gc.mem_free())

As you can see in the output only 4752 bytes are free after importing import adafruit_ltr390:

code.py output:
UV: 0       Ambient Light: 224
UV Index: 0.0       Lux: 179.2
mem:  4752

Code done running.

The code to measure free memory:

import gc

gc.collect()
print("mem: ", gc.mem_free())

Without including anything and measuring the free memory the circuit playground express has 19648 bytes free.

code.py output:
mem:  19648

Code done running.

The code which includes the adafruit_ltr390 toghter with the adafruit_circuitplayground.express libraries:

from time import sleep
import board
import busio
from adafruit_circuitplayground.express import cpx
import adafruit_ltr390

i2c = busio.I2C(board.SCL, board.SDA)
ltr = adafruit_ltr390.LTR390(i2c)

print("mem: ", gc.mem_free())

As a result of including both libraries the memory allocation failes:

code.py output:
Traceback (most recent call last):
  File "code.py", line 5, in <module>
  File "adafruit_ltr390.py", line 90, in <module>
MemoryError: memory allocation failed, allocating 184 bytes

The problem is that I cannot use any other funcionalty of my board in combination with the ltr390 uv sensor. I don`t unterstand why the ltr390 library causes the memory allocation failure.

My usecase involves visualizing the uv index value using the neo pixels and speakers. Because of the memory failure I can only use either the neo pixels + sound or the ltr390 uv sensor.

jposada202020 commented 3 years ago

Some M0 boards are limited in memory, so this possible the main of your problem. Please see this https://learn.adafruit.com/Memory-saving-tips-for-CircuitPython There are some hacking to do in the libraries themselves. please see https://github.com/adafruit/Adafruit_CircuitPython_DPS310/pull/15. If you want to discuss more about this please feel free to comment in the Discor channel #help-with-circuitpython

Thanks.

caternuson commented 2 years ago

Closing. I think this a known issue of dealing with limited memory space. So not really an issue specific to this library.

The CPX is an M0 based board. The adafruit_circuitplayground.express is also fairly large, so is likely taking up most of the post. If you aren't using many of the CPX's features, you can try not using the adafruit_circuitplayground.express library and then manually setup of those things you are using, like NeoPixels.