Main Content

readVoltage

Read voltage from ADC pins on sensor

Since R2023a

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

Description

[voltage,timestamp] = readVoltage(sensorObj,pin) reads the voltage on the specified external ADC pin on lis3dh sensor along with the timestamp. Timestamp is optional.

Note

The readVoltage function is available only for the LIS3DH sensor.

Examples

Read Voltage

Create an Arduino object and include the I2C library.

a = arduino();

Or, you can explicitly specify it in the Libraries Name-Value pair while creating the Arduino object.

clear a;
a = arduino('COM8', 'Uno', 'Libraries', 'I2C');

Create the sensor object for the sensor in use.

sensorObj = lis3dh(a);

Return one sample of voltage data.

sensorObj = 
 
  lis3dh with properties:

               I2CAddress: 24 ("0x18") 
                      Bus: 0 
                   SCLPin: "D21" 
                   SDAPin: "D20"

               SampleRate: 100 (samples/s)
           SamplesPerRead: 10     
                 ReadMode: "latest"     
              SamplesRead: 0     
         SamplesAvailable: 0     
         AvailableADCPins: ["ADC1","ADC2","ADC3"]

Input Arguments

collapse all

The sensor object.

ADC pin on the sensor, specified as a character vector.

Output Arguments

collapse all

Voltage read from a ADC pin on the sensor specified as a numeric double.

The time at which MATLAB® receives voltage data from the sensor, specified as a datetime.

More About

collapse all

Code Generation Using MATLAB Function Block

  • 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.

  • Timestamp returned is always in seconds.

Version History

Introduced in R2023a