This experiment demonstrates how to use a color sensor with a Raspberry Pi Pico to detect and display RGB color values on the console. The TCS3200 or TCS230 color sensor is used to measure light intensity for red, green, and blue wavelengths. By interpreting these values, we can identify the detected color.
Scope:
This experiment is designed to:
Understand the working principles of a color sensor.
Demonstrate how to interface a color sensor with a microcontroller (Raspberry Pi Pico).
Write code in both MicroPython and Embedded C to read RGB values and display them.
Prerequisite:
3.1. Hardware:
Pico microcontroller
Color sensor
Breadboard
Jumper wires
USB cable (to connect the computer)
3 .2. Software:
Thonny Software(For micro python)
MicroPython firmware installed on Raspberry Pi Pico
ubuntu Terminal(For embedded C)
CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.
Connection Details:
S0 to GPIO 0 on the Pico
S1 to GPIO 1 on the Pico
S2 to GPIO 2 on the Pico
S3 to GPIO 3 on the Pico
OUT to GPIO 4 on the Pico (input pin to read frequency)
OE to GPIO 5 on the Pico (output enable, active low)
GND to GND
VCC to 3.3V or 5V (depending on sensor requirements)
UART Serial Console: Use a micro-USB cable to connect the Pico to the PC, then open PuTTY and select the correct COM port and baud rate (115200).
Working Principle:
The TCS3200 color sensor can detect a wide variety of colors based on their wavelength. This sensor is specially useful for color recognition projects such as color matching, color sorting, test strip reading and much more.
The TCS3200 color sensor – shown in the figure below – uses a TAOS TCS3200 RGB sensor chip to detect color. It also contains four white LEDs that light up the object in front of it.

The TCS3200 has an array of photodiodes with 4 different filters. A photodiode is simply a semiconductor device that converts light into current. The sensor has:
16 photodiodes with red filter – sensitive to red wavelength
16 photodiodes with green filter – sensitive to green wavelength
16 photodiodes with blue filter – sensitive to blue wavelength
16 photodiodes without filter
If you take a closer look at the TCS3200 chip you can see the different filters.

By selectively choosing the photodiode filter’s readings, you’re able to detect the intensity of the different colors. The sensor has a current-to-frequency converter that converts the photodiodes’ readings into a square wave with a frequency that is proportional to the light intensity of the chosen color. This frequency is then, read by the Arduino – this is shown in the figure below.

Pinout
Here’s the sensor pinout:

Pin Name | I/O | Description |
GND (4) | Power supply ground | |
OE (3) | I | Enable for output frequency (active low) |
OUT (6) | O | Output frequency |
S0, S1(1,2) | I | Output frequency scaling selection inputs |
S2, S3(7,8) | I | Photodiode type selection inputs |
VDD(5) | Voltage supply |
Filter selection
To select the color read by the photodiode, you use the control pins S2 and S3. As the photodiodes are connected in parallel, setting the S2 and S3 LOW and HIGH in different combinations allows you to select different photodidodes. Take a look at the table below:
Photodiode type | S2 | S3 |
Red | LOW | LOW |
Blue | LOW | HIGH |
No filter (clear) | HIGH | LOW |
Green | HIGH | HIGH |
Frequency scaling
Pins S0 and S1 are used for scaling the output frequency. It can be scaled to the following preset values: 100%, 20% or 2%. Scaling the output frequency is useful to optimize the sensor readings for various frequency counters or microcontrollers. Take a look at the table below:
Output frequency scaling | S0 | S1 |
Power down | L | L |
2% | L | H |
20% | H | L |
100% | H | H |
For the Arduino, it is common to use a frequency scaling of 20%. So, you set the S0 pin to HIGH and the S1 pin to LOW.
Application code:
MicroPython:
from machine import Pin, time_pulse_us
import time
# Define color sensor pins
S0 = Pin(0, Pin.OUT)
S1 = Pin(1, Pin.OUT)
S2 = Pin(2, Pin.OUT)
S3 = Pin(3, Pin.OUT)
sensor_out = Pin(4, Pin.IN)
OE = Pin(5, Pin.OUT)
# Set frequency scaling to 20%
S0.value(1)
S1.value(0)
OE.value(0) # Enable the sensor (OE is active low)
def get_color_pw(s2, s3):
S2.value(s2)
S3.value(s3)
time.sleep_ms(200)
pulse_width = time_pulse_us(sensor_out, 0)
return pulse_width
def read_color_values():
red_pw = get_color_pw(0, 0)
green_pw = get_color_pw(1, 1)
blue_pw = get_color_pw(0, 1)
print("Red =", red_pw, "Green =", green_pw, "Blue =", blue_pw)
while True:
read_color_values()
time.sleep(1)
Embedded C:
#include <stdio.h>
#include "pico/stdlib.h"
#define S0_PIN 0
#define S1_PIN 1
#define S2_PIN 2
#define S3_PIN 3
#define SENSOR_OUT_PIN 4
#define OE_PIN 5
void setup_pins() {
gpio_init(S0_PIN); gpio_set_dir(S0_PIN, GPIO_OUT);
gpio_init(S1_PIN); gpio_set_dir(S1_PIN, GPIO_OUT);
gpio_init(S2_PIN); gpio_set_dir(S2_PIN, GPIO_OUT);
gpio_init(S3_PIN); gpio_set_dir(S3_PIN, GPIO_OUT);
gpio_init(SENSOR_OUT_PIN); gpio_set_dir(SENSOR_OUT_PIN, GPIO_IN);
gpio_init(OE_PIN); gpio_set_dir(OE_PIN, GPIO_OUT);
gpio_put(OE_PIN, 0);
gpio_put(S0_PIN, 1);
gpio_put(S1_PIN, 0);
}
uint32_t get_color_pw(int s2, int s3) {
gpio_put(S2_PIN, s2);
gpio_put(S3_PIN, s3);
sleep_ms(200);
uint32_t pulse_width = 0;
absolute_time_t start = get_absolute_time();
while (!gpio_get(SENSOR_OUT_PIN) && absolute_time_diff_us(start, get_absolute_time()) < 1000000) {
pulse_width++;
}
return pulse_width;
}
void read_color_values() {
int redPW = get_color_pw(0, 0);
int greenPW = get_color_pw(1, 1);
int bluePW = get_color_pw(0, 1);
printf("Red = %d, Green = %d, Blue = %d\n", redPW, greenPW, bluePW);
}
int main() {
stdio_init_all();
setup_pins();
while (1) {
read_color_values();
sleep_ms(1000);
}
}
Hardware Image:

Output:
When executed, the program prints the intensity values of red, green, and blue to the console. The output looks like:

Video Demonstration:
Appendix:
Common CMakeLists:
cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
project(pico_experiments C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()
# Initialize the SDK
pico_sdk_init()
add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that arent called
)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-maybe-uninitialized)
endif()
# Hardware-specific examples in subdirectories:
add_subdirectory(expt_10_LCD)
CMakeLists:
cmake_minimum_required(VERSION 3.13)
include(pico_sdk_import.cmake)
project(Color_Sensor)
pico_sdk_init()
add_executable(Color_Sensor color_sensor.c)
target_link_libraries(Color_Sensor pico_stdlib)
pico_add_extra_outputs(Color_Sensor)
References:
| # | Topic | Date & time | Action |
|---|
0% Completed (0/1)