Skip to content

Commit f0bf67e

Browse files
authored
Merge pull request #2934 from adafruit/ESP32-S2-TFT_AS77341_Color_Detection
Added code.py for AS7341 Color detection
2 parents 65e8e79 + eec86c9 commit f0bf67e

File tree

1 file changed

+121
-0
lines changed
  • ESP32-S2_TFT_AS7341_Color_Detection

1 file changed

+121
-0
lines changed
+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# SPDX-FileCopyrightText: 2024 Trevor Beaton for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
'''This is a program for a color detection project using the AS7341 and Feather ESP32-S2 & S3'''
5+
6+
import os
7+
import ssl
8+
import time
9+
import wifi
10+
import board
11+
import busio
12+
import socketpool
13+
import adafruit_requests
14+
import adafruit_as7341
15+
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
16+
17+
# WiFi and Adafruit IO setup
18+
aio_username = os.getenv("aio_username")
19+
aio_key = os.getenv("aio_key")
20+
wifi.radio.connect(
21+
os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
22+
)
23+
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!")
24+
25+
# Initialize network pool and Adafruit IO HTTP
26+
pool = socketpool.SocketPool(wifi.radio)
27+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
28+
io = IO_HTTP(aio_username, aio_key, requests)
29+
30+
# Set up I2C connection and AS7341 sensor
31+
i2c = busio.I2C(board.SCL, board.SDA)
32+
sensor = adafruit_as7341.AS7341(i2c)
33+
34+
# Configure the sensor with correct gain setting
35+
sensor.gain = adafruit_as7341.Gain.GAIN_64X
36+
sensor.atime = 100
37+
sensor.astep = 999
38+
39+
# Adafruit IO feed setup
40+
try:
41+
hue_feed = io.get_feed("hue")
42+
except AdafruitIO_RequestError:
43+
hue_feed = io.create_new_feed("hue")
44+
45+
def spectral_to_rgb(ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8):
46+
# Normalize the channel readings
47+
total = ch1 + ch2 + ch3 + ch4 + ch5 + ch6 + ch7 + ch8
48+
if total == 0:
49+
total = 1
50+
ch1 /= total
51+
ch2 /= total
52+
ch3 /= total
53+
ch4 /= total
54+
ch5 /= total
55+
ch6 /= total
56+
ch7 /= total
57+
ch8 /= total
58+
59+
# Map channels to RGB components
60+
red = ch6 + ch7 + ch8 # Orange, Red, Deep Red
61+
green = ch4 + ch5 # Green, Yellow-Green
62+
blue = ch1 + ch2 + ch3 # Violet, Blue, Blue-Green
63+
64+
# Normalize RGB components
65+
rgb_total = red + green + blue
66+
if rgb_total == 0:
67+
rgb_total = 1
68+
red /= rgb_total
69+
green /= rgb_total
70+
blue /= rgb_total
71+
72+
# Scale to 0-255 range
73+
red = int(min(max(red * 255, 0), 255))
74+
green = int(min(max(green * 255, 0), 255))
75+
blue = int(min(max(blue * 255, 0), 255))
76+
return red, green, blue
77+
78+
def rgb_to_hue(r: int, g: int, b: int) -> float:
79+
r_norm = r / 255.0
80+
g_norm = g / 255.0
81+
b_norm = b / 255.0
82+
c_max = max(r_norm, g_norm, b_norm)
83+
c_min = min(r_norm, g_norm, b_norm)
84+
delta = c_max - c_min
85+
86+
if delta == 0:
87+
calculated_hue = 0
88+
elif c_max == r_norm:
89+
calculated_hue = (60 * ((g_norm - b_norm) / delta) + 360) % 360
90+
elif c_max == g_norm:
91+
calculated_hue = (60 * ((b_norm - r_norm) / delta) + 120) % 360
92+
elif c_max == b_norm:
93+
calculated_hue = (60 * ((r_norm - g_norm) / delta) + 240) % 360
94+
else:
95+
calculated_hue = 0
96+
return calculated_hue
97+
98+
# Main loop
99+
while True:
100+
# Read all spectral channels
101+
channel_readings = sensor.all_channels
102+
# Extract individual channel readings
103+
f1 = channel_readings[0]
104+
f2 = channel_readings[1]
105+
f3 = channel_readings[2]
106+
f4 = channel_readings[3]
107+
f5 = channel_readings[4]
108+
f6 = channel_readings[5]
109+
f7 = channel_readings[6]
110+
f8 = channel_readings[7]
111+
112+
red_value, green_value, blue_value = spectral_to_rgb(f1, f2, f3, f4, f5, f6, f7, f8)
113+
hue_value = round(rgb_to_hue(red_value, green_value, blue_value))
114+
print(f"Detected Hue: {hue_value}")
115+
116+
try:
117+
io.send_data(hue_feed["key"], hue_value)
118+
print(f"Sent Hue value {hue_value} to Adafruit IO feed 'hue'")
119+
except AdafruitIO_RequestError as e:
120+
print(f"Error sending data: {e}")
121+
time.sleep(4)

0 commit comments

Comments
 (0)