← Course overview
Week 7 of 12 · Sensing

Colour Sensing with the AS7341

Wire up a real spectral sensor, control your own lighting, and collect a dataset worth trusting.

~5 hrs Bench + data

Concept Eight numbers, not three

A simple RGB sensor reports three broad numbers. The AS7341 instead samples eight narrower wavelength bands — violet through red — giving a much more detailed “fingerprint” of what's in front of it. That detail is only useful if the lighting hitting the sensor is consistent: any stray ambient light shifts every one of the eight channels unpredictably, which is exactly why the AS7341 breakout has its own onboard white LED, controlled over the same I2C connection, that illuminates the Skittle consistently right before each reading rather than trusting the room's lighting. The NeoPixel strip wired up this week is a separate thing entirely — it plays no part in the sensor reading at all, and exists purely to give the person standing at the machine visual feedback (for example, a colour or animation showing what the machine just sorted). The sensor talks to the Pi over I2C, a shared two-wire bus where each device answers to its own address — useful to know when a sensor isn't responding at all.

Kit What you need this week

  • Adafruit AS7341 sensor (has its own onboard illumination LED)
  • NeoPixel LED strip (user-feedback indicator, not used for scanning)
  • Week 6 mechanism
  • Real Skittles, several of each colour
  • A quiet, dim corner of the room

Wiring Diagrams

Raspberry PiGPIO headerSDA / SCL / 3V3 / GNDAS7341 sensorI2C, pins 3 & 5own onboard LEDGPIO12 / 5V / GNDNeoPixel stripdata on pin 32user feedback onlyreads 8 channels8 valuesV I B C G Y O R
The Pi talks to the colour sensor over I2C — the sensor illuminates each Skittle with its own onboard LED — and drives the NeoPixel feedback strip separately — the two are wired independently and serve different jobs.
Raspberry Pi 40-pin header (top view)13V325V3GPIO2 (SDA) — SDA45V5GPIO3 (SCL) — SCL6GND7GPIO48GPIO14 (TXD)9GND10GPIO15 (RXD)11GPIO1712GPIO1813GPIO2714GND15GPIO2216GPIO23173V318GPIO2419GPIO10 (MOSI)20GND21GPIO9 (MISO)22GPIO2523GPIO11 (SCLK)24GPIO8 (CE0)25GND26GPIO7 (CE1)27ID_SD28ID_SC29GPIO530GND31GPIO632GPIO12 — NeoPixel data33GPIO1334GND35GPIO1936GPIO1637GPIO2638GPIO2039GND40GPIO21
powergroundused this weekunused GPIO
I2C for the sensor (whose own onboard LED handles illumination), and a separate GPIO pin for the NeoPixel feedback strip.

Build Step by step

Step 1

Wire it up

Connect the AS7341 over I2C (SDA/SCL/3V3/GND — a STEMMA QT cable makes this simple) and the NeoPixel strip to a PWM-capable GPIO pin. These are two independent circuits doing two different jobs — nothing about the sensor reading depends on the NeoPixel being wired at all.

Step 2

Confirm the sensor responds

Initialise it in Python and confirm no errors:

import board
from adafruit_as7341 import AS7341

i2c = board.I2C()
sensor = AS7341(i2c)
print("Sensor ready")
Step 3

Control the sensor's own illumination LED

The AS7341 breakout has a built-in white LED, controlled over the same I2C connection as the readings — not the NeoPixel. Turn it on to a fixed, known brightness right before each reading, and off otherwise; this consistency is the whole point:

sensor.led_current = 10
sensor.led = True
import time; time.sleep(0.25)  # let it settle
# ... take a reading here ...
sensor.led = False
Step 4

Read the eight channels

Print all eight values with nothing in front of the sensor first, as a baseline:

print(sensor.channel_415nm, sensor.channel_445nm, sensor.channel_480nm,
      sensor.channel_515nm, sensor.channel_555nm, sensor.channel_590nm,
      sensor.channel_630nm, sensor.channel_680nm)
Step 5

Read real Skittles

Hold a Skittle of each colour to the sensor a few times and print the readings. Notice how, for example, yellow lights up the green/yellow/orange channels strongly while purple stays low across almost everything.

Step 6

Log to a file

Write a script that appends one row per scan — label, timestamp, then the 8 channel values — to a tab-separated file, matching the column order Violet, Indigo, Blue, Cyan, Green, Yellow, Orange, Red so it's ready for next week's classifier.

Step 7

Collect your dataset

Scan at least 5 Skittles per colour, all under the same lighting setup and enclosure — consistency here matters more than volume.

Step 8

Give the NeoPixel a job

Separately from any of the above, write a tiny script that lights the NeoPixel a different colour depending on a value you pass in — this is the pattern the main program will later use to show the operator what colour was just sorted, entirely independent of the sensor reading itself.

Make it yours
  • Scan the same Skittle with the room lights on, then off, and see how much the numbers shift — a direct, visible reason why the sensor's own illumination control matters.
  • Add a simple cardboard shroud around the sensor to block stray light and compare consistency before and after.
  • Design your own NeoPixel feedback pattern — a solid colour, a pulse, a brief animation — it's pure user-experience polish and entirely up to you.

If it breaks Troubleshooting

Sensor not found on I2CCheck wiring (especially that it's on 3.3V, not 5V) and try an I2C address scan to confirm it's visible on the bus at all.
Readings are all near zeroThe sensor's own illumination LED probably isn't turning on (check sensor.led = True is actually being called), or the Skittle isn't close enough to the sensor.
“Identical” scans vary wildlyThe Skittle likely isn't seated the same way each time, or ambient light is leaking in — check the enclosure/shroud. The NeoPixel being on or off makes no difference here, since it isn't part of the sensing path.
NeoPixel doesn't light up but the sensor works fineExpected to be unrelated — they're two separate circuits. Check the NeoPixel's own wiring (5V/GND/GPIO12) and that it's a PWM-capable pin.
This week's checkpointYou hold your own labelled dataset — Violet…Red readings for at least 5 Skittles per colour — saved in the same column layout the classifier expects.
Next week: Turning that dataset into a working colour classifier.