Skip to content

Commit 46621d0

Browse files
authored
Merge pull request #2931 from adafruit/itsaSNAP-Esp32-S2-tft-color-detection-
ESP32-s2 tft color detection with itsaSNAP
2 parents 4b1479b + 5532a6e commit 46621d0

File tree

1 file changed

+80
-0
lines changed
  • ESP32-S2_TFT_Color_Detection

1 file changed

+80
-0
lines changed

ESP32-S2_TFT_Color_Detection/code.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 APD-9960 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+
from adafruit_apds9960.apds9960 import APDS9960
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+
21+
wifi.radio.connect(
22+
os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
23+
)
24+
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!")
25+
26+
# Initialize network pool and Adafruit IO HTTP
27+
pool = socketpool.SocketPool(wifi.radio)
28+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
29+
io = IO_HTTP(aio_username, aio_key, requests)
30+
31+
# Set up I2C connection and APDS9960 sensor
32+
i2c = busio.I2C(board.SCL, board.SDA)
33+
apds = APDS9960(i2c)
34+
apds.enable_proximity = True
35+
apds.enable_color = True
36+
37+
# Adafruit IO feed setup
38+
try:
39+
hue_feed = io.get_feed("hue")
40+
except AdafruitIO_RequestError:
41+
hue_feed = io.create_new_feed("hue")
42+
43+
44+
# Function to convert RGB to Hue
45+
def rgb_to_hue(r: int, g: int, b: int) -> float:
46+
r_norm = r / 255.0
47+
g_norm = g / 255.0
48+
b_norm = b / 255.0
49+
c_max = max(r_norm, g_norm, b_norm)
50+
c_min = min(r_norm, g_norm, b_norm)
51+
delta = c_max - c_min
52+
53+
if delta == 0:
54+
calculated_hue = 0
55+
elif c_max == r_norm:
56+
calculated_hue = (60 * ((g_norm - b_norm) / delta) + 360) % 360
57+
elif c_max == g_norm:
58+
calculated_hue = (60 * ((b_norm - r_norm) / delta) + 120) % 360
59+
elif c_max == b_norm:
60+
calculated_hue = (60 * ((r_norm - g_norm) / delta) + 240) % 360
61+
else:
62+
calculated_hue = 0 # Fallback case
63+
64+
return calculated_hue
65+
66+
67+
# Main loop
68+
while True:
69+
color_data = apds.color_data
70+
red_value, green_value, blue_value = color_data[0], color_data[1], color_data[2]
71+
hue_value = round(rgb_to_hue(red_value, green_value, blue_value))
72+
print(f"Detected Hue: {hue_value}")
73+
74+
try:
75+
io.send_data(hue_feed["key"], hue_value)
76+
print(f"Sent Hue value {hue_value} to Adafruit IO feed 'hue'")
77+
except AdafruitIO_RequestError as e:
78+
print(f"Error sending data: {e}")
79+
80+
time.sleep(3)

0 commit comments

Comments
 (0)