-
Notifications
You must be signed in to change notification settings - Fork 1
/
whadda_you_see.py
292 lines (223 loc) · 12 KB
/
whadda_you_see.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/python3
"""
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@ @@@. @@@ @@@. @@@@ @@@@ @@@@@@@@@ @@@@@@@@@@@@@. .@@@@@@@@@@@@@ @@@@@@@@@
@@@@@%%@@@%%@@@%%@@@@@ @@@@ @@@ @@@@ @@@@ @@@@ @@@@@@@@@@ @@@@@@@@@@@@@@ @@@@@@@@@@@@@@ @@@@@@@@@
@@@@@ @@@ @@@ @@@@@ @@@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@@ @@@@@ @@@@@ @@@@@ @@@@ @@@@ @@@@
@@@@@ @@@@@ @@@@ @@@@ @@@@ @@@@@@@@@@@@ @@@@ @@@@ @@@@@ @@@@@ @@@@@ @@@@ @@@@ @@@@
@@@@@ @ @ @@@@@ @@@@ @@@@ @@@@ @@@@@@@@@@@@ @@@@@@@@@@@ @@@@@ @@@@@ @@@@@ @@@@ @@@@@@@@@@@
@@@@@ @@@@@ @@@@###@@@@@##@@@@@ @@@@ @@@@ @@@@@@@@@@@@ #@@@@@###@@@@@ ##@@@@@###@@@@ @@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@ @@@@ @@@@ @@@@ @@@@@ @@@@@@@@@@@@@@ @@@@@@@@@@@@@@ @@@@ @@@@
@@@@@@@@@@@@@@@@@@@@@@
Project name: Whadda you see? (Raspberry pi edition)
Colour sensor Raspberry Pi Demo
Description:
This project uses a colour sensor to determine the main colour of an object.
The Hue color parameter (a number that represents the "pure colour") is calculated based on the RGB-values that are read from the sensor.
From the Hue parameter it is possible to determine which basic color is presented to the sensor.
If the button (connected to GPIO pin 20) is pressed, the corresponding colour is printed on the terminal and can be heard using the on-board audio (using a Text-To-Speech program)
More information on the working principles can be found on whadda.com
Level of difficulty: Intermediate
Parts required:
- Raspberry Pi 3B/B+ or 4B or equivalent (e.g. Whadda PI4SET)
- 0.96" or 1.3" OLED screen (e.g. Whadda INSERT WHADDA CODE/VMA437 1.3 INCH OLED SCREEN FOR ARDUINO or INSERT WHADDA CODE/VMA438 0.96 INCH OLED SCREEN WITH I2C FOR ARDUINO)
- TCS3200 color sensor module (e.g. Whadda INSERT WHADDA CODE/VMA325 COLOR SENSOR TCS3200 MODULE)
See illustrated wiring diagram for the necessary connections, available on whadda.com
For more information, check out whadda.com
Required Python modules:
- RPi.GPIO (should be installed by default, if not use folowing command to install: sudo apt install python-rpi.gpio python3-rpi.gpio)
- Pillow (command to install: pip3 install Pillow)
- adafruit_ssd1306 (command to install: pip3 install adafruit-circuitpython-ssd1306)
Code inspired by how to Mechatronics (https://www.electronicshub.org/raspberry-pi-color-sensor-tutorial/)
Author: Midas Gossye
(c) 2020 Whadda, premium makers brand by Velleman
"""
# Import necessary modules
import RPi.GPIO as GPIO
from time import sleep
import time
import colorsys
import os
from PIL import Image, ImageDraw, ImageFont
import argparse
# Parse the command arguments
parser = argparse.ArgumentParser(prog='whadda_you_see', description='Determine and display colour from TCS3200 colour sensor')
parser.add_argument('--OLED', help='Add if OLED screen is installed', action='store_true', default=False)
args = parser.parse_args()
OLED_SCREEN_INSTALLED = args.OLED
###############################################################################################################################
# Import the OLED modules if an OLED screen is installed
if OLED_SCREEN_INSTALLED:
import adafruit_ssd1306
from board import SCL, SDA
import busio
########################################################
GPIO.setmode(GPIO.BCM) # Use processor pin numbering system
NUM_CYCLES = 10 # number of measurements to perform to determine colour
# pin connections
S0 = 5 # S0 colour sensor
S1 = 6 # S1 colour sensor
S2 = 13 # 20 colour sensor
S3 = 19 # S3 colour sensor
Out = 26 # Out colour sensor
Button = 20 # Button
###############################
if OLED_SCREEN_INSTALLED:
i2c = busio.I2C(SCL, SDA) # Initialize the I2C bus for the OLED screen
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c) # Initialize OLED screen (128x64 pixels)
oled.fill(0) # fill the screen with 0's (clear screen)
# setup GPIO pins (inputs/outputs)
GPIO.setup(S0, GPIO.OUT)
GPIO.setup(S1, GPIO.OUT)
GPIO.setup(S2, GPIO.OUT)
GPIO.setup(S3, GPIO.OUT)
GPIO.setup(Out, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(Button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#####################################################
# pin reading sequence for Red, Green, Blue measurements [S2, S3]
pin_reading_seq = [[GPIO.LOW, GPIO.LOW], [GPIO.HIGH, GPIO.HIGH], [GPIO.LOW, GPIO.HIGH], [GPIO.HIGH, GPIO.LOW]]
##############################################################################################################
RGBW = [0, 0, 0, 0] # Initialize empty array to hold Red, Green, Blue & White colour sensor readings
# Set colour frequency scaling to 2% / 12 kHz
GPIO.output(S0, GPIO.LOW)
GPIO.output(S1, GPIO.HIGH)
colour_names = ['Red', 'Green', 'Blue', 'White']
if OLED_SCREEN_INSTALLED:
# Show Raspberry Pi Logo on OLED screen for 1 sec
image = Image.open("RPI.bmp")
oled.image(image)
oled.show()
sleep(1)
##################################################
oled.fill(0) # fill the screen with 0's (clear screen)
# Show Whadda Logo on OLED screen for 2 secs
image = Image.open("WHADDA.bmp")
oled.image(image)
oled.show()
sleep(2)
##################################################
# Open a new blank image and draw object to later draw tex and shapes on
image = Image.new("1", (oled.width, oled.height))
draw = ImageDraw.Draw(image)
#########################################################################
oled.fill(0) # fill the screen with 0's (clear screen)
# Load a font in 2 different sizes.
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 10)
font2 = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 22)
# Draw the text that doesn't need updating
draw.text((0, 0), "Red: ", font=font, fill=255, align="right")
draw.text((0, 10), "Green: ", font=font, fill=255, align="right")
draw.text((0, 20), "Blue: ", font=font, fill=255, align="right")
draw.text((70, 0), "Hue: ", font=font, fill=255, align="right")
draw.text((70, 10), "Sat.: ", font=font, fill=255, align="right")
draw.text((70, 20), "Value: ", font=font, fill=255, align="right")
##################################################################
draw.line((64, 0, 64, 32), width=1, fill=255) # draw vertical seperation line
draw.line((0, 32, 127, 32), width=1, fill=255) # draw horizontal seperation line
# Update image on OLED
oled.image(image)
oled.show()
######################
"""
Function descrition:
Reads the Red, Green, Blue & White colour components from the TCS3200 colour sensor,
converts the RGB colour into the HSV colour space, determines the colour name from the Hue value,
and displays all of the above information on the OLED screen.
Arguments:
None
Returns:
-color_str (String with the colour name)
-HSV_Val (list with Hue, Saturation and Value components respectively)
"""
def read_color():
for idx, pin_states in enumerate(pin_reading_seq):
# Select colour filter
GPIO.output(S2, pin_states[0])
GPIO.output(S3, pin_states[1])
##############################
time.sleep(0.01)
start = time.time() # start timing measurement to time pulse length
# Wait until the NUM_cycles pulses have been counted
for impulse_count in range(NUM_CYCLES):
GPIO.wait_for_edge(Out, GPIO.FALLING)
####################################################
duration = time.time() - start # calculate duration of pulses
RGBW[idx] = ((NUM_CYCLES / duration) / 10000) *255 # compute Red, Green and White colour components and scale to 0-255
# Scale Red, Green & Blue components with respect to the White/Brightness reading
RGBW[0] = (RGBW[0] /RGBW[3])
RGBW[1] = (RGBW[1] / RGBW[3])
RGBW[2] = (RGBW[2] / RGBW[3])
#################################################################################
HSV_val = colorsys.rgb_to_hsv(RGBW[0], RGBW[1], RGBW[2]) # Convert RGB colour readings into HSV colour space
color_str = ""
# Convert Red, Green & Blue values to rounded percentages
R_perc = round(RGBW[0]*100)
G_perc = round(RGBW[1]*100)
B_perc = round(RGBW[2]*100)
#########################################################
hue_deg = round(HSV_val[0]*360) # Convert Hue to rounded degrees
# Convert value and saturation to rounded percentages
val_perc = round(HSV_val[1]*100)
sat_perc = round(HSV_val[2]*100)
#####################################################
if OLED_SCREEN_INSTALLED:
# draw "black" rectangle to clear screen space for updated numbers
draw.rectangle((34, 0, 63, 30), fill=0)
draw.rectangle((100, 0, 127, 30), fill=0)
###################################################################
# Put updated Red, Green and Blue percentages on screen
draw.text((30, 0), "{:3d} %".format(R_perc), font=font, fill=255, align="right")
draw.text((30, 10), "{:3d} %".format(G_perc), font=font, fill=255, align="right")
draw.text((30, 20), "{:3d} %".format(B_perc), font=font, fill=255, align="right")
#################################################################################
# Put updated Hue, value and saturation numbers on screen
draw.text((100, 0), "{:3d} °".format(hue_deg), font=font, fill=255, align="right")
draw.text((100, 10), "{:3d} %".format(val_perc), font=font, fill=255, align="right")
draw.text((100, 20), "{:3d} %".format(sat_perc), font=font, fill=255, align="right")
####################################################################################
# Determine colour based on hue parameter
if sat_perc < 35:
color_str = "Black/Dark"
elif hue_deg > 357 or hue_deg < 10:
color_str = "ORANGE"
elif hue_deg > 10 and hue_deg < 40:
color_str = "YELLOW"
elif hue_deg > 45 and hue_deg < 160:
color_str = "GREEN"
elif hue_deg > 210 and hue_deg < 270:
color_str = "BLUE"
elif hue_deg > 270 and hue_deg < 330:
color_str ="PURPLE"
elif hue_deg > 340 and hue_deg < 356:
color_str = "RED"
else:
color_str = "No valid color detected"
#########################################
if OLED_SCREEN_INSTALLED:
draw.rectangle((0, 34, 128, 60), fill=0) # clear bottom part of screen by drawing "black" rectangle
# if a valid color is detected...
if color_str != "No valid color detected" and color_str != "Black/Dark":
draw.rectangle((0, 34, 128, 60), fill=255) # Draw a filled rectangle as background
w, h = draw.textsize(color_str, font=font2) # Calculate pixel width and height of color name
draw.text(((128-w)/2, 33), color_str, font=font2, fill=0) # Put color name on screen with inversed text
# Update OLED screen with new image
oled.image(image)
oled.show()
###################################
return color_str, HSV_val
print("Whadda you see?")
print("===============")
print("\nPush button to get colour info\n")
while True:
color_str, HSV_val = read_color() # Read and display colour name and values
# If button is pressed...
if GPIO.input(Button) == GPIO.LOW:
# Print colour details on terminal
print("Detected color: {}".format(color_str))
print("Hue:", HSV_val[0]*360)
print("\n")
##################################
# Using the Pico Text-To-Speech program SAY the colour
os.system('pico2wave -w tts.wav "{}" && aplay tts.wav'.format(color_str))
print("=================")
print("\n")