Skip to content

sparkfun/Qwiic_OPT4048_Py

Repository files navigation

Qwiic_OPT4048_Py

follow on Twitter

SparkFun Qwiic Tristimulus Color Sensor OPT4048

Python module for the following:

SparkFun Tristimulus Color Sensor - OPT4048DTSR (Qwiic) SparkFun Mini Tristimulus Color Sensor - OPT4048DTSR (Qwiic)

This python package is a port of the existing SparkFun OPT4048 Arduino Library

This package can be used in conjunction with the overall SparkFun Qwiic Python Package

New to qwiic? Take a look at the entire SparkFun qwiic ecosystem.

⚠️ Using this sensor on a Raspberry Pi? ⚠️

Your system might need modification. See this note.

Contents

Supported Platforms

The qwiic Buzzer Python package current supports the following platforms:

Dependencies

This driver package depends on the qwiic I2C driver: Qwiic_I2C_Py

Documentation

The SparkFun Qwiic Trisitumuls Color Sensor module documentation is hosted at ReadTheDocs

Installation

PyPi Installation

This repository is hosted on PyPi as the sparkfun_opt4048_tristimulus_color_sensor package. On systems that support PyPi installation via pip, this library is installed using the following commands

For all users (note: the user must have sudo privileges):

sudo pip install sparkfun_opt4048_tristiumuls_color_sensor

For the current user:

pip install sparkfun_opt4048_tristiumuls_color_sensor

To install, make sure the setuptools package is installed on the system.

Direct installation at the command line:

python setup.py install

To build a package for use with pip:

python setup.py sdist

A package file is built and placed in a subdirectory called dist. This package file can be installed using pip.

cd dist
pip install sparkfun_opt4048_tristimulus_color_sensor-<version>.tar.gz

Raspberry Pi Use

For this sensor to work on the Raspberry Pi, I2C clock stretching must be enabled.

To do this:

  • Login as root to the target Raspberry Pi
  • Open the file /boot/config.txt in your favorite editor (vi, nano ...etc)
  • Scroll down until the block that contains the following is found:
dtparam=i2c_arm=on
dtparam=i2s=on
dtparam=spi=on
  • Add the following line:
# Enable I2C clock stretching
dtparam=i2c_arm_baudrate=10000
  • Save the file
  • Reboot the raspberry pi

Example Use

See the examples directory for more detailed use examples.

import qwiic_opt4048 
import sys
import time


def runExample():
    print("\nExample 1 - Basic Settings\n")

    # Create instance of device
    myColor = qwiic_opt4048.QwOpt4048()

    # Check if it's connected
    if myColor.is_connected() is False:
        print(
            "The device isn't connected to the system. Please check your connection",
            file=sys.stderr,
        )
        return

    # Initialize the device
    if myColor.begin() is False:
        print(
            "Could not communicate with the Sensor, is the correct address selected?",
            file=sys.stderr,
        )
        return

    myColor.set_basic_setup()

    while True:
        print("CIEx: %f, CIEy: %f" % (myColor.get_CIEx(), myColor.get_CIEy()))
        # Delay time is set to the conversion time * number of channels
        # You need three channels for color sensing @ 200ms conversion time = 600ms.
        time.sleep(0.6)


if __name__ == "__main__":
    try:
        runExample()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print("\nEnding Example")
        sys.exit(0)

SparkFun - Start Something