Main Content

Analog Input

R2026b

Read ADC register value after conversion at Teensy 4.0 and 4.1 analog pins and trigger conversions from FlexPWM events

Since R2026b

  • Arduino Analog Input Teensy 4 Icon

Libraries:
Simulink Support Package for Arduino Hardware / Advanced / TEENSY4

Description

Add-On Required: This feature requires the Simulink Support Package for Arduino Hardware add-on.

The Teensy Analog Input block reads analog-to-digital converter (ADC) register values after conversion from analog pins on Teensy 4.0 and 4.1 boards.

Use this block to:

  • Read up to three analog voltage inputs simultaneously or sequentially.

  • Run ADC conversions in a downstream function-call subsystem triggered by a Hardware Interrupt -configured interrupt service routine (ISR).

    Run ADC conversions in a downstream function-call subsystem triggered by a Hardware Interrupt-configured interrupt service routine (ISR).

    Run ADC conversions in a downstream function-call subsystem triggered by an interrupt service routine (ISR) configured with the Teensy Hardware Interrupt block.

Supported Arduino Boards

  • Teensy 4.0 (Arduino® Compatible)

  • Teensy 4.1 (Arduino Compatible)

Examples

expand all

Read an analog voltage from a Teensy pin and output the ADC value.

Add the Teensy PWM and Teensy Analog Input blocks to your model.

Block diagram showing a Constant block providing a duty cycle value to a Teensy PWM block and a separate Teensy Analog Input block with its ADC output connected to a Display block. Both blocks are configured to use the same FlexPWM timer for synchronized ADC triggering on a Teensy board.

In the model settings, set Hardware board to either Teensy 4.0 (Arduino Compatible) or Teensy 4.1 (Arduino Compatible).

In the Teensy PWM block parameters, set Pin number to a PWM-capable Teensy pin. Identify the FlexPWM timer module associated with the selected pin. For more information, see Map Analog Input pin numbers on Teensy boards.

In the Teensy Analog Input block parameters, set Pin number 1 to a valid Teensy analog input pin.

Set ADC trigger timer to match the FlexPWM timer used by the Teensy PWM block.

Set ADC trigger event to select when the ADC conversion occurs relative to the PWM cycle (for example, Overflow to trigger at the start of each PWM period).

Connect the analog signal to the selected pin on the Teensy board. Use a Scope or Display block to observe the ADC output value.

Generate an interrupt when an ADC conversion completes on a Teensy board and trigger a function-call subsystem using the Teensy Hardware Interrupt block.

Add the Teensy PWM, Teensy Analog Input, and Teensy Hardware Interrupt blocks to your model.

Block diagram showing a Constant block providing a duty cycle value to a Teensy PWM block, a Teensy Analog Input block with its ADC output connected to a Display block, and a Teensy Hardware Interrupt block triggering a Function-Call Subsystem. The Analog Input block is configured with ADC conversion complete interrupt enabled and the Hardware Interrupt block handles the ADC interrupt on a Teensy board.

In the model settings, set Hardware board to either Teensy 4.0 (Arduino Compatible) or Teensy 4.1 (Arduino Compatible).

In the Teensy PWM block parameters, set Pin number to a PWM-capable Teensy pin. Identify the FlexPWM timer module associated with the selected pin. For more information, see Map Analog Input pin numbers on Teensy boards.

In the Teensy Analog Input block parameters, set Pin number 1 to a valid Teensy analog input pin.

Set ADC trigger timer to match the FlexPWM timer used by the Teensy PWM block.

Set ADC trigger event to select when the ADC conversion occurs relative to the PWM cycle.

Select Enable ADC conversion complete interrupt.

In the Teensy Hardware Interrupt block parameters, set Interrupt group to Analog to digital converter (ADC).

Set Interrupt name to ADC_EOC_Channel_1 (or the channel that corresponds to the ADC channel used by the Teensy Analog Input block).

In the Events to serve table, select the ADC RESRDY event to trigger the function-call subsystem when the ADC conversion result is ready.

Extended Examples

Ports

Output

expand all

The block outputs the ADC conversion value that it reads on the analog input pin of the Teensy board. The Teensy boards have 12-bit resolution ADC, so the output ranges from 0 to 4095. The number of output ports depends on the number of analog input pins you specify in the Number of analog input pins parameter. For example, if you set this parameter to 3, the block enables three output ports with port names ADC_Ch1(A0), ADC_Ch2(A1), and ADC_Ch3(A2).

Data Types: uint16

Parameters

expand all

Select how many ADC channels the block reads. The block creates the same number of output ports as the selected channel count.

Specify the Teensy board analog input pin number used for ADC conversion. To open the Arduino pin mapping table, click View pin map. To know how to assign pins for the Analog Input block, see Pin Mapping for Arduino Timer-Independent Blocks.

The number of Pin Number parameters matches the number of analog input pins you specify in the Number of analog input pins parameter.

Select how the block performs ADC conversion when you configure more than one ADC pin.

  • Sequential — The block reads the selected pins one after another and updates the outputs in a sequence. You can read one, two, or three pins in this mode.

  • Simultaneous — The block reads multiple pins at the same time and updates the outputs together. You can use this mode only when Number of analog input pins is 2.

Select the FlexPWM timer that triggers ADC conversion. To synchronize ADC sampling with a PWM output, include a Teensy PWM block in the model and configure both blocks to use the same FlexPWM timer. To view the pin mapping table, see Map PWM Pins to FlexPWM Timer Modules.

For example, if the Teensy PWM block uses pin 4 on Teensy 4.0, the pin mapping table shows this pin maps to FlexPWM2_0. Set ADC trigger timer to FlexPWM2_0.

Select the FlexPWM timing event that triggers the ADC conversion.

  • Overflow — Trigger ADC conversion at the start of each PWM cycle when the PWM timer reloads.

  • PWMCenter — Trigger ADC conversion in the middle of the PWM cycle (central point).

  • RisingEdge — Trigger ADC conversion when the PWM signal transitions from low to high (rising edge).

  • FallingEdge — Trigger ADC conversion when the PWM signal transitions from high to low (falling edge).

This parameter requires a Teensy PWM block in the model configured with the same FlexPWM timer module to generate the trigger signal.

Enable this interrupt after the voltage at the pin of the Teensy board is converted to a digital value. To handle the interrupt, configure the Interrupt group, Interrupt name, and Events to serve in the Teensy Hardware Interrupt block.

More About

expand all

Extended Capabilities

expand all

Version History

Introduced in R2026b