Main Content

Measure Frequency and Duty Cycle Using TCS230 Color Sensor and Arduino Hardware

This example shows how to use the Simulink® Support Package for Arduino® Hardware to measure frequency and duty cycle of the red color component of an object using a TCS230 color sensor and Arduino hardware.

Supported Arduino Boards:

  • Arduino Due

  • Arduino MKR Zero

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino MKR 1000

  • Arduino MKR WIFI 1010

  • Arduino Leonardo

  • Arduino Micro

  • Arduino Nano 3.0

  • Arduino Nano 33 IoT

  • Arduino Uno

  • Arduino Robot Control board

  • Arduino Robot Motor board


In this example, the TCS230 color sensor (RGB color sensor) converts the red color component of an object detected by the photodiode into a square wave by using the color-to-frequency converter. The frequency of the square wave is directly proportional to the intensity of the red colored component detected by the photodiode. The photodiode is mounted on the TCS230 color sensor.

The TCS230 color sensor reads the RGB color based on the combination of the S2 and S3 pins. The scaled frequency percentage output from the color sensor changes according to the combination of the S0 and S1 pins. In this example, the color sensor pins are configured to detect the red color component of the object placed in front of the sensor with a scaled frequency output of 100%.

For more information regarding the TCS230 color sensor, refer to its datasheet.


We recommend that you complete the Get Started with Arduino Hardware and Communicating with Arduino Hardware before starting on this examples.

Required Hardware

  • This example uses the Arduino Due board. However, you can use any Arduino board from the suggested list of boards.

  • TCS230 color sensor

  • Connecting wires

  • Red colored object

Task 1: Hardware Setup

Connect the TCS230 color sensor to the Arduino board using these pin connections.

   TCS230 Color Sensor Pin Number   |   Arduino Board Pin Number
             VCC                    |        3.3V or 5V
             GND                    |           GND
             S0                     |           36
             S1                     |           38
             S2                     |           40
             S3                     |           42
             OUT                    |           2

Task 2: Configure Simulink Model and Calibrate Parameters

This support package provides a preconfigured model to measure the frequency and duty cycle of the red colored object. The output frequency of the TCS230 color sensor is scaled to 100%.

Open the arduino_tcs230_frequency_dutycycle_measurement Simulink model.

1. Configure the Input capture pin number parameter on the Input Capture block. The Out pin of the TCS230 color sensor is connected to the input capture pin of the Arduino board. Ensure to enter the same pin number in the Input capture pin number parameter. For more information on setting the input capture pin on supported Arduino boards, click View pin map on the Block Parameters dialog box.

2. Double-click the Initialize panel in the model to open the Initialize Function subsystem.

The Setup area is divided into two parts, Frequency Scaling Configuration and Photodiode Type Configuration. Use this table to connect the TCS230 sensor pins to the specified pins on the Arduino board to detect the red colored object with an output frequency scaled to 100%. Configure the same combination in the Pin number parameter of each TCS230 color sensor pin. Verify the value in the Constant Value parameter in the Constant block corresponding to each pin on the TCS230 color sensor.

   TCS230 Color Sensor Pin   |   Arduino Board Pin   |   Value Set in Constant Block
            S0               |          36           |               High
            S1               |          38           |               High
            S2               |          40           |               Low
            S3               |          42           |               Low

Task 3: Run Simulink Model

1. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

2. Place the red colored object in front of the TCS230 color sensor. Observe how values displayed in the Frequency parameter change.

Note: The TCS230 color sensor always outputs a square wave. For more information, refer to the TCS230 color sensor datasheet.

Other Things to Try

  • Configure the TCS230 color sensor pin values to detect green and blue color components with varying output frequency scales. You will have to map the color values as per the scaling factor you have chosen for the output frequency.

  • Observe how the values displayed in the Frequency parameter change for objects in a lighter or darker shade of red.

Related Links

For more information on Initialize Function subsystem, refer to Initialize Function.