Main Content

readVoltage

Read voltage from analog pin on Arduino hardware

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

Description

voltage = readVoltage(a,pin) reads the voltage on the specified analog input pin on Arduino® hardware.

example

voltage = readVoltage(___,Name=Value) specifies additional options using one or more name-value arguments. You can use this syntax only when you connect the Arduino hardware to your computer over USB. (since R2025a)

example

Examples

collapse all

Create an Arduino object and read voltage from analog input pin.

a = arduino;
readVoltage(a,'A4')
ans = 1.0606

Since R2025a

Create an arduino object and read the voltage from an analog pin for a given number of samples at a specified sample rate.

a = arduino("COM24");
readVoltage(a,"A3",NumSamples=5,SampleRate=200)
ans = 5×1

    3.2551
    3.2551
    3.2551
    3.2600
    3.2551

Since R2025a

Create an arduino object and read the voltage from an analog pin for a given duration and at a specified sample rate.

a = arduino("COM24");
readVoltage(a,"A3",Duration=0.2,SampleRate=50)
ans = 10×1

    3.2502
    3.2551
    3.2551
    3.2551
    3.2551
    3.2551
    3.2551
    3.2551
    3.2551
    3.2551

Since R2025a

Create an arduino object and read the voltage from an analog pin for a given number of samples. Return the read voltage as a matrix.

a = arduino("COM24");
readVoltage(a,"A3",NumSamples=5,OutputFormat="matrix")
ans = 5×1

    3.2551
    3.2551
    3.2551
    3.2551
    3.2551

Since R2025a

Create an arduino object and read the voltage from an analog pin for a given duration and sample rate. Return the read voltage as a timetable.

a = arduino("COM24");
readVoltage(a,"A3",Duration=0.1,SampleRate=200,OutputFormat="timetable")
ans=20×1 timetable
      Time       Voltage
    _________    _______

    0 sec        3.2551 
    0.005 sec    3.2502 
    0.01 sec     3.2551 
    0.015 sec    3.2551 
    0.02 sec     3.2502 
    0.025 sec    3.2551 
    0.03 sec     3.2551 
    0.035 sec    3.2551 
    0.04 sec     3.2551 
    0.045 sec    3.2551 
    0.05 sec     3.2551 
    0.055 sec    3.2551 
    0.06 sec     3.2551 
    0.065 sec    3.2551 
    0.07 sec     3.2551 
    0.075 sec    3.2551 
      ⋮

Input Arguments

collapse all

Arduino hardware connection, specified as an object. Use the arduino object to create the connection.

Analog pin number on the physical hardware, specified as a character vector.

Note

If you connect to the ESP32 board, the pin number must always correspond to a digital pin. For details, see Pin-mapping for ESP32 Boards.

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: readVoltage(a,pin,Duration=0.1,SampleRate = 200,OutputFormat=“timetable”)

Since R2025a

Number of output sample voltages, specified as a positive integer.

NumSamples cannot be greater than 1 if:

  • You set the TraceOn property of the arduino object to True.

  • Arduino hardware communicates with the host computer over Wi-Fi® or Bluetooth®.

Example: readVoltage(a,pin,NumSamples=5)

Data Types: double

Since R2025a

Duration for acquisition of voltage in seconds, specified as positive integer.

Example: readVoltage(a,pin,Duration=5)

Data Types: double

Since R2025a

Sample rate at which to read data from the analog pin in samples/second, specified as positive integer in the range [1, 500].

Example: readVoltage(a,pin,NumSamples=5,SampleRate=200)

Data Types: double

Since R2025a

Output format, specified as matrix or timetable.

Example: readVoltage(a,pin,NumSamples=5,OutputFormat=“timetable”)

Data Types: string | character vector

Note

You can specify the Duration or NumSamples argument, with or without the SampleRate and OutputFormat arguments.

Output Arguments

collapse all

Voltage read from an analog pin on an Arduino hardware, returned as positive integer, matrix, or timetable.

Note

The readVoltage function continuously acquires data when you specify the NumSamples, Duration, and SampleRate arguments.. As it is a blocking function call, MATLAB® waits until the voltage acquisition is complete and all the specified samples are collected before proceeding to the next operation.

More About

collapse all

Version History

Introduced in R2014b

expand all