Python & CircuitPython

It's easy to use the AS7262 with Python or CircuitPython and the Adafruit CircuitPython AS726x module.  This module allows you to easily write Python code that reads color data and temperature from the sensor.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

You can easily wire this breakout to a microcontroller running CircuitPython. We will be using a Metro M0 Express.

  • Connect Vin to the power supply, 3-5V is fine
  • Connect GND to common power/data ground
  • Connect the SCL pin to the I2C clock SCL pin on your Feather or Metro M0 (on a Gemma M0 this would be Pad #2/ A1)
  • Connect the SDA pin to the I2C data SDA pin on your Feather or Metro M0 (on a Gemma M0 this would be Pad #0/A2)

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with I2C:

  • Pi 3V3 to sensor VIN
  • Pi GND to sensor GND
  • Pi SDA to sensor SDA
  • Pi SCL to sensor SCL

This sensor uses I2C address 0x49.

CircuitPython Installation of AS726x Library

You'll need to install the Adafruit_CircuitPython_AS726x library library on your CircuitPython board.  

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our introduction guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards you'll need to manually install the necessary libraries from the bundle:

  • adafruit_as726x.mpy
  • adafruit_bus_device
  • adafruit_register

You can also download the adafruit_as726x.mpy from it's release page on Github.

Before continuing make sure your board's lib folder or root filesystem has the adafruit_as726x.mpy,  adafruit_register, and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of AS726x Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-as726x

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate usage we will initialize the sensor and read it's onboard temperature sensor from the board's Python REPL.

Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

Download: file
import board
import busio
from adafruit_as726x import Adafruit_AS726x

i2c = busio.I2C(board.SCL, board.SDA)
sensor = Adafruit_AS726x(i2c)

now you can read the temperature property from the sensor:

Download: file
print('Temperature: {0}C'.format(sensor.temperature))

As long as you get a reasonable temperature (usually around 28 degrees C) you know your sensor is wired up correctly and working!

Below is a complete example that reads all color channels and prints them out as a graph in the REPL. Save this as on your board and open the REPL to see the output.

import time

import board
import busio

from adafruit_as726x import Adafruit_AS726x

#maximum value for sensor reading
max_val = 16000

#max number of characters in each graph
max_graph = 80

def graph_map(x):
    return min(int(x * max_graph / max_val), max_graph)

# Initialize I2C bus and sensor.
i2c = busio.I2C(board.SCL, board.SDA)
sensor = Adafruit_AS726x(i2c)

sensor.conversion_mode = sensor.MODE_2

while True:
    # Wait for data to be ready
    while not sensor.data_ready:

    #plot plot the data
    print("V: " + graph_map(sensor.violet)*'=')
    print("B: " + graph_map(*'=')
    print("G: " + graph_map(*'=')
    print("Y: " + graph_map(sensor.yellow)*'=')
    print("O: " + graph_map(*'=')
    print("R: " + graph_map(*'=')


Running the above code should something like this in your REPL:

This guide was first published on Mar 28, 2018. It was last updated on Mar 28, 2018. This page (Python & CircuitPython) was last updated on Dec 12, 2018.