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
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)voltage
= readVoltage(___,Name=Value
)
Examples
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
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
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 thearduino
object toTrue
.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
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
Use
readVoltage
in a MATLAB Function block with the Simulink® Support Package for Arduino Hardware to generate code that can be deployed on Arduino Hardware. This functionality is not supported when you use this function with the name-value arguments.Configure the Arduino peripherals to the appropriate mode using
configurePin
before usingreadVoltage
in the MATLAB Function block.
Version History
Introduced in R2014bYou can now use the readVoltage
function to acquire
voltage data at a higher sampling rate. This function also allows you to acquire
voltage data at the specified sample rate for a specific duration or number of
samples, and return data in a particular output format.
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)