This code example demonstrates the use of the ADC HAL driver to perform voltage measurements. In this example, the driver is configured to sample the input voltage periodically and display the sampled voltage on the UART terminal. By default, the ADC is configured to use one channel in single-ended mode.
- ModusToolbox™ software v3.0 or later (tested with v3.1)
- Programming language: C
- Associated parts: TRAVEO™ T2G Body Entry MCU
- GNU Arm® embedded compiler v11.3 (
GCC_ARM) - Default value ofTOOLCHAIN - Arm® compiler v6.16 (
ARM) - IAR C/C++ compiler v9.30.1 (
IAR)
- TRAVEO™ T2G body entry evaluation kit (
KIT_T2G-B-E_LITE) - Default value ofTARGET
This example uses the board's default configuration. See the kit user guide to ensure that the board is configured correctly.
Install a terminal emulator if you don't have one. Instructions in this document use Tera Term.
Create the project and open it using one of the following:
In Eclipse IDE for ModusToolbox™ software
-
Click the New Application link in the Quick Panel (or, use File > New > ModusToolbox™ Application). This launches the Project Creator tool.
-
Pick a kit supported by the code example from the list shown in the Project Creator - Choose Board Support Package (BSP) dialog.
When you select a supported kit, the example is reconfigured automatically to work with the kit. To work with a different supported kit later, use the Library Manager to choose the BSP for the supported kit. You can use the Library Manager to select or update the BSP and firmware libraries used in this application. To access the Library Manager, click the link from the Quick Panel.
You can also just start the application creation process again and select a different kit.
If you want to use the application for a kit not listed here, you may need to update the source files. If the kit does not have the required resources, the application may not work.
-
In the Project Creator - Select Application dialog, choose the example by enabling the checkbox.
-
(Optional) Change the suggested New Application Name.
-
The Application(s) Root Path defaults to the Eclipse workspace which is usually the desired location for the application. If you want to store the application in a different location, you can change the Application(s) Root Path value. Applications that share libraries should be in the same root path.
-
Click Create to complete the application creation process.
For more details, see the Eclipse IDE for ModusToolbox™ software user guide (locally available at {ModusToolbox™ software install directory}/docs_{version}/mt_ide_user_guide.pdf).
In command-line interface (CLI)
ModusToolbox™ software provides the Project Creator as both a GUI tool and the command line tool, "project-creator-cli". The CLI tool can be used to create applications from a CLI terminal or from within batch files or shell scripts. This tool is available in the {ModusToolbox™ software install directory}/tools_{version}/project-creator/ directory.
Use a CLI terminal to invoke the "project-creator-cli" tool. On Windows, use the command line "modus-shell" program provided in the ModusToolbox™ software installation instead of a standard Windows command-line application. This shell provides access to all ModusToolbox™ software tools. You can access it by typing modus-shell in the search box in the Windows menu. In Linux and macOS, you can use any terminal application.
The "project-creator-cli" tool has the following arguments:
| Argument | Description | Required/optional |
|---|---|---|
--board-id |
Defined in the <id> field of the BSP manifest |
Required |
--app-id |
Defined in the <id> field of the CE manifest |
Required |
--target-dir |
Specify the directory in which the application is to be created if you prefer not to use the default current working directory | Optional |
--user-app-name |
Specify the name of the application if you prefer to have a name other than the example's default name | Optional |
The following example will clone the "mtb-t2g-lite-example-adc-basic" application with the desired name "ADC_basic" configured for the KIT_T2G-B-E_LITE BSP into the specified working directory, C:/mtb_projects:
project-creator-cli --board-id KIT_T2G-B-E_LITE --app-id mtb-t2g-lite-example-adc-basic --user-app-name ADC_basic --target-dir "C:/mtb_projects"
Note: The project-creator-cli tool uses the git clone and make getlibs commands to fetch the repository and import the required libraries. For details, see the "Project creator tools" section of the ModusToolbox™ software user guide (locally available at {ModusToolbox™ software install directory}/docs_{version}/mtb_user_guide.pdf).
To work with a different supported kit later, use the Library Manager to choose the BSP for the supported kit. You can invoke the Library Manager GUI tool from the terminal using make modlibs command or use the Library Manager CLI tool "library-manager-cli" to change the BSP.
The "library-manager-cli" tool has the following arguments:
| Argument | Description | Required/optional |
|---|---|---|
--add-bsp-name |
Name of the BSP that should be added to the application | Required |
--set-active-bsp |
Name of the BSP that should be as active BSP for the application | Required |
--add-bsp-version |
Specify the version of the BSP that should be added to the application if you do not wish to use the latest from manifest | Optional |
--add-bsp-location |
Specify the location of the BSP (local/shared) if you prefer to add the BSP in a shared path | Optional |
In third-party IDEs
Use one of the following options:
-
Use the standalone Project Creator tool:
-
Launch Project Creator from the Windows Start menu or from {ModusToolbox™ software install directory}/tools_{version}/project-creator/project-creator.exe.
-
In the initial Choose Board Support Package screen, select the BSP, and click Next.
-
In the Select Application screen, select the appropriate IDE from the Target IDE drop-down menu.
-
Click Create and follow the instructions printed in the bottom pane to import or open the exported project in the respective IDE.
-
-
Use command-line interface (CLI):
-
Follow the instructions from the In command-line interface (CLI) section to create the application, and then import the libraries using the
make getlibscommand. -
Export the application to a supported IDE using the
make <ide>command. -
Follow the instructions displayed in the terminal to create or import the application as an IDE project.
-
For a list of supported IDEs and more details, see the "Exporting to IDEs" section of the ModusToolbox™ software user guide (locally available at {ModusToolbox™ software install directory}/docs_{version}/mtb_user_guide.pdf).
-
Connect the board to your PC using the provided USB cable through the KitProg3 USB connector.
-
Open a terminal program and select the KitProg3 COM port. Set the serial port parameters to 8N1 and 115200 baud.
-
Program the board using one of the following:
Using Eclipse IDE for ModusToolbox™ software
-
Select the application project in the Project Explorer.
-
In the Quick Panel, scroll down, and click <Application Name> Program (KitProg3_MiniProg4).
Using CLI
From the terminal, execute the
make programcommand to build and program the application using the default toolchain to the default target. The default toolchain and target are specified in the application's Makefile but you can override those values manually:make program TOOLCHAIN=<toolchain>Example:
make program TOOLCHAIN=GCC_ARM -
-
After programming, the application starts automatically. Ensure that input voltages are provided at analog input pin P6_6. This pin is connected to the potentiometer.
-
Rotate the potentiometer to change the ADC input voltage.
-
Confirm that "ADC using HAL" and the input voltage are displayed on the UART terminal.
Figure 1. Terminal output with single-channel configuration
You can debug the example to step through the code. In the IDE, use the <Application Name> Debug (KitProg3_MiniProg4) configuration in the Quick Panel. For details, see the "Program and debug" section in the Eclipse IDE for ModusToolbox™ software user guide.
Note: (Only while debugging) On the debugging CPU, some code in main() may execute before the debugger halts at the beginning of main(). This means that some code executes twice – once before the debugger stops execution, and again after the debugger resets the program counter to the beginning of main(). See KBA231071 to learn about this and for the workaround.
In this example, the ADC is configured with 12-bit resolution; by default, one channel with 0-VDDA range is configured using the HAL. The input voltage is sampled every 200 ms by calling cyhal_adc_read_uv; the measured input voltage is displayed on the UART terminal.
This code example uses a delay of 200 ms between reads for demonstration purpose to print the sampled results on the UART terminal. To achieve sampling at higher rates, remove the delay and use the cyhal_adc_set_sample_rate function to configure the number of samples per second.
TRAVEO™ T2G HAL currently restricts mapping of any GPIO pins as input to the ADC. Only pins that have direct connection to the ADC can be used as inputs to the ADC. On all supported boards, P6_x are the preferred GPIO pins as the input for the ADC because they directly connect to the ADC; therefore, the input pin P6_6 is used as the input channel.
Table 1. Application resources
| Resource | Alias/Object | Purpose |
|---|---|---|
| ADC (HAL) | adc_obj | Analog-to-Digital converter driver |
| UART (HAL) | cy_retarget_io_uart_obj | UART HAL object used by Retarget-IO for Debug UART port |
| Resources | Links |
|---|---|
| Application notes | AN235305 – Getting started with TRAVEO™ T2G family MCUs in ModusToolbox™ AN219755 – Using a SAR ADC in TRAVEO™ T2G automotive microcontrollers |
| Code examples | TRAVEO™ T2G MCU examples on GitHub |
| Device documentation | TRAVEO™ T2G body entry family MCUs datasheets TRAVEO™ T2G body entry family MCUs architecture/registers reference manuals |
| Development kits | Select your kits from the Evaluation board finder |
| Libraries on GitHub | mtb-pdl-cat1 – Peripheral driver library (PDL) mtb-hal-cat1 – Hardware abstraction layer (HAL) library retarget-io – Utility library to retarget STDIO messages to a UART port |
| Tools | ModusToolbox™ – ModusToolbox™ software is a collection of easy-to-use libraries and tools enabling rapid development with Infineon MCUs for applications ranging from wireless and cloud-connected systems, edge AI/ML, embedded sense and control, to wired USB connectivity using PSoC™ Industrial/IoT MCUs, AIROC™ Wi-Fi and Bluetooth® connectivity devices, XMC™ Industrial MCUs, and EZ-USB™/EZ-PD™ wired connectivity controllers. ModusToolbox™ incorporates a comprehensive set of BSPs, HAL, libraries, configuration tools, and provides support for industry-standard IDEs to fast-track your embedded application development. |
Infineon provides a wealth of data at www.infineon.com to help you select the right device, and quickly and effectively integrate it into your design. For TRAVEO™ T2G body entry MCU devices, see TRAVEO™ T2G CYT2BL Series
| Version | Description of change |
|---|---|
| 1.0.0 | New code example. |
| 1.1.0 | Added support for KIT_T2G-B-E_LITE |
| 1.2.0 | Removed support for KIT_T2G-B-H_LITE |
All referenced product or service names and trademarks are the property of their respective owners.
The Bluetooth® word mark and logos are registered trademarks owned by Bluetooth SIG, Inc., and any use of such marks by Infineon is under license.
PSOC™, formerly known as PSoC™, is a trademark of Infineon Technologies. Any references to PSoC™ in this document or others shall be deemed to refer to PSOC™.
(c) 2022-2026, Infineon Technologies AG, or an affiliate of Infineon Technologies AG. All rights reserved.
This software, associated documentation and materials ("Software") is owned by Infineon Technologies AG or one of its affiliates ("Infineon") and is protected by and subject to worldwide patent protection, worldwide copyright laws, and international treaty provisions. Therefore, you may use this Software only as provided in the license agreement accompanying the software package from which you obtained this Software. If no license agreement applies, then any use, reproduction, modification, translation, or compilation of this Software is prohibited without the express written permission of Infineon.
Disclaimer: UNLESS OTHERWISE EXPRESSLY AGREED WITH INFINEON, THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, ALL WARRANTIES OF NON-INFRINGEMENT OF THIRD-PARTY RIGHTS AND IMPLIED WARRANTIES SUCH AS WARRANTIES OF FITNESS FOR A SPECIFIC USE/PURPOSE OR MERCHANTABILITY. Infineon reserves the right to make changes to the Software without notice. You are responsible for properly designing, programming, and testing the functionality and safety of your intended application of the Software, as well as complying with any legal requirements related to its use. Infineon does not guarantee that the Software will be free from intrusion, data theft or loss, or other breaches (“Security Breaches”), and Infineon shall have no liability arising out of any Security Breaches. Unless otherwise explicitly approved by Infineon, the Software may not be used in any application where a failure of the Product or any consequences of the use thereof can reasonably be expected to result in personal injury.

