Vernier Adapter

class VernierAdapter(port, conversion=None)

LEGO® MINDSTORMS® Education NXT/EV3 Adapter for Vernier Sensors.

Parameters:
  • port (Port) – Port to which the sensor is connected.

  • conversion (callable) – Function of the format conversion(). This function is used to convert the raw analog voltage to the sensor-specific output value. Each Vernier Sensor has its own conversion function. The example given below demonstrates the conversion for the Surface Temperature Sensor.

voltage() int: mV

Measures the raw analog sensor voltage.

Returns:

Analog voltage.

conversion(voltage) float

Converts the raw voltage (mV) to a sensor value.

If you did not provide a conversion() function earlier, no conversion will be applied.

Parameters:

voltage (Number, mV) – Analog sensor voltage

Returns:

Converted sensor value.

value() float

Measures the sensor voltage() and then applies your conversion() to give you the sensor value.

Returns:

Converted sensor value.

Examples

Using the Surface Temperature Sensor

from math import log

from pybricks.nxtdevices import VernierAdapter
from pybricks.parameters import Port


# Conversion formula for Surface Temperature Sensor
def convert_raw_to_temperature(voltage):

    # Convert the raw voltage to the NTC resistance
    # according to the Vernier Adapter EV3 block.
    counts = voltage / 5000 * 4096
    ntc = 15000 * (counts) / (4130 - counts)

    # Handle log(0) safely: make sure that ntc value is positive.
    if ntc <= 0:
        ntc = 1

    # Apply Steinhart-Hart equation as given in the sensor documentation.
    K0 = 1.02119e-3
    K1 = 2.22468e-4
    K2 = 1.33342e-7
    return 1 / (K0 + K1 * log(ntc) + K2 * log(ntc) ** 3)


# Initialize the adapter on port 1
thermometer = VernierAdapter(Port.S1, convert_raw_to_temperature)

# Get the measured value and print it
temp = thermometer.value()
print(temp)